Spring boot iterative release JAR slimming configuration

Keywords: Maven Spring Apache Java

By default, the plug-in spring boot Maven plugin packages the whole project into a Jar package that can run (the so-called Flat Jar), resulting in a large Jar package (usually dozens of M +). Nowadays, there are many things in iterative publishing. It will waste a lot of time to upload such a huge file every time, and sometimes there will be errors in the process of uploading.

The root cause of the big jar package is that there are many jars that depend on the third party. Next, we separate the third party's jars from the project code. The third party's jars can be removed to the lib folder to reduce the weight of our executable jars. The configuration is as follows:

<plugins>
    <!-- spring boot thin jar configuration -->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!-- main Entrance -->
            <mainClass>com.bdfint.logistics.app.driver.LogisticsAppDriverApplication</mainClass>
            <!-- Set to ZIP,Under this mode spring-boot-maven-plugin Will Manifest.MF Document Main-Class Set to org.springframework.boot.loader.PropertiesLauncher -->
            <layout>ZIP</layout>
            <!-- Need to include jar package -->
            <includes>
                <!-- Does not contain any jar package -->
                <!--<include>-->
                    <!--<groupId>nothing</groupId>-->
                    <!--<artifactId>nothing</artifactId>-->
                <!--</include>-->
                <include>
                    <groupId>com.bdfint.logistics</groupId>
                    <artifactId>logistics-api</artifactId>
                </include>
                <include>
                    <groupId>com.bdfint.logistics</groupId>
                    <artifactId>logistics-common</artifactId>
                </include>
            </includes>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <!-- third-party jar into lib directory -->
    <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}/lib</outputDirectory>
                    <!-- Need to be excluded jar Of groupId -->
                    <excludeGroupIds>
                        com.bdfint.logistics
                    </excludeGroupIds>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

Next, execute the packing command: mvn clean package -Dmaven.test.skip=true. After packing, our JAR and lib directories will be included in the target directory, as shown below:

Locate the CMD in the target directory and execute the command: java -Dloader.path=./lib -jar logistics-app-driver-2.9.1.1.jar. You can run the project JAR!

Posted by Steve Mellor on Fri, 03 Jan 2020 19:59:28 -0800