Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot fully executable jar without dependencies inside

NOTE: Please, before marking this question as a duplicate make sure you know the difference between executable JAR and fully executable SpringBoot JAR.

The official Spring Boot documentation describes how to build fully executable JAR. Then generated JAR file can be linked from /etc/init.d/ and started/stopped/restarted/statused as a normal unix service without additional scripts or tools like JSVC.

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

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

UPDATE

I want to reduce the artifact size in order to speed up deploy->test->fix cycle. Sometimes I'm working via mobile network and big file size can decrease my job speed dramatically.

In case there is no a simple configuration property or a profile or a command line option I would use a kind of hack.

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

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

I don't think this can be done with jar utility because file utility recognizes fully executable jar as data

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

unlike the usual jar

$ file usual.jar
usual.jar: Java Jar file data (zip)
like image 864
humkins Avatar asked Jun 22 '17 19:06

humkins


People also ask

Does a JAR file contain all dependencies?

the executable jar should NOT contain the dependencies, just enough of your own code to run, with the dependent classes coming from the before mentioned classpath set up in the manifest.

What is plain jar in spring boot?

app-plain. jar is the archive produced by the jar task. This is a plain or standard jar file that contains only the module's classes and resources. You can learn a bit more about this in the documentation for Spring Boot's Gradle plugin.


1 Answers

You may want to consider using Spring Boot Thin Launcher. It creates a jar file with your application code but none of its dependencies. It adds a special thin launcher that knows how to resolve your application's dependences from a remote Maven repository or from a local cache when the jar is executed. Judging by the description of what you want to do, you'd utilise the local cache option.

The configuration of Spring Boot's Maven plugin to produce a fully executable jar that uses the 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>
like image 198
Andy Wilkinson Avatar answered Nov 09 '22 23:11

Andy Wilkinson