cacert
cacert

Reputation: 2797

Deploying spring boot fat jar

I have a spring boot app which works well in production environment, however lately I have some question marks about fat jar deployment strategy.Total size of the fat jar is about 80 MB and since it all bundled as single jar, every time even I change a single line ,this 80 MB package is redeployed. How can I divide this fat jar in to a main jar and other jars(which are not developed by me) in /lib directory. What choices do I have ?

Upvotes: 0

Views: 2092

Answers (1)

Magnus
Magnus

Reputation: 8300

You have a few options, simplest way (and suggested by official docs) is to just extract the fat jar file.

$ unzip -q myapp.jar
$ java org.springframework.boot.loader.JarLauncher

There are a couple of issues with this, first of all your application code will now be a bunch of class files(not in it's own jar).
The second issue is that you are still using the spring-boot loader, which isn’t providing as much utility anymore and also pollutes the filesystem.

The other option is to change your build to provide you what you want.
With gradle you can use the application plugin, with maven, I would recommend the appassembler plugin.

Appassembler produces the directory target/appassembler/ which contains a bin directory with a start-script, and a repo directory with all of your dependencies.

To use it, you need to disable the spring-boot:repackage task, and tell the appassembler plugin what your main class is.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <programs>
                    <program>
                        <mainClass>com.example.MyMainClass</mainClass>
                        <id>myappname</id>
                    </program>
                </programs>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 1

Related Questions