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

发布时间:2020-12-29 14:03:21 作者:Leah
来源:亿速云 阅读:162

本篇文章给大家分享的是有关怎么在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进行打包,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. spring boot的运行原理
  2. Spring boot打包jar如何分离lib和resources

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot maven

上一篇:利用C语言怎么编写一个推箱子小游戏

下一篇:使用vue3和typeScript怎么实现一个穿梭框功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》