我正在构建可执行JAR文件的人希望所有外部需要的库位于单独的lib/
目录中,该目录在运行我的JAR时将位于其当前工作目录中。他还要求我将log4j.xml
和config.properties
文件从JAR中取出,以便他可以编辑它们的值。如何使用IntelliJ和maven构建具有此类清单的JAR?
答案 0 :(得分:2)
这可以通过一些Maven
插件来完成。
要将所有dependencies
放入其他文件夹,请使用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>
要将指定的resources
放入其他文件夹,请使用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
执行以下操作:
以下内容应该有效:
<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>
希望这有帮助,
威尔