欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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名稱 -->
        <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 包名稱 -->
                            <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添加到生成包的名稱中。設(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使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼

    Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼

    這篇文章主要為大家詳細(xì)介紹了Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • SpringBoot詳解MySQL如何實(shí)現(xiàn)讀寫(xiě)分離

    SpringBoot詳解MySQL如何實(shí)現(xiàn)讀寫(xiě)分離

    當(dāng)響應(yīng)的瓶頸在數(shù)據(jù)庫(kù)的時(shí)候,就要考慮數(shù)據(jù)庫(kù)的讀寫(xiě)分離,當(dāng)然還可以分庫(kù)分表,那是單表數(shù)據(jù)量特別大,當(dāng)單表數(shù)據(jù)量不是特別大,但是請(qǐng)求量比較大的時(shí)候,就要考慮讀寫(xiě)分離了.具體的話,還是要看自己的業(yè)務(wù)...如果還是很慢,那就要分庫(kù)分表了...我們這篇就簡(jiǎn)單講一下讀寫(xiě)分離
    2022-09-09
  • 基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    這篇文章主要介紹了基于SpringBoot中activeMq的JmsTemplate的實(shí)例問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Bean的管理與SpringBoot自動(dòng)裝配原理解讀

    Bean的管理與SpringBoot自動(dòng)裝配原理解讀

    在SpringBoot項(xiàng)目中,啟動(dòng)時(shí)自動(dòng)創(chuàng)建IOC容器并初始化bean對(duì)象,支持通過(guò)依賴注入獲取,Bean可以通過(guò)name或類(lèi)型獲取,支持單例和非單例等多種作用域,對(duì)于第三方Bean,推薦在配置類(lèi)中用@Bean標(biāo)識(shí)方法進(jìn)行定義
    2024-11-11
  • IDEA中thymeleaf語(yǔ)法沒(méi)有提示的問(wèn)題及解決

    IDEA中thymeleaf語(yǔ)法沒(méi)有提示的問(wèn)題及解決

    這篇文章主要介紹了IDEA中thymeleaf語(yǔ)法沒(méi)有提示的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java正則替換括號(hào)中的逗號(hào)實(shí)現(xiàn)示例

    java正則替換括號(hào)中的逗號(hào)實(shí)現(xiàn)示例

    本文主要介紹了java正則替換括號(hào)中的逗號(hào)實(shí)現(xiàn)示例,主要介紹了兩種示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • 淺析JAVA中toString方法的作用

    淺析JAVA中toString方法的作用

    以下是對(duì)在JAVA中toString方法的作用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • Java SpringMVC攔截器與異常處理機(jī)制詳解分析

    Java SpringMVC攔截器與異常處理機(jī)制詳解分析

    SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請(qǐng)求驅(qū)動(dòng)類(lèi)型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡?qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡(jiǎn)化開(kāi)發(fā),SpringMVC也是要簡(jiǎn)化我們?nèi)粘eb開(kāi)發(fā)
    2021-10-10
  • java靈活使用mysql中json類(lèi)型字段存儲(chǔ)數(shù)據(jù)詳解

    java靈活使用mysql中json類(lèi)型字段存儲(chǔ)數(shù)據(jù)詳解

    在數(shù)據(jù)庫(kù)設(shè)計(jì)中,面對(duì)一對(duì)多的關(guān)系,如訂單和商品,可以考慮使用單表存儲(chǔ)而非傳統(tǒng)的分表方式,這篇文章主要介紹了java靈活使用mysql中json類(lèi)型字段存儲(chǔ)數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2024-09-09
  • Java應(yīng)用程序CPU100%問(wèn)題排查優(yōu)化實(shí)戰(zhàn)

    Java應(yīng)用程序CPU100%問(wèn)題排查優(yōu)化實(shí)戰(zhàn)

    這篇文章主要介紹了如何排查和優(yōu)化Java應(yīng)用程序CPU使用率達(dá)到100%的問(wèn)題,文中通過(guò)代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2025-02-02

最新評(píng)論