Spring Boot Executable Jar File Without Dependenci

2020-07-18 09:18发布

What is the easiest way to build spring boot jar file without its dependencies? Basically I should be able to keep dependency jar files in a separate folder.

Currently I'm using spring boot maven plugin, however, it creates a Fat jar file with all dependencies.

4条回答
劳资没心,怎么记你
2楼-- · 2020-07-18 09:35

Below is a solution I found on How to Create an Executable JAR with Maven, You just need to put these in your plugins.

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

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>
                    org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
查看更多
冷血范
3楼-- · 2020-07-18 09:41

Just do not use spring-boot-maven-plugin at all and use JAR packaging. This way the build wouldn't package dependencies into the JAR.

查看更多
疯言疯语
4楼-- · 2020-07-18 09:49

spring-boot-maven-plugin has option for repackaging that puts dependencies inside (making uber jar)

You can disable repackaging or make repackaged .jar go with other classifier [2]

  1. http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

  2. http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-07-18 09:59

Replace change your build entry in pom.xml to

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/dependency_jar</outputDirectory>
                  <overWriteReleases>false</overWriteReleases>
                  <overWriteSnapshots>false</overWriteSnapshots>
                  <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
              </execution>
            </executions>
          </plugin>
    </plugins>

In the target folder there will be a dependency_jar folder with all the dependency jars, along with "project_name.jar"(fat jar) and "project_name.jar.original"(jar file of your code)

查看更多
登录 后发表回答