我正在为其构建可执行 JAR 文件的人希望所有外部需要的库都位于单独的 lib/
目录中,当他运行我的 JAR 时,该目录将位于他当前的工作目录中。他还要求我从 JAR 中提取 log4j.xml
和 config.properties
文件,以便他可以编辑它们的值。我将如何使用 IntelliJ 和 maven 构建包含此类 list 的 JAR?
最佳答案
这可以在一些 Maven
插件的帮助下完成。
要将所有依赖项
放入不同的文件夹,请使用maven-dependency-plugin
。此示例将它们放在 target/lib
文件夹中。
<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>
要将指定的资源
放入不同的文件夹,请使用maven-resources-plugin
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>log4j.xml</include>
<include>config.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
最后,您需要使用maven-jar-plugin
执行以下操作:
- 从jar中排除资源
- 在您的 jar list 中引用 lib 文件夹中的所有依赖项
- 引用 jar list 中 conf 文件夹中的资源
像下面这样的东西应该可以工作:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>foo.bar.Main</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
<classifier>jar-without-resources</classifier>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>config.properties</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
希望对你有帮助,
将
关于maven - 使用 IntelliJ 和 Maven 构建可执行 jar 的自定义 jar 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23199940/