java maven 使用简单的命令行安装和部署 3rd 方依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maven install and deploy 3rd party dependencies with simple command line
提问by Steve
We have a number of third party dependencies that aren't hosted anywhere. For each of these we have a jar file that we'd like to be able to install and/or deploy to our repository. Some of the jar files have their own dependencies and we also need to declare these.
我们有许多未托管在任何地方的第三方依赖项。对于其中的每一个,我们都有一个 jar 文件,我们希望能够安装和/或部署到我们的存储库。一些 jar 文件有自己的依赖项,我们也需要声明这些。
We've made pom.xml files for each jar file that declare the groupId, artifactId, dependencies, etc. These pom.xml files all have a common parent pom that declares some of the common info (e.g. <repositories>
and <distributionManagement>
).
我们为每个 jar 文件创建了 pom.xml 文件,声明了 groupId、artifactId、依赖项等。这些 pom.xml 文件都有一个共同的父 pom,它声明了一些共同的信息(例如<repositories>
和<distributionManagement>
)。
I'd like to be able to install or deploy these dependencies with something as simple as mvn install
and mvn deploy
(or maybe mvn install:install-file
and mvn deploy:deploy-file
) and have all the necessary properties for these commands (artifactId
, repositoryId
, etc.) be read from the pom.xml files.
我希望能够用一些简单的安装或部署这些依赖关系mvn install
和mvn deploy
(或者mvn install:install-file
和mvn deploy:deploy-file
)以及对这些命令(所有必要的属性artifactId
,repositoryId
等等)从pom.xml的文件中读取。
To get this to work, at least for deploying, I tried putting the following in my parent pom:
为了让它起作用,至少为了部署,我尝试将以下内容放入我的父 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>
and then having each of the child poms define the jarfile
property. That allows me to run mvn deploy:deploy-file
to deploy all the child pom artifacts. Presumably I could do something similar to get mvn install:install-file
to work.
然后让每个子 poms 定义jarfile
属性。这允许我运行mvn deploy:deploy-file
以部署所有子 pom 工件。大概我可以做一些类似的事情mvn install:install-file
来上班。
But with this approach, I'm unable to release the parent pom (which I must do since the child poms depend on it), and if I try to mvn release:perform
on the parent pom, I get errors like:
但是使用这种方法,我无法释放父 pom(我必须这样做,因为子 pom 依赖于它),如果我尝试mvn release:perform
在父 pom 上,我会收到如下错误:
Cannot override read-only parameter: pomFile
I feel like I'm probably going about this the wrong way. All I really want to do is:
我觉得我可能会以错误的方式解决这个问题。我真正想做的是:
- Put the common code for all the third party jars in one shared parent pom
- Write an additional minimal pom for each third party jar
- Be able to run something like
mvn install
ormvn deploy
without having to specify all those complicated command line properties
- 将所有第三方 jar 的通用代码放在一个共享的父 pom 中
- 为每个第三方 jar 编写一个额外的最小 pom
- 能够运行类似
mvn install
或mvn deploy
无需指定所有那些复杂的命令行属性的东西
How can I best accomplish that?
我怎样才能最好地做到这一点?
Edit:Made it clearer above that ideally I'd like to be able to run something as simple as mvn install
or mvn deploy
and not have to specify properties on the command line.
编辑:上面说得更清楚,理想情况下,我希望能够运行一些简单的东西,mvn install
或者mvn deploy
不必在命令行上指定属性。
采纳答案by Steve
Ok, I found a solution that allows me to run just mvn install
or mvn deploy
and have the jar file installed to the local or remote repository. Inspired by a post to the maven-users listand using the build-helper plugin, in the parent pom, I have:
好的,我找到了一个解决方案,它允许我只运行mvn install
或mvn deploy
将 jar 文件安装到本地或远程存储库。受到maven-users 列表中的帖子的启发并使用build-helper 插件,在父 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>
And then in the child poms, I have:
然后在子 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>
Some of the pieces of this that initially tripped me up:
最初让我绊倒的一些内容:
- The
attach-artifact
execution should be under<pluginManagement>
so it doesn't get executed if youmvn install
ormvn deploy
the parent pom. - The children need to specify the
build-helper-maven-plugin
under the build plugins so that code from the parent<pluginManagement>
gets run. - The children have to be declared as having
<packaging>pom</packaging>
because you can't add a jar to an artifact if it has the same name as the artifact.
- 该
attach-artifact
执行应当根据<pluginManagement>
,因此没有得到,如果你执行mvn install
或mvn deploy
父POM。 - 子级需要
build-helper-maven-plugin
在构建插件下指定,以便<pluginManagement>
运行父级的代码。 - 必须将子项声明为具有,
<packaging>pom</packaging>
因为如果它与工件具有相同的名称,则不能将 jar 添加到工件中。
The only downside I see to this approach is that the artifact gets deployed as type pom
instead of type jar
. But I haven't seen any real consequences of that.
我认为这种方法的唯一缺点是工件被部署为 typepom
而不是 type jar
。但我还没有看到任何真正的后果。
回答by Brian Topping
When Maven is missing a dependency, it will give you an error in it's output that contains the command line to issue to add the jar to a remote repository. That command line will automatically create a POM for you and upload the two together. Is that not sufficient for your needs?
当 Maven 缺少一个依赖项时,它会在它的输出中给你一个错误,其中包含将 jar 添加到远程存储库的命令行。该命令行将自动为您创建一个 POM 并将两者一起上传。这还不足以满足您的需求吗?
Using this facility, if I know that I have a dependency with no remote repository representation, I usually just go ahead and define it in my build with the GAV that I want, run the build once, then look at the build errors. Copy out the command for the deployment, then run that command. You'll want to make sure that you have the <repository>
elements set up properly in the parent POM plus also set up corresponding <server>
elements in your settings.xml
with the repository upload password. Other than that, you should be good to go.
使用这个工具,如果我知道我有一个没有远程存储库表示的依赖项,我通常只是继续在我的构建中使用我想要的 GAV 定义它,运行一次构建,然后查看构建错误。复制部署命令,然后运行该命令。您需要确保<repository>
在父 POM 中正确设置了<server>
元素,settings.xml
并在您的存储库上传密码中设置了相应的元素。除此之外,你应该很高兴。
I would add that you should check out Nexus before getting to far. It's worth the hassle to set up now! :-)
我想补充一点,你应该在深入了解 Nexus 之前。值得现在设置的麻烦!:-)
回答by Cnetwork
I meet this problem in my work:
我在工作中遇到了这个问题:
Now I have a target.jar (it has a dependencies list : a.jar, b.jar, c.jar...), I want to use
mvn install:install-file
to put it into my local repo, but when I run the command blow
现在我有一个 target.jar(它有一个依赖项列表:a.jar、b.jar、c.jar...),我想用
mvn install:install-file
它把它放到我的本地 repo 中,但是当我运行命令时
mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance -DartifactId=target -Dversion=1.0.0
but when I use it I found there are many error, the jar which use target.jar cannot find a.jar
, b.jar
, c.jar
, such as:
但是当我使用它时,我发现有很多错误,使用 target.jar 的 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
Then I went to ~/.m2/repository/.../target/1.0.0/target.pom
to find the pom file of the target, but nothing in it!
然后我去找~/.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 !!!
This is what going wrong, the install:file -Dfile -DgroupId -D..
does not add dependencies into pom, I correct the answer by this method
这是出了什么问题,install:file -Dfile -DgroupId -D..
没有将依赖项添加到pom中,我通过这种方法纠正了答案
if you already
have this maven project source
, just install it into local repomvn clean install
if you
have the jar and the pom of it
, install jar with pommvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom
if you
don't have a pom with target jar
, write one and use upper command.if you cannot recover the pom file of it, may be you should give up.
如果你已经
have this maven project source
,只需将它安装到本地存储库中mvn clean install
如果你
have the jar and the pom of it
,用 pom 安装 jarmvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom
如果你
don't have a pom with target jar
,写一个并使用上层命令。如果你不能恢复它的 pom 文件,可能你应该放弃。