如何正确地将预编译的jar放入maven项目中?

时间:2014-09-11 09:29:20

标签: java maven

有一个预编译的jar文件。我的消息来源取决于它。我需要:

  • 此文件应添加到classpath
  • 当我执行mvn deploy不带参数时,应将文件上传到远程仓库
  • 当其他人使用我的项目作为依赖项时,该文件应该从远程仓库自动下载并添加到classpath
  • 我希望这个jar有自己的工件ID和pom文件
  • 如果可能的话,我想保留原来的jar名称

我试着将它放在我的pom中:

<repositories>
    <repository>
        <id>pom-local</id>
        <url>file://${basedir}/repo</url>
    </repository>
</repositories>

然后写了另一个pom.xml并将它和jar文件放到${basedir}/repo/。 奇怪的是,根据谷歌,没有人使用这个。我没弄明白如何保留原来的jar名称。我无法让maven部署它。

目标是attach-artifact,但它需要一个文件名,而不是artifactId作为参数,它将jar放到同一个文件夹中。

可能的解决方案:

        <dependency>
            <groupId>org.foo</groupId>
            <version>1.0</version>
            <artifactId>eeerrr</artifactId>
        </dependency>
</dependencies>

<repositories>
    <repository>
        <id>pom-local</id>
        <url>file://${basedir}/repo</url>
    </repository>
</repositories>


<properties>
    <!-- Will use this instead of <distributionManagement>, because it's easier to extract url from one place -->
    <altDeploymentRepository>todo::default::http://0.0.0.0</altDeploymentRepository>
</properties>

<build>
    <plugins>
        <!-- Extract url -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>extract-url</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>deploymentRepository.url</name>
                        <value>${altDeploymentRepository}</value>
                        <regex>.*::(.*)</regex>
                        <replacement>$1</replacement>
                        <failIfNoMatch>true</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Upload additional artifact -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <executions>
                <execution>
                    <id>deploy-3rd-party-jar</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy-file</goal>
                    </goals>
                    <configuration>
                        <groupId>org.foo</groupId>
                        <artifactId>eeerrr</artifactId>
                        <version>1.0</version>
                        <packaging>jar</packaging>
                        <file>${basedir}/repo/org/foo/eeerrr/1.0/eeerrr-1.0.jar</file>
                        <url>${deploymentRepository.url}</url>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

1 个答案:

答案 0 :(得分:0)

假设这个预编译的jar经常没有改变。 您可以使用mvn install:install-file和mvn deploy:deploy-file一次性部署到本地缓存的repo和远程repo。

看看How to deploy jars to nexus with cmd,这是一个类似的问题。