如何将所有依赖项放在runnable jar的单独文件夹中? [英] How to put all dependencies in separate folder for runnable jar?
问题描述
我正在使用 mvn package
来创建一个可运行的jar,其中包含所有依赖项,运行正常。
但我更喜欢将所有外部依赖项打包在一个单独的文件夹中。那么我需要改变什么呢?
I'm using mvn package
to create a runnable jar with all dependencies packed inside, which runs fine.
But I'd prefer to have all external dependencies packed in a separate folder. What would I have to change therefore?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>my.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
推荐答案
使用 maven-dependencies -plugin
指定 copy-dependencies
执行的输出目录。
Use the maven-dependencies-plugin
to specify an output directory for the copy-dependencies
execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
更新:
要让jar知道在哪里找到 lib
文件夹,您可以将其指定为 Class-Path
清单
中的值使用 maven-jar-plugin
To let the jar know where to find the lib
folder, you can specify this as a Class-Path
value in the manifest
using the maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>foo.bar.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
希望这会有所帮助。
这篇关于如何将所有依赖项放在runnable jar的单独文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!