【发布时间】:2012-03-03 22:09:32
【问题描述】:
我正在尝试使用 Maven 创建一个可执行 jar 文件,该文件在单独的目录中包含依赖项。我在 SO 上看到了很多关于创建“uberjar”的问题——我不希望这样。我想要的是将依赖项复制到这样的“lib”目录中(以及带有指向它们的ClassPath 条目的jar MANIFEST.MF 文件):
/myApp
myApp.SNAPSHOT-1.0.jar
/lib
hibernate.jar
log4j.jar
...
如果我可以将/src/main/resources/* 复制到/myApp/conf 然后将整个/myApp 目录压缩到myApp.zip 中,那将是一个额外的好处。
编辑:我使用 maven-dependency-plugin 和 maven-resources-plugin 和 maven-jar-plugin。这是我在 pom.xml 中包含的内容(它将运行时依赖项复制到 /target/release/lib 以便我可以压缩 /target/release 并准备就绪):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>
${project.build.directory}/release/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/release</outputDirectory>
<resources>
<resource>
<directory>src/main/config</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/release/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
【问题讨论】:
标签: java maven executable-jar