Java Maven 是否可以收集项目的所有依赖 JAR 以帮助部署应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/567996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:20:01  来源:igfitidea点击:

Can Maven collect all the dependent JARs for a project to help with application deployment?

javadeploymentmaven-2desktop-application

提问by rcreswick

I'm just starting to use Maven, (evaluating it, really) and I need to be able to quickly generate a JARfile 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 libdirectory that I can deploy.

我刚刚开始使用Maven,(真的在评估它),我需要能够为我的应用程序快速生成一个JAR文件和一个包含所有依赖项(例如 lib)的目录,以便我可以部署这两个以独立方式运行。使用正确的清单生成 JAR 文件很容易,但我不知道如何让 Maven 将当前项目的依赖项复制到lib我可以部署的目录中。

Since this is for a stand-alone Java applications, I am notinterested in deploying to a Maven repository, that is also fairly trivial, or at least easily googleable.

由于这是一个独立的 Java 应用程序,我对部署到 Maven 存储库感兴趣,这也相当简单,或者至少很容易用 google 搜索。

I've found out how to do everything exceptcopy the dependent JAR files into some specified directory. This is the workflow I'm looking for:

我已经找到了除了将依赖的 JAR 文件复制到某个指定目录之外的所有其他操作。这是我正在寻找的工作流程:

$ mvn clean
$ mvn package
$ cp -r target/{lib,myApp.jar} installLocation

Then, running myApp.jarfrom installLocationas a JAR file should "just work" regardless of my $CLASSPATH.

然后,运行myApp.jarinstallLocation一个JAR文件应该“只是工作”无论我的$CLASSPATH

To try and pre-empt some answers:

尝试抢占一些答案:

  • 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>)
  • 我确实有一个 Main-class: set,它工作正常。
  • 我还在 MANIFEST.MF 中设置了类路径,这也很好用。
  • 我已经找到了如何使用<classpathPrefix><classpathMavenRepositoryLayout>使其工作 - 但仅在我的机器上。(通过:<classpathPrefix>${settings.localRepository}</classpathPrefix>

采纳答案by laz

What you want to investigate is Maven's dependency plugin. Add something similar to the following to pom.xml:

您要调查的是Maven 的依赖插件。将类似于以下内容的内容添加到 pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <outputDirectory>
            ${project.build.directory}
        </outputDirectory>
    </configuration>
</plugin>

Then run mvn clean dependency:copy-dependenciesto copy perform the copy. Combine this with the assembly pluginand you can package everything into a self contained archive for distribution.

然后运行mvn clean dependency:copy-dependencies复制执行复制。将此与程序集插件结合使用,您可以将所有内容打包到一个独立的存档中以进行分发。

回答by stimms

It sure can. You need to use the shade pluginwhich can be done by adding

肯定可以。您需要使用可以通过添加来完成的阴影插件

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.3-SNAPSHOT</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
      </plugin>

to your project.

到您的项目。

回答by Sergey Aldoukhov

Yet another one is appassembler plugin
What I like about it is that it packages the app in a form ready to use (with a .bat file ans such)

另一个是appassembler 插件
我喜欢它的地方是它将应用程序打包成一种可以使用的形式(带有 .bat 文件等)

回答by Gordon Dickens

I did not care for the Shade plugin since it rolls all the packages from all the jars together.

我不关心 Shade 插件,因为它会将所有 jar 中的所有包滚动到一起。

To include all of the external libs you can use the Dependency Plugin as mentioned above.

要包含所有外部库,您可以使用上面提到的依赖插件。

This example will create a "lib" directory under "target/classes" before the "package" phase.

此示例将在“包”阶段之前在“目标/类”下创建一个“lib”目录。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>target/classes/lib</outputDirectory>
        <overWriteIfNewer>true</overWriteIfNewer>
        <excludeGroupIds>
          junit,org.hamcrest,org.mockito,org.powermock,${project.groupId}
        </excludeGroupIds>
      </configuration>
    </execution>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>sources</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <verbose>true</verbose>
    <detail>true</detail>
    <outputDirectory>${project.build.directory}</outputDirectory>
  </configuration>
</plugin>

回答by Amar

Take a look at maven's dependency plugin, specifically the copy-dependencies goal. The usage section describes how to do exactly what you want.

看看 maven 的依赖插件,特别是 copy-dependencies 目标。用法部分描述了如何做你想做的事。

To do it from the command line just do:

要从命令行执行此操作,只需执行以下操作:

$ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR

回答by ozma

Using maven.repo.localone can collect all jars, but, they are collected into a directory with maven hierarchy (.m2).

使用maven.repo.local可以收集所有 jar,但是,它们被收集到具有 maven 层次结构 (.m2) 的目录中。

mvn install -Dmaven.repo.local=./pick/some/folder

You can then collect them (on Linux):

然后你可以收集它们(在 Linux 上):

mkdir flat-repo
find ./pick/some/folder -type f -name "*.jar" | xargs -I'{}' cp '{}' flat-repo/