Maven可以收集项目的所有依赖JAR来帮助应用程序部署吗? [英] Can Maven collect all the dependent JARs for a project to help with application deployment?
问题描述
我刚刚开始使用 Maven ,(评估它,真的),我需要能够为我的应用程序和具有所有依赖项的目录快速生成一个 JAR 文件(适用于例如lib),以便我可以部署这两个以独立的方式运行。使用正确的清单生成JAR文件很容易,但我不知道如何让Maven将当前项目的依赖项复制到可以部署的 lib
目录中。
I'm just starting to use Maven, (evaluating it, really) and I need to be able to quickly generate a JAR file for my application and a directory with all the dependencies (for example, lib) so that I can deploy those two to be run in a stand-alone manner. Generating the JAR file with the proper manifest is easy, but I do not know how to get Maven to copy the dependencies for the current project into a lib
directory that I can deploy.
由于这是一个独立的Java应用程序,因此我不想有兴趣部署到Maven存储库,这也是相当微不足道的,或者至少很容易googleable。
Since this is for a stand-alone Java applications, I am not interested in deploying to a Maven repository, that is also fairly trivial, or at least easily googleable.
我已经发现如何做所有的事情,除了将依赖的JAR文件复制到某个指定的目录中。这是我正在寻找的工作流程:
I've found out how to do everything except copy the dependent JAR files into some specified directory. This is the workflow I'm looking for:
$ mvn clean
$ mvn package
$ cp -r target/{lib,myApp.jar} installLocation
然后,运行作为JAR文件,来自
不管我的 installLocation
的myApp.jar $ CLASSPATH
。
Then, running myApp.jar
from installLocation
as a JAR file should "just work" regardless of my $CLASSPATH
.
尝试预先解决一些答案:
To try and pre-empt some answers:
- 我确实有一个Main类:set,它的工作正常。
- 我也在MANIFEST.MF中设置了类路径,而且工作正常。
- 我已经发现如何使用
< classpathPrefix>
和< classpathMavenRepositoryLayout>
这个工作 - 但只能在我的机器上。 (via:< classpathPrefix> $ {settings.localRepository}< / classpathPrefix>
)
- I do have a Main-class: set, and it works fine.
- I've also set the classpath in the MANIFEST.MF, and that works fine too.
- I've found out how to use
<classpathPrefix>
and<classpathMavenRepositoryLayout>
to make this work -- but only on my machine. (via:<classpathPrefix>${settings.localRepository}</classpathPrefix>
)
推荐答案
你想要调查的是 Maven的依赖插件。在pom.xml中添加类似于以下内容的内容:
What you want to investigate is Maven's dependency plugin. Add something similar to the following to pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>
${project.build.directory}
</outputDirectory>
</configuration>
</plugin>
然后运行 mvn clean依赖关系:copy-dependencies
程序集插件相结合,您可以将所有内容打包成一个自包含的归档文件。
Then run mvn clean dependency:copy-dependencies
to copy perform the copy. Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.
这篇关于Maven可以收集项目的所有依赖JAR来帮助应用程序部署吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!