How to create a runnable jar with maven that includes dependencies in a separate “lib” folder (not an uber jar)

风流意气都作罢 提交于 2020-01-01 03:45:13

问题


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

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.

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

回答1:


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

mvn dependency:copy-dependencies

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



来源:https://stackoverflow.com/questions/9244738/how-to-create-a-runnable-jar-with-maven-that-includes-dependencies-in-a-separate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!