溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

maven?springboot怎么將jar包打包到指定目錄

發布時間:2021-12-18 17:03:11 來源:億速云 閱讀:542 作者:小新 欄目:開發技術

這篇文章將為大家詳細講解有關maven springboot怎么將jar包打包到指定目錄,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

如何將jar包打包到指定目錄

1.目的

將不常用的比如spring,druid等不常用打包到lib目錄,這樣每次上線不需要上傳這些。第三方或者常改動的還打包到本身的jar包內,每次上線都會新打包。

這樣原來的100多M的jar包,可以變成2、3M。

如圖所示:

原來的打包方式

maven?springboot怎么將jar包打包到指定目錄

改后的方式:

maven?springboot怎么將jar包打包到指定目錄

maven?springboot怎么將jar包打包到指定目錄

2.修改pom

簡單解釋一下,includes標簽內就是打入jar包第三方jar。將上面的2M的包解壓縮后,就是includes的包。

如圖所示。

maven?springboot怎么將jar包打包到指定目錄

excludeGroupIds和excludeArtifactIds 是配置不在lib目錄里的包,由于java啟動加載的機制是優先加載jar包,
再加載外部目錄,如果jar包都存在兩個地方,這樣配置就沒有意義了,每次還是得重新發布lib目錄,所以將includes
中的包,再在excludeGroupIds 和 excludeArtifactIds 配置上
。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-api</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-core</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-rpc-api</artifactId>
                    </include>
                    <include>
                        <groupId>com.sinoiov.etc.apollo</groupId>
                        <artifactId>apollo-spring-boot-starter</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludeGroupIds>
                            com.sinoiov.etc.apollo
                        </excludeGroupIds>
                        <excludeArtifactIds>
                            etc-manage-api,etc-manage-core,etc-manage-rpc-api
                        </excludeArtifactIds>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3.修改啟動腳本

原腳本

java -jar etc-manage-service-basic-2.2.0.jar

現腳本 (如果相對目錄不好用,就用絕對目錄試試)

java Dloader.path=../lib  -jar etc-manage-service-basic-2.2.0.jar

jar包外指定配置文件及原理

解決方案

修改maven的pom.xml文件

不拷貝資源文件

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>*</exclude>
        </excludes>
        <filtering>true</filtering>
    </resource>
</resources>

修改打包方式

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
    </configuration>
</plugin>

運行

假設application.properties和application-{profile}.properties都在/tmp/temp/config,jar文件在/tmp/temp

java -Dloader.path=file:///tmp/temp/config,demo-1.0.jar -jar demo-1.0.jar

原理

對比jar包中MANIFEST.MF文件在`ZIP配置前后的區別

配置前:

Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

配置后:

Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

發現是類加載器變了,查看org.springframework.boot.loader包下所有加載器實現:

maven?springboot怎么將jar包打包到指定目錄

查看五個類描述:官方文檔

JarLauncher

Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.

WarLauncher

Launcher for WAR based archives. This launcher for standard WAR archives. Supports dependencies in WEB-INF/lib as well as WEB-INF/lib-provided, classes are loaded from WEB-INF/classes.

PropertiesLauncher

Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn't exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn't exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

關于“maven springboot怎么將jar包打包到指定目錄”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女