MAVEN安装和部署第三方依赖项,具有简单的命令行
题
我们有许多第三方依赖性在任何地方都没有托管。对于每个这些,我们都有一个jar文件,我们希望能够安装和/或部署到我们的存储库。一些JAR文件具有自己的依赖性,我们还需要声明这些文件。
我们已经为每个声明groupID,artifactID,依赖项等的JAR文件制作了pom.xml文件。这些pom.xml文件都有一个共同的父pom,可以声明某些常见信息(例如 <repositories>
和 <distributionManagement>
).
我希望能够以简单的方式安装或部署这些依赖项 mvn install
和 mvn deploy
(或者可能 mvn install:install-file
和 mvn deploy:deploy-file
)并具有这些命令的所有必要属性(artifactId
, repositoryId
, ,等等)可以从pom.xml文件中读取。
为了使它起作用,至少用于部署,我尝试将以下内容放在父母的pom中:
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<file>${jarfile}</file>
<pomFile>pom.xml</pomFile>
<repositoryId>our-id</repositoryId>
<url>our-url</url>
</configuration>
</plugin>
</plugins>
</build>
然后让每个孩子的poms定义 jarfile
财产。这使我能够跑步 mvn deploy:deploy-file
部署所有儿童伪造。大概我可以做类似的事情 mvn install:install-file
去工作。
但是,使用这种方法,我无法释放父po(因为孩子pom依赖于它,所以我必须这样做),如果我尝试 mvn release:perform
在父母的POM上,我会遇到类似的错误:
Cannot override read-only parameter: pomFile
我觉得我可能会以错误的方式进行操作。我真正想做的就是:
- 将所有第三方罐子的通用代码放在一个共享的父母pom中
- 为每个第三方罐子编写一个最小的POM
- 能够运行类似
mvn install
或者mvn deploy
不必指定所有这些复杂的命令行属性
我怎么能最好地完成这件事?
编辑: 更清楚地表明,理想情况下,我希望能够运行像 mvn install
或者 mvn deploy
并且不必在命令行上指定属性。
解决方案 2
好的,我找到了一个解决方案,让我可以运行 mvn install
或者 mvn deploy
并将JAR文件安装到本地或远程存储库中。受到启发 Maven-Users列表的帖子 并使用 构建助手插件, ,在父母的pom中,我有:
<pluginManagement>
<plugins>
<!-- Attach the jar file to the artifact -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${artifactId}-${version}.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
然后在孩子的poms中,我有:
<packaging>pom</packaging>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
最初使我绊倒的一些作品:
- 这
attach-artifact
执行应在<pluginManagement>
因此,如果您没有执行mvn install
或者mvn deploy
父母。 - 孩子们需要指定
build-helper-maven-plugin
在构建插件下,使父母的代码<pluginManagement>
运行。 - 必须宣布孩子
<packaging>pom</packaging>
因为如果它具有与工件相同的名称,则无法在工件中添加罐子。
我看到这种方法的唯一缺点是,工件被部署为类型 pom
而不是类型 jar
. 。但是我还没有看到任何真正的后果。
其他提示
当Maven缺少依赖关系时,它将为您的输出错误,其中包含要发出的命令行以将JAR添加到远程存储库中的命令行。该命令行将自动为您创建POM,并将两者上传在一起。这还不足以满足您的需求吗?
使用此功能,如果我知道我有一个没有远程存储库表示的依赖项,我通常会继续使用我想要的GAV在构建中定义它,请运行一次构建,然后查看构建错误。复制部署的命令,然后运行该命令。您将要确保您有 <repository>
在父型POM加上正确设置的元素还设置了相应的元素 <server>
您的元素 settings.xml
使用存储库上传密码。除此之外,您应该很好。
我会补充说,您应该在遥远之前检查Nexus。现在值得麻烦! :-)
我在工作中遇到了这个问题:
现在我有一个target.jar(它有一个依赖项列表:A.Jar,B.Jar,C.Jar ...),我想使用
mvn install:install-file
将其放入我的本地仓库中,但是当我运行命令时
mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance -DartifactId=target -Dversion=1.0.0
但是当我使用它时,我发现有很多错误,使用target的jar找不到 a.jar
, b.jar
, c.jar
, , 如:
com.cnetwork.a does not exist
com.cnetwork.b does not exist
com.cnetwork.c does not exist
然后我去了 ~/.m2/repository/.../target/1.0.0/target.pom
要找到目标的POM文件,但没有任何内容!
...
<groupId>com.cnetwork.relevance</groupId>
<artifactId>target</artifactId>
<version>1.0.0</version>
....
# no dependencies about a/b/c.jar !!!
这就是出了什么问题, install:file -Dfile -DgroupId -D..
不会将依赖项添加到POM中,我通过此方法更正答案
如果你已经
have this maven project source
, ,只需将其安装到本地存储库中mvn clean install
如果你
have the jar and the pom of it
, ,安装pommvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom
如果你
don't have a pom with target jar
, ,写一个并使用上命令。如果您无法恢复其POM文件,则可能应该放弃。