最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot項(xiàng)目瘦身之如何將jar包與lib依賴分開(kāi)打包

 更新時(shí)間:2025年04月30日 10:18:25   作者:William-Yu  
這篇文章主要介紹了Springboot項(xiàng)目瘦身之如何將jar包與lib依賴分開(kāi)打包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將jar包與lib依賴分開(kāi)打包

方法一:項(xiàng)目和依賴完全分離

maven-jar-plugin 負(fù)責(zé)生成 jar 文件(jar文件中不包含如何依賴),并為 jar 文件的 manifest.mf 文件配置相關(guān)內(nèi)容;maven-dependency-plugin 插件用于在構(gòu)建時(shí)將項(xiàng)目的運(yùn)行時(shí)依賴項(xiàng)復(fù)制到指定目錄。

部署項(xiàng)目時(shí),生產(chǎn)的jar文件要和lib依賴包在同一目錄下。

<build>
        <!-- 生成的jar名稱(chēng) -->
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 生成的jar中不要包含pom.xml和pom.properties這兩個(gè)文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--這里需要修改為你的項(xiàng)目的主啟動(dòng)類(lèi)-->
                            <mainClass>你的啟動(dòng)類(lèi)路徑</mainClass>
                            <!-- 是否使用唯一版本號(hào),控制 MANIFEST.MF 中類(lèi)路徑的版本格式;如果不加,可能會(huì)出現(xiàn)依賴后面加時(shí)間戳-->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!--拷貝依賴的jar外面的lib目錄-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 指定依賴拷貝的輸出目錄 -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- 不排除傳遞性依賴 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 不移除依賴版本號(hào) -->
                            <stripVersion>false</stripVersion>
                            <!-- 僅包含 runtime 范圍的依賴 -->
                            <includeScope>runtime</includeScope>
                            <!-- 排除 common 和 coo 依賴 -->
                            <excludeArtifactIds>common,coo</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

方法二:部分依賴打入jar文件

  • maven-jar-plugin 插件用于生成主 JAR 文件,并配置 MANIFEST.MF 文件中的相關(guān)信息;
  • maven-shade-plugin 插件用于將一些特定的依賴項(xiàng)(例如 common 和 coo)打包到主 JAR 文件中,通常用于創(chuàng)建一個(gè)"uber JAR"(即包含所有依賴的 JAR);
  • maven-dependency-plugin 插件用于在構(gòu)建過(guò)程中將除指定依賴(如 common 和 coo)外的其他所有運(yùn)行時(shí)依賴復(fù)制到 lib/ 目錄;

因?yàn)閙aven-jar-plugin會(huì)生成一個(gè)無(wú)依賴的jar文件,所以不需要的情況下可以刪除掉,maven-antrun-plugin 插件用于執(zhí)行一些額外的任務(wù),比如刪除不需要的 JAR 文件。

部署項(xiàng)目時(shí),生產(chǎn)的jar文件要和lib依賴包在同一目錄下。

<build>
        <plugins>
            <!-- maven-jar-plugin 用于生成主 JAR 文件,并配置 MANIFEST.MF 文件指定lib文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <!-- 包中包含 Maven 描述符(如 pom.xml 和 pom.properties 文件) -->
                        <addMavenDescriptor>true</addMavenDescriptor>
                        <manifest>
                            <!-- 在 MANIFEST.MF 中添加類(lèi)路徑 -->
                            <addClasspath>true</addClasspath>
                            <!-- 指定依賴的類(lèi)路徑前綴為 lib/ -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 指定主啟動(dòng)類(lèi) -->
                            <mainClass>你的啟動(dòng)類(lèi)路徑</mainClass>
                            <!-- 使用非唯一版本(不在依賴路徑中添加版本號(hào)) -->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!-- maven-shade-plugin 用于將指定依賴(如 common 和 coo)打包進(jìn)主 JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- 在 package 階段執(zhí)行 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 使用 shade 目標(biāo) -->
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <!-- 指定需要包含在主 JAR 中的依賴 -->
                                <includes>
                                    <include>com.test:common</include>
                                    <include>com.test:coo</include>
                                </includes>
                            </artifactSet>
                            <!-- 禁用生成附加的 original-xxx.jar 文件 -->
                            <shadedArtifactAttached>false</shadedArtifactAttached>
                            <!-- 禁用生成 dependency-reduced-pom.xml 文件 -->
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <!-- 自定義最終生成的 JAR 包名稱(chēng) -->
                            <finalName>${project.artifactId}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven-dependency-plugin 用于將除 common 和 coo 外的其他依賴拷貝到 lib 文件夾 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- 設(shè)置執(zhí)行的 ID -->
                        <id>copy-lib</id>
                        <!-- 在 package 階段執(zhí)行 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 使用 copy-dependencies 目標(biāo) -->
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 指定依賴拷貝的輸出目錄 -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- 不排除傳遞性依賴 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 不移除依賴版本號(hào) -->
                            <stripVersion>false</stripVersion>
                            <!-- 僅包含 runtime 范圍的依賴 -->
                            <includeScope>runtime</includeScope>
                            <!-- 排除 common 和 coo 依賴 -->
                            <excludeArtifactIds>common,coo</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven-antrun-plugin 用于刪除多余的 JAR 文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- 在 package 階段執(zhí)行 -->
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <!-- 刪除由 maven-jar-plugin 生成的默認(rèn) JAR 文件 -->
                                <delete file="${project.build.directory}/${project.artifactId}-${project.version}.jar" />
                            </tasks>
                        </configuration>
                        <goals>
                            <!-- 使用 run 目標(biāo) -->
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

jar文件和lib打包命令

maven-assembly-plugin 配置是用于 Maven 項(xiàng)目的構(gòu)建過(guò)程,生成一個(gè)壓縮包(tar 或 zip 格式)并將其輸出到指定的目錄

			<!--maven-assembly-plugin 用于打包項(xiàng)目生成壓縮文件-->
            <plugin>
                <!-- 指定使用的插件:maven-assembly-plugin -->
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- 是否將assembly的ID添加到生成包的名稱(chēng)中。設(shè)為false時(shí),不會(huì)在包名中添加ID。 -->
                    <appendAssemblyId>false</appendAssemblyId>

                    <!-- 指定最終生成的tar或zip包的文件名,這里設(shè)置為djys-business。 -->
                    <finalName>build-jar</finalName>

                    <!-- 輸出目錄,生成的tar或zip包會(huì)存放在target目錄下。 -->
                    <outputDirectory>target/</outputDirectory>

                    <descriptors>
                        <!-- 指定引用的assembly配置文件,這里引用src/main/resources/assembly.xml -->
                        <descriptor>src/main/resources/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <!-- 執(zhí)行ID,可以任意命名,這里使用make-assembly。 -->
                        <id>make-assembly</id>

                        <!-- 將該插件綁定到package生命周期階段,當(dāng)執(zhí)行mvn package時(shí)會(huì)調(diào)用該插件。 -->
                        <phase>package</phase>

                        <goals>
                            <!-- 設(shè)置插件目標(biāo),這里使用single目標(biāo),它會(huì)創(chuàng)建單個(gè)壓縮包(tar/zip)。 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

assembly.xml文件內(nèi)容

<assembly xmlns="http://maven.apache.org/ASSEMBLY/3.3.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/3.3.0 http://maven.apache.org/xsd/assembly-3.3.0.xsd">

    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
<!--        &lt;!&ndash;拷貝application.yml文件到j(luò)ar包的外部config目錄下面&ndash;&gt;-->
<!--        <fileSet>-->
<!--            <directory>${basedir}/src/main/resources</directory>-->
<!--            <includes>-->
<!--                <include>*.yml</include>-->
<!--            </includes>-->
<!--            <filtered>true</filtered>-->
<!--            <outputDirectory>${file.separator}config</outputDirectory>-->
<!--        </fileSet>-->

        <!--拷貝lib包到j(luò)ar包的外部lib下面-->
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <!-- 打包需要包含的文件 -->
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <!--如有需要,可以配置多個(gè)需要拷貝的文件即可-->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中如何避免sql注入實(shí)例詳解

    Java中如何避免sql注入實(shí)例詳解

    SQL注入是最常見(jiàn)的攻擊方式之一,它不是利用操作系統(tǒng)或其它系統(tǒng)的漏洞來(lái)實(shí)現(xiàn)攻擊的,而是程序員因?yàn)闆](méi)有做好判斷,被不法用戶鉆了SQL的空子,下面這篇文章主要給大家介紹了關(guān)于Java中如何避免sql注入的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • JAVA遞歸與非遞歸實(shí)現(xiàn)斐波那契數(shù)列

    JAVA遞歸與非遞歸實(shí)現(xiàn)斐波那契數(shù)列

    這篇文章主要為大家詳細(xì)介紹了JAVA遞歸與非遞歸實(shí)現(xiàn)斐波那契數(shù)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • spring boot啟動(dòng)時(shí)加載外部配置文件的方法

    spring boot啟動(dòng)時(shí)加載外部配置文件的方法

    這篇文章主要給大家介紹了關(guān)于spring boot啟動(dòng)時(shí)加載外部配置文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02
  • Spring Boot的單例模式實(shí)現(xiàn)代碼

    Spring Boot的單例模式實(shí)現(xiàn)代碼

    Spring Boot里的單例不是GoF意義上的全JVM 唯一對(duì)象 + 私有構(gòu)造器,而是由 Spring 容器管理的每個(gè) ApplicationContext 僅一個(gè)實(shí)例,本文介紹Spring Boot的單例模式是如何實(shí)現(xiàn)的,感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • 實(shí)現(xiàn)一個(gè)基于Servlet的hello world程序詳解步驟

    實(shí)現(xiàn)一個(gè)基于Servlet的hello world程序詳解步驟

    Java Servlet 是運(yùn)行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來(lái)自 Web 瀏覽器或其他 HTTP 客戶端的請(qǐng)求和 HTTP 服務(wù)器上的數(shù)據(jù)庫(kù)或應(yīng)用程序之間的中間層
    2022-02-02
  • SpringBoot配置MongoDB多數(shù)據(jù)源的方法步驟

    SpringBoot配置MongoDB多數(shù)據(jù)源的方法步驟

    這篇文章主要介紹了SpringBoot配置MongoDB多數(shù)據(jù)源的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 詳解Java字符型常量和字符串常量的區(qū)別

    詳解Java字符型常量和字符串常量的區(qū)別

    Java 中的字符型常量和字符串常量是兩種不同的數(shù)據(jù)類(lèi)型,本文將給大家詳細(xì)介紹一下Java字符型常量和字符串常量的區(qū)別,文中通過(guò)代碼講解的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Java如何利用POI讀取Excel行數(shù)

    Java如何利用POI讀取Excel行數(shù)

    這篇文章主要介紹了java如何利用POI讀取Excel行數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot項(xiàng)目中讀取resource目錄下的文件六種方法

    SpringBoot項(xiàng)目中讀取resource目錄下的文件六種方法

    這篇文章給大家總結(jié)了SpringBoot項(xiàng)目中讀取resource目錄下的文件六種方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-05-05
  • Java 并發(fā)編程創(chuàng)建線程的四種方式示例詳解

    Java 并發(fā)編程創(chuàng)建線程的四種方式示例詳解

    本文對(duì)比Java創(chuàng)建線程的四種方式:繼承Thread類(lèi)(單繼承限制)、實(shí)現(xiàn)Runnable接口(靈活推薦)、實(shí)現(xiàn)Callable接口(支持返回值)、使用線程池(最佳實(shí)踐,資源管理高效),推薦優(yōu)先使用線程池提升系統(tǒng)穩(wěn)定性與資源利用率,感興趣的朋友一起看看吧
    2025-09-09

最新評(píng)論

增城市| 会东县| 卫辉市| 同江市| 安仁县| 呈贡县| 苍梧县| 招远市| 甘孜县| 阿克陶县| 四会市| 桓台县| 萨嘎县| 雷州市| 泽普县| 阿拉善右旗| 中西区| 天长市| 正宁县| 调兵山市| 华坪县| 新昌县| 惠安县| 万盛区| 本溪市| 徐闻县| 宿州市| 新疆| 佛学| 辽宁省| 遵义县| 威远县| 扶沟县| 晋江市| 丹寨县| 蓝山县| 额敏县| 柳州市| 黄浦区| 太仆寺旗| 库车县|