溫馨提示×

溫馨提示×

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

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

怎么在Spring Boot中使用Maven進行打包

發布時間:2020-12-29 14:03:21 來源:億速云 閱讀:220 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關怎么在Spring Boot中使用Maven進行打包,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一、第一種Maven打包方式,將jar及resources下全部配置文件,拷貝到指定目錄:

<!--配置項--><properties> <!--自定義配置--> <project.jar.output.directory>E:/IDEAFile/file-copy/target/project</project.jar.output.directory></properties>
<build>
  <plugins>
   <!--項目依賴的jar文件,放置默認配置目錄下-->
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>

   <!-- 設置jar的入口類 -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
       <mainClass>com.example.filecopy.FileCopyApplication</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>

  <!-- 使用maven-resources-plugin插件復制resources目錄下所有文件到指定的路徑-->
   <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
     <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
       <goal>copy-resources</goal>
      </goals>
      <configuration>
       <outputDirectory>${project.build.directory}/project</outputDirectory>
       <resources>
        <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
        </resource>
       </resources>
      </configuration>
     </execution>
    </executions>
   </plugin>

   <!--使用maven-antrun-plugin插件將jar復制到指定的目錄下-->
   <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
     <execution>
      <!-- 在maven進行package的時候執行-->
      <phase>package</phase>
      <configuration>
       <tasks>
        <!--todir:是將要復制jar包到的地方,overwrite:是否重寫-->
        <copy todir="${project.jar.output.directory}" overwrite="true">
         <!--獲取父目錄下的target文件夾中的jar-->
         <fileset dir="${project.build.directory}">
          <include name="*.jar"/>
         </fileset>
        </copy>
       </tasks>
      </configuration>
      <goals>
       <goal>run</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

第二種Maven打包方式使用 assembly插件,將jar及配置文件進行壓縮打包到指定目錄:

<plugins>
   <!-- 項目依賴的jar文件,放置默認配置目錄下-->
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>

   <!-- 設置jar的入口類-->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
     <archive>
      <manifest>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
       <mainClass>com.example.filecopy.FileCopyApplication</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>

    <!--assembly插件-->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <!--指定壓縮包名稱-->
     <finalName>project</finalName>
     <!--指定assembly配置文件配置-->
     <descriptors>
      <descriptor>/assembly/assembly.xml</descriptor>
     </descriptors>
     <!--打包tar.gz輸出target文件夾中-->
     <outputDirectory>${project.build.directory}</outputDirectory>
     <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
     <execution>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>

assembly文件:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
 <id>leaves</id>
 <formats>
  <!--壓縮文件形式 可選 zip tar.gz等 -->
  <format>zip</format>
 </formats>
 <includeBaseDirectory>true</includeBaseDirectory>

 <!-- 項目文件處理 -->
 <fileSets>
  <!--配置文件輸出位置根目錄文件夾下-->
  <fileSet>
   <directory>${basedir}/src/main/resources</directory>
   <includes>
    <include>**</include>
   </includes>
   <filtered>true</filtered>
   <outputDirectory>${file.separator}</outputDirectory>
  </fileSet>

  <!-- 項目代碼生成的jar文件放在根目錄 -->
  <fileSet>
   <directory>${project.build.directory}</directory>
   <outputDirectory>${file.separator}</outputDirectory>
   <includes>
    <include>*.jar</include>
   </includes>
  </fileSet>
 </fileSets>
</assembly>

以上就是怎么在Spring Boot中使用Maven進行打包,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

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

AI

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