SpringBoot fully executable jar with no dependencies inside

NOTE. Before marking this question as a duplicate, make sure you know the difference between an executable JAR and a fully executable SpringBoot JAR file .

The Official Spring Boot Documentation describes how to create a fully executable JAR . Then the generated JAR file can be linked to /etc/init.d/

and started / stopped / restarted / registered as a regular unix service without additional scripts or tools like JSVC.

But the generated JAR contains all the libraries and can be quite large in size (in my case 70Mb +).

I want to create such a fully executable JAR without libraries, but then to be able to run it as a SystemV service on Linux and link external libraries (JARs) somehow.

UPDATE

I want to reduce the size of the artifact to speed up the deploy-> test-> fix cycle. Sometimes I work over a mobile network, and the large file size can significantly slow down my work speed.

If there is no simple config property or profile or command line parameter, I would use some kind of hack.

In the beginning, I can generate an assembly containing all the dependencies. Then I can unzip it and move all libraries to a special folder.

Then I need to package it again as fully executable and run it pointing to the libraries folder.

I don't think it can be done with a utility jar

, because the utility file

recognizes a fully executable jar asdata

$ file fully-executable.jar
file fully-executable: data

      

unlike an ordinary can

$ file usual.jar
usual.jar: Java Jar file data (zip)

      

+3


source to share


1 answer


You might want to use the Spring Boot thin launcher . It creates a jar file with your application code, but none of its dependencies. It adds a custom thin launcher that knows how to resolve your application dependencies from a remote Maven repository or from your local cache when jar is running. From the description of what you want to do, you are using local cache code.

The configuration of the Spring Boot Maven plugin to create a fully executable jar using a thin launcher looks like this:



<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot.experimental</groupId>
                    <artifactId>spring-boot-thin-layout</artifactId>
                    <version>1.0.3.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

      

+6


source







All Articles