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

Spring boot打包jar分離lib和resources方法實例

 更新時間:2020年05月18日 09:19:37   投稿:yaominghui  
這篇文章主要介紹了Spring boot打包jar分離lib和resources方法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

為什么要配置、依賴文件分離:

1.在傳統(tǒng)jar包中,所有文件都打包到一個jar包中,jar非常臃腫,在進行服務更新部署時非常不便,而且傳輸不穩(wěn)定時導致傳輸失敗。如果過實行文件分離、在依賴不改變的情況下,僅僅上傳更新后的 編譯文件是非常方便的。

如果要修改一些配置文件:properties、xml,靜態(tài)文件等可以直接在服務器上編輯。

那么怎么實行配置、依賴文件分離呢?

插件介紹

  • maven-jar-plugin 這個插件式專門用來打包用的,可以配置需要打包進去的文件,程序的入口類等。
  • maven-resources-plugin 這個插件是用來拷貝資源文件的。
  • maven-maven-dependency-plugin 這個插件是用來拷貝依賴庫的。
  • maven-assembly-plugin 可以說包含了以上插件的功能,但是可以做到更精細的控制。
  • spring-boot-maven-plugin 這個不用說,springboot 項目最重要的插件,整個項目的打包處理過程還是要依附于它。

打包成可執(zhí)行jar,不僅僅局限SpringBoot項目(主入口函數(shù)存在)

maven-jar-plugin 插件打包jar

在pom文件中配置,但是這樣 依賴的jar并不會打進來(后面會有解決方法),適用不需要依賴文件的項目。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version>


        <configuration>
          <archive>
            <manifest>

              <!--是否添加依賴-->
              <addClasspath>true</addClasspath>

              <!--設置啟動類-->
              <mainClass>xxx.xxx.Main</mainClass>

            </manifest>
          </archive>
          <!--設置生成jar輸出位置-->
          <outputDirectory>${project.build.directory}</outputDirectory>

        </configuration>
      </plugin> 

maven-assembly-plugin 插件打包jar

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>

          <!--不添加AssemblyId-->
          <appendAssemblyId>false</appendAssemblyId>
          <descriptorRefs>
            <!--配置打包的時候一并打包依賴jar-->
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <!--入口類-->
              <mainClass>xxx.xxx.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <!--綁定生命周期-->
            <phase>package</phase>
            <goals>
              <!--執(zhí)行assembly -->
              <goal>assembly</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

打包SpringBoot 項目

方案一、

<plugins>
    <!--打包jar-->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <!--MANIFEST.MF 中 Class-Path 加入前綴-->
            <classpathPrefix>lib/</classpathPrefix>
            <!--jar包不包含唯一版本標識-->
            <useUniqueVersions>false</useUniqueVersions>
            <!--指定入口類-->
            <mainClass>xxx.xxx.Application</mainClass>
          </manifest>
          <manifestEntries>
            <!--MANIFEST.MF 中 Class-Path 加入資源文件目錄-->
            <Class-Path>/resources</Class-Path>
          </manifestEntries>
        </archive>
        <outputDirectory>${project.build.directory}/dis</outputDirectory>
      </configuration>
    </plugin>

    <!--拷貝依賴 copy-dependencies-->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>
                ${project.build.directory}/dis/lib/
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!--拷貝資源文件 copy-resources-->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <resources>
                <resource>
                  <directory>src/main/resources</directory>
                </resource>
              </resources>
              <outputDirectory>${project.build.directory}/dis/resources</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!--spring boot repackage,依賴 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
         
          <layout>ZIP</layout>
          <!--使用外部配置文件,jar包里沒有資源文件-->
          <addResources>true</addResources>
        
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

方案二

這里依賴assembly.xml 描述文件

        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <appendAssemblyId>false</appendAssemblyId>
              <descriptors>
                <descriptor>assembly.xml</descriptor>
              </descriptors>
              <outputDirectory>${project.build.directory}/dist/</outputDirectory>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

          <!-- 打包成jar文件,并指定lib文件夾以及resources資源文件夾 -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>xxx.xxx.Application</mainClass>
                  <!--依賴前綴-->
                  <classpathPrefix>lib/</classpathPrefix>
                  <addClasspath>true</addClasspath>
                </manifest>
                <manifestEntries>
                  <Class-Path>resources/</Class-Path>
                </manifestEntries>
              </archive>
            </configuration>
          </plugin>
        </plugins>

assembly.xml

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

  <id>distribution</id>

  <!--輸出格式 zip 最終結果生成zip -->
  <formats>
    <format>zip</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <!--設置需要輸出文件-->
  <fileSets>
    <fileSet>
      <directory>src/main/resources/</directory>
      <outputDirectory>/resources</outputDirectory>
    </fileSet>
  
  </fileSets>

  <dependencySets>
    <dependencySet>
      <!--依賴包的輸出目錄-->
      <outputDirectory>/lib</outputDirectory>
      <scope>runtime</scope>
      <excludes>
        <exclude>${project.groupId}:${project.artifactId}</exclude>
      </excludes>
    </dependencySet>
    <dependencySet>
      <!--jar包的輸出目錄-->
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>${project.groupId}:${project.artifactId}</include>
      </includes>
    </dependencySet>
  </dependencySets>
</assembly>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解Java中clone的寫法

    詳解Java中clone的寫法

    這篇文章主要介紹了Java中clone的寫法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Java 線程狀態(tài)和等待喚醒機制和線程池的實現(xiàn)

    Java 線程狀態(tài)和等待喚醒機制和線程池的實現(xiàn)

    這篇文章主要介紹了Java 線程狀態(tài)和等待喚醒機制和線程池的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • 初識Java基礎之數(shù)據(jù)類型與運算符

    初識Java基礎之數(shù)據(jù)類型與運算符

    Java是一種強類型語言,每個變量都必須聲明其數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關于Java基礎之數(shù)據(jù)類型與運算符的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-10-10
  • SpringCloud?Gateway實現(xiàn)API接口加解密

    SpringCloud?Gateway實現(xiàn)API接口加解密

    這篇文章主要為大家介紹了SpringCloud?Gateway如何實現(xiàn)API接口加解密的,文中的示例代碼講解詳細,對我們學習有一定的幫助,需要的可以參考一下
    2022-06-06
  • 頁面設計之事件處理綜合介紹

    頁面設計之事件處理綜合介紹

    頁面設計之事件處理,當你把界面都設計好了,總需要添加相應的執(zhí)行動作給組件,在有相應的時間處理機制
    2012-12-12
  • SpringBoot使用thymeleaf模板過程解析

    SpringBoot使用thymeleaf模板過程解析

    這篇文章主要介紹了SpringBoot使用thymeleaf模板過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • java計算兩點間的距離方法總結

    java計算兩點間的距離方法總結

    小編給大家總結了在java中計算兩點之家距離的方法以及相關實例代碼分享,有需要的讀者參考下。
    2018-02-02
  • Spring?boot?啟動流程及外部化配置方法

    Spring?boot?啟動流程及外部化配置方法

    平時我們開發(fā)Spring boot 項目的時候,一個SpringBootApplication注解加一個main方法就可以啟動服務器運行起來,那它到底是怎么運行起來的呢?這篇文章主要介紹了Spring?boot?啟動流程及外部化配置,需要的朋友可以參考下
    2022-12-12
  • 詳解Spring Cloud Zuul網(wǎng)關修改為短連接方法

    詳解Spring Cloud Zuul網(wǎng)關修改為短連接方法

    本文主要介紹了詳解Spring Cloud Zuul網(wǎng)關修改為短連接方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • 通過Java實現(xiàn)文件斷點續(xù)傳功能

    通過Java實現(xiàn)文件斷點續(xù)傳功能

    用戶上傳大文件,網(wǎng)絡差點的需要歷時數(shù)小時,萬一線路中斷,不具備斷點續(xù)傳的服務器就只能從頭重傳,而斷點續(xù)傳就是,允許用戶從上傳斷線的地方繼續(xù)傳送,這樣大大減少了用戶的煩惱。本文將用Java語言實現(xiàn)斷點續(xù)傳,需要的可以參考一下
    2022-05-05

最新評論

龙南县| 江口县| 夏邑县| 鄂尔多斯市| 乌鲁木齐县| 柞水县| 平凉市| 新干县| 龙州县| 开化县| 普格县| 新和县| 隆化县| 崇礼县| 勐海县| 楚雄市| 邻水| 陈巴尔虎旗| 宁远县| 遂川县| 梨树县| 建平县| 玉环县| 鄄城县| 南昌市| 永宁县| 德安县| 寻甸| 天祝| 唐山市| 五莲县| 宣恩县| 荆州市| 兰西县| 根河市| 邯郸市| 泊头市| 文登市| 韶山市| 来安县| 临西县|