SpringBoot迭代發佈JAR瘦身配置

默認狀況下,插件 spring-boot-maven-plugin 會把整個項目打包成一個可運行的Jar包(即所謂的Flat Jar),致使了這個Jar包很大(一般有幾十M+)。現在迭代發佈時常有的事情,每次都上傳一個如此龐大的文件,會浪費不少時間,有些時候上傳的過程當中還會出錯。java

形成Jar包很大的根本緣由就是依賴第三方的jar不少,很大,下面咱們就把第三方的JAR與項目代碼分離,第三方的JAR把移除到lib文件夾中,便可實現爲咱們的可執行JAR瘦身,配置以下:spring

<plugins>
    <!-- spring boot thin jar configuration -->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!-- main 入口 -->
            <mainClass>com.bdfint.logistics.app.driver.LogisticsAppDriverApplication</mainClass>
            <!-- 設置爲ZIP,此模式下spring-boot-maven-plugin會將Manifest.MF文件中的Main-Class設置爲org.springframework.boot.loader.PropertiesLauncher -->
            <layout>ZIP</layout>
            <!-- 須要包含的jar包 -->
            <includes>
                <!-- 不包含任何jar包 -->
                <!--<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>
                    <!-- 須要排除的jar的 groupId -->
                    <excludeGroupIds>
                        com.bdfint.logistics
                    </excludeGroupIds>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

接下來,執行打包命令:mvn clean package -Dmaven.test.skip=true,打包後在target目錄下就包含咱們的JAR和lib目錄,以下圖:apache

CMD定位到target目錄下,執行命令:java -Dloader.path=./lib -jar logistics-app-driver-2.9.1.1.jar,便可把項目JAR跑起來!api

相關文章
相關標籤/搜索