Yasitha Waduge
Yasitha Waduge

Reputation: 13540

Spring Boot Executable Jar File Without Dependencies

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.

Upvotes: 6

Views: 8822

Answers (5)

JackJun
JackJun

Reputation: 21

I often encounter this problem, but it takes some time to solve it every time. I will share my solution here.

My scene this time is: I have a SpringBoot project with multiple modules. Each module may be dependecy. Because the project is not large, I want to start my service directly with the jar package. However, every time I push fat-jar package is very slow because its size is 100Mb. I try to remove all jar packages except my project module from the package product. I use the following plugins:

<!--   package special class   -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactSet>
            <includes>
                <!--   my project group id   -->
                <include>com.example.your-group-id</include>
            </includes>
        </artifactSet>
    </configuration>
</plugin>
<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>
                <outputDirectory>
                    ${project.build.directory}/libs
                </outputDirectory>
                <excludeGroupIds>
                    <!--   my project group id   -->
                    com.example.your-group-id
                </excludeGroupIds>
            </configuration>
        </execution>
    </executions>
</plugin>

This way, he can move all dependencies to the target/libs directory, and my jar package contains all the modules in my project because they may be updated with each deployment, now, jar file only 700kb. I hope I can help everyone :)

Upvotes: 0

user6439492
user6439492

Reputation:

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>

Upvotes: 1

Jishnu Prathap
Jishnu Prathap

Reputation: 2033

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)

Upvotes: 1

Paul Verest
Paul Verest

Reputation: 63912

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

Upvotes: 3

luboskrnac
luboskrnac

Reputation: 24561

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.

Upvotes: 4

Related Questions