java Maven 将带有依赖项的 jar 部署到 repo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13865255/
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 deploy jar with dependencies to repo
提问by djechlin
I can deploy a jar
by using the following in my pom.xml
and running mvn deploy
:
我可以jar
通过在 mypom.xml
和 running 中使用以下内容来部署mvn deploy
:
<distributionManagement>
<repository>
<id>releases</id>
<url>http://${host}:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://${host}:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
And I can build an executable jar-with-dependencies
using the following:
我可以jar-with-dependencies
使用以下内容构建一个可执行文件:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>deploy</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>my.company.app.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
Problem is I don't know how to stitch these together to deploy the executable jar
to my Maven repo. I don't really know if this is accomplished by a new plugin or by adding a goal or other step to the existing assembly plugin.
问题是我不知道如何将这些拼接在一起以将可执行文件部署jar
到我的 Maven 存储库。我真的不知道这是通过新插件还是通过向现有程序集插件添加目标或其他步骤来完成的。
采纳答案by djechlin
Essentially my difficulty doing this revealed the fact that my pom.xml
was way off the rails already. Everything would have snapped into place on its own. I was formerly doing:
从本质上讲,我这样做的困难揭示了我pom.xml
已经偏离轨道的事实。一切都会自行卡入到位。我以前在做:
- Save all the dependencies into a lib folder
- Build a jar with a classpath slurping up that lib folder
- Use the assembly plugin to make another deployable jar
- 将所有依赖项保存到一个 lib 文件夹中
- 使用包含该 lib 文件夹的类路径构建一个 jar
- 使用 assembly 插件制作另一个可部署的 jar
I think there were several reasons this sort of made sense along the way, especially when my libraries were not well-factored from my applications.
我认为在此过程中有几个原因是有意义的,尤其是当我的库没有从我的应用程序中很好地分解时。
However by deleting 1 and 2 all that is needed is the distributionManagement
section and the deploy phase works automagically. So all in all this is an amazing case of literally adding functionality by deleting large swathes of code.
然而,通过删除 1 和 2 所需要的只是该distributionManagement
部分,并且部署阶段会自动工作。所以总而言之,这是一个通过删除大量代码来真正增加功能的惊人案例。
回答by Bastian Voigt
If you bind the assembly to the packaging phase, it will install in your repository both the "regular" jar and the with-dependencies jar when you do a build:
如果您将程序集绑定到打包阶段,它将在您进行构建时在您的存储库中安装“常规”jar 和带有依赖项的 jar:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>my.company.app.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Then simply run mvn clean install deploy
to upload both jars to your repository.
然后只需运行mvn clean install deploy
即可将两个 jar 文件上传到您的存储库。
回答by Stephan
In order to build a (so-called) über JAR and deploy it using maven, you could also use the shade plugin. The following code is taken from their website but I've made one or two projects using this feature.
为了构建(所谓的)über JAR 并使用 maven 部署它,您还可以使用shade 插件。以下代码取自他们的网站,但我已经使用此功能制作了一两个项目。
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>Hymanofall</shadedClassifierName> <!-- Any name that makes sense -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
In this configuration you get the über JAR as one deployment besides the normal JAR. The user of your JAR can then decide to pull the all-in-one package or the JAR with dependencies based on the classifier.
在此配置中,除了普通 JAR 之外,您还可以将 über JAR 作为一个部署。然后,您的 JAR 的用户可以根据分类器决定是提取多合一包还是具有依赖关系的 JAR。
I'll usually use the shade plugin to build über JARs (or modify the JAR in a way) and use the assembly plugin to build things like installation packages (containing the JAR and possibly other things). I am unsure what the intended goals of the single plugins are however.
我通常会使用 shade 插件来构建 über JAR(或以某种方式修改 JAR)并使用组装插件来构建诸如安装包之类的东西(包含 JAR 和其他可能的东西)。但是,我不确定单个插件的预期目标是什么。
回答by djechlin
The following worked. I'm going to leave this question open a bit because I'm not positive this is best practice, but working is something.
以下工作。我打算让这个问题保持开放,因为我不认为这是最佳实践,但工作是一件很重要的事情。
Problems I notice are that I made up the ID name and I don't know if this is usual practice and that I have to hard code the jar name; it isn't inferred from anything else.
我注意到的问题是我编造了 ID 名称,我不知道这是否是通常的做法,我必须对 jar 名称进行硬编码;它不是从其他任何东西推断出来的。
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>deploy-executable</id>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>target/Monitoring-Client-1.0-SNAPSHOT-jar-with-dependencies.jar</file>
</configuration>
</execution>
</executions>
</plugin>
回答by khmarbaise
First you shouldn't do the creation of the ueber jar in the deploy phase it's better to do this in the package phase. Furthermore the created jar file is usually automatically attached to your artifact and will be transfered to the remote repository (in your case Nexus). You can check this if you simply try to do a mvn install and take a look at the output if the created jar is installed into the local repository. To deploy the results into nexus you need to call mvn deploy.
首先,您不应该在部署阶段创建 ueber jar,最好在打包阶段创建。此外,创建的 jar 文件通常会自动附加到您的工件,并将传输到远程存储库(在您的情况下为 Nexus)。如果您只是尝试执行 mvn install 并查看输出(如果创建的 jar 安装到本地存储库中),则可以检查这一点。要将结果部署到 nexus,您需要调用 mvn deploy。