【问题标题】:How to create a runnable jar with maven that includes dependencies in a separate "lib" folder (not an uber jar)如何使用 maven 创建一个可运行的 jar,该 jar 在单独的“lib”文件夹中包含依赖项(不是 uber jar)
【发布时间】: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


    【解决方案1】:

    maven 依赖插件有一个 copy-dependencies 目标,应该做你想做的事。

    mvn dependency:copy-dependencies
    

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

    【讨论】:

    • 嗯,很好,我不知道。但是如何更新我的 jar 清单文件以将该位置用于类路径?
    • manifest customization 怎么样?当然还要使用属性或 Maven 变量。
    猜你喜欢
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2011-04-12
    • 2017-01-01
    • 1970-01-01
    • 2014-01-08
    • 2019-06-19
    • 2018-07-09
    相关资源
    最近更新 更多