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 存储库不感兴趣,这也相当简单,或者至少很容易在谷歌上搜索.
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
然后,从 installLocation
运行 myApp.jar
作为 JAR 文件应该正常工作",而不管我的 $CLASSPATH
.
Then, running myApp.jar
from installLocation
as a JAR file should "just work" regardless of my $CLASSPATH
.
尝试抢占一些答案:
- 我确实有一个 Main-class: 集,它运行良好.
- 我还在 MANIFEST.MF 中设置了类路径,这也很好用.
- 我已经找到了如何使用
和
来完成这项工作——但仅限于我的机器.(通过:
)${settings.localRepository}
- 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 dependency:copy-dependencies
来复制执行复制.将此与 assembly plugin 结合起来,您就可以将所有内容打包成一个独立的存档以供分发.
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屋!