Spring Boot Maven自定义打包 详解Spring Boot使用Maven自定义打包方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring Boot Maven自定义打包 详解Spring Boot使用Maven自定义打包方式

南国以南i   2021-01-15 我要评论

前言:本文将告诉你如何将程序Jar与与依赖Jar及配置文件分离打包,以下列举了两种不同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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们