如何使用maven创建一个可运行的jar,其中包含一个单独的“lib”文件夹(不是超级jar)中的依赖项

时间:2012-02-11 22:48:59

标签: java maven executable-jar

我正在尝试使用Maven创建一个可执行jar文件,该文件包含一个单独目录中的依赖项。我已经看到很多关于创造“uberjar”的问题 - 我不希望如此。我想要的是将依赖项复制到像这样的“lib”目录中(以及带有MANIFEST.MF条目的jar ClassPath文件):

/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,以便我可以压缩/目标/发布并准备好了):

<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>

1 个答案:

答案 0 :(得分:5)

maven依赖插件有一个复制依赖目标,可以做你想要的。

mvn dependency:copy-dependencies

有关可用选项,请参阅here。即'outputDirectory'将依赖项复制到/ lib