如何使用maven创建一个可运行的jar,该maven包含一个单独的“lib”中的依赖项。文件夹(不是超级罐) [英] How to create a runnable jar with maven that includes dependencies in a separate "lib" folder (not an uber jar)

查看:160
本文介绍了如何使用maven创建一个可运行的jar,该maven包含一个单独的“lib”中的依赖项。文件夹(不是超级罐)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Maven to create an executable jar file that includes the dependencies in a separate directory. I've seen a lot of questions on SO about creating an "uberjar"--I don't want that. What I want is to have the depenedencies copied into a "lib" directory like so (and the jar MANIFEST.MF file with a ClassPath entry that points to them):

/myApp
  myApp.SNAPSHOT-1.0.jar
  /lib
    hibernate.jar
    log4j.jar
    ...

作为一个额外的奖励,如果我可以将 / src / main / resources / * 复制到 / myApp / conf 然后将整个 / myApp 目录压缩到 myApp.zip 中。

As an added bonus it would be nice if I could copy /src/main/resources/* into /myApp/conf and then zip up the entire /myApp directory into myApp.zip as well.

编辑:我使用maven-dependency-plugin和maven-resources-plugin以及maven-jar-plugin。这是我在我的pom.xml中包含的内容(它将运行时依赖项复制到/ target / release / lib,这样我就可以压缩/目标/发布并准备好了):

I use the maven-dependency-plugin and the maven-resources-plugin and the maven-jar-plugin. This is what I include in my pom.xml (which copies the runtime dependencies to /target/release/lib so that I can zip up /target/release and it's ready to go):

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


推荐答案

maven依赖插件具有复制依赖性目标,应该做你想做的事。

The maven dependency plugin has a copy-dependencies goal that should do what you want.

mvn dependency:copy-dependencies

参见这里可用选项。即'outputDirectory'将依赖项复制到/ lib

See here for the available options. Namely 'outputDirectory' to copy the dependencies to /lib

这篇关于如何使用maven创建一个可运行的jar,该maven包含一个单独的“lib”中的依赖项。文件夹(不是超级罐)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆