Spring Boot Executable Jar File Without Dependencies

*爱你&永不变心* 提交于 2019-12-06 05:07:48

问题


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.


回答1:


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




回答2:


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.




回答3:


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)




回答4:


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>


来源:https://stackoverflow.com/questions/33607168/spring-boot-executable-jar-file-without-dependencies

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