How to put all dependencies in separate folder for runnable jar?

前端 未结 1 1986
不思量自难忘°
不思量自难忘° 2020-12-29 12:14

I\'m using mvn package to create a runnable jar with all dependencies packed inside, which runs fine. But I\'d prefer to have all external dependencies packed i

相关标签:
1条回答
  • 2020-12-29 12:30

    Use the maven-dependencies-plugin to specify an output directory for the copy-dependencies execution.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
            <goal>copy-dependencies</goal>
            </goals>
            <configuration>
            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
          </execution>
        </executions>
    </plugin>
    

    Update:

    To let the jar know where to find the lib folder, you can specify this as a Class-Path value in the manifest using the maven-jar-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
        <archive>
            <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>foo.bar.MainClass</mainClass>
            </manifest>
        </archive>
        </configuration>
    </plugin>
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题