Detailed explanation of Maven packaging plug-in

mavenPackaged libinto use buildplugin, either use assmeblyplugin or not

mavenPlug-in naming convention:
mavenPlug-ins have their own set of naming conventions. The official plug-in naming format is maven-xxx-plugin, and the unofficial plug-in is namedxxx-maven-plugin

mavenThere are three packaging methods:

  • maven-jar-plugin, the default packaging plug-in, used to package ordinary project JARpackages;
  • maven-shade-plugin, used to make executable JARpackages, also known as fat JARpackages;
  • maven-assembly-plugin, supports custom packaging structure, and can also customize dependencies, etc.

There is also spring-boot-maven-plugina plugin package

1 assmebly

1.1 Introduction to assmebly

Maven-assembly-pluginIt is mavena standard plug-in provided for packaging tasks, which can realize custom packaging.
It mainly provides the following functions:

  • Provide a way to store project dependent elements, modules, website documents and other files into a single archive file
  • Packaged into a distribution package in a specified format, supporting various mainstream formats such as zip, tar.gz, jarand waretc. Which files are packaged is highly controllable
  • Ability to customize 包含/排除specified directories or files

assmebly is downloaded from the official website: http://maven.apache.org/plugins/maven-assembly-plugin/download.cgi

1.2 assmebly configuration

1.2.1 Configuration in maven

Configured in the tab mavenof<build>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>${maven-assembly-plugin.version}<version>
            <configuration>
             	<!-- 配置描述符文件 指定文件位置 -->
                <descriptor>assembly/assembly.xml</descriptor> 
            </configuration>
            <executions>
            	<execution>
                	<id>make-assembly</id>
                    <!-- 绑定到package生命周期 -->
                    <phase>package</phase>
                    <goals>
                    	<!-- 只运行一次 -->
                    	<goal>single</goal>
                    </goals>
                 </execution>
             </executions>
         </plugin>
     </plugins>
</build>

1.2.2 maven label

1.2.2.1 Configuration label description

descriptorIn the label assembly.xmlis the specified configuration file location
insert image description here

1.2.3 assembly.xml configuration

<assembly>
    <id>assembly</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
        	<!--  启动信息 -->
            <directory>src/main/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <!--  配置信息 -->
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/sql</directory>
            <includes>
                <include>*.sql</include>
            </includes>
            <outputDirectory>sql</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/classes/</directory>
            <includes>
                <include>*.properties</include>
                <include>*.xml</include>
                <include>*.txt</include>
            </includes>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}.jar</source>
            <outputDirectory>.</outputDirectory>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

1.2.4 assembly label

1.2.4.1 id tag

idIdentifier, a suffix added to the name of the generated file. If specified id, the target file is${artifactId}-${id}.tar.gz

1.2.4.2 formats tag

formatsThere are assemblypackaging formats supported by the plug-in zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war, and multiple packaging formats can be specified at the same time

<formats> 
     <format>tar.gz</format> 
     <format>dir</format>
 </formats>

1.2.4.3 includeBaseDirectory tag

includeBaseDirectoryThe label specifies whether the package contains the packaging layer directory (for example finalName, terminal-dispatchif the value is true, all files are placed in terminal-dispatchthe directory in the package, otherwise they are directly placed in the root directory of the package)
insert image description here

1.2.4.4 dependencySets tag

It is used to customize jarthe packaging method of project dependent packages. The core elements are shown in the following table:

  • outputDirectory: Specifies the package dependency directory, which is relative to the root directory
  • includes/include*: contains dependencies
  • excludes/exclude*: exclude dependencies
  • useProjectArtifactjar: Specify whether to include the package generated by the project itself when packaging
  • unpack: Boolean value, falseindicating that the dependency will be packaged in the original JARform, trueand it will be *.classpackaged in the directory structure of the file
  • scope: Indicates which scope of dependencies will be packaged. compileIt doesn’t matter if it’s with or providednot, it’s usually writtenruntime

1.2.4.5 fileSets tag

fileSetsTags are used to set the properties of a group of files when packaging:

  • directory: The path to the source directory.
  • includes/excludes: Set which files to include or exclude, wildcards are supported.
  • fileMode: Specify the file permissions under this directory, using Unixoctal description method, User Group Otherthe default value of the attribute ( ) is 0644,Read = 4,Write = 2和Execute = 1
  • outputDirectory: Specifies the output directory of the file collection, the path to the generated directory, which is relative to the root directory

1.2.4.6 files tag

filesThe label can specify 目的文件the name to the specified directory, and the others are fileSetsthe same as . The core elements are shown in the following table:

  • source: source file, relative path or absolute path
  • outputDirectory: output directory
  • destName: target filename
  • fileMode:File Permissions

2 pack directly

In this way, there is no need to configure the assmebly file, just pack it directly

2.1 Example files

  <build>
        <plugins>  
        <!-- maven依赖打包配置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
 <!-- maven主文件打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <!-- <version>2.4</version>-->
                <!-- 对要打的jar包进行配置 -->
                <configuration>
                    <!-- Configuration of the archiver -->
                    <archive>
                        <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <!-- Manifest specific configuration -->
                        <manifest>
                        	<!-- 指定运行类 -->
                            <mainClass>
                                cn.test.App
                            </mainClass>
                            <!--是否要把第三方jar放到manifest的classpath中-->
                            <addClasspath>true</addClasspath>
                            <!-- 生成的manifest中classpath的前缀, 因为要把第三方jar放到lib目录下, 所以classpath的前缀是lib/ -->
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                    <!--过滤掉不希望包含在jar中的文件-->
                    <excludes>
                        <!-- 排除不需要的文件夹(路径是jar包内部的路径) -->
                        <exclude>**/assembly/</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

3 Use the SpringBoot plugin to package

3.1 Introduction

spring-boot-maven-pluginis a packaged plugin spring bootprovided maven. You can type Directly Runnable jar包or war包.
If you use 2.2.1.RELEASEversion, you need mavenversion 2.0 and above, JDK 1.8 and above

3.2 pom example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.6.11</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>mine</artifactId>

    <dependencies>
        ···
    </dependencies>

    <build>
    	<!-- mavne打包动态修改替换配置文件中的占位符 -->
	    <resources>
	        <resource>
	            <directory>${basedir}/src/main/resources</directory>
	            <!-- 处理其中的可替换参数(@..@符合标注的变量) -->
	            <filtering>true</filtering>
	        </resource>
	        <!-- 可以配置引入哪些配置文件includes或排除哪些文件excludes -->
	    </resources>

        <plugins>
        	<!-- 跳过单元测试的 插件-->
			<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-surefire-plugin</artifactId>
			    <version>2.19.1</version>
			    <configuration>
			     	<!-- 默认关掉单元测试,不用手动关闭了 -->
			        <skipTests>true</skipTests>
			    </configuration>
			</plugin>
			<!-- 配置文件处理插件  -->
	        <plugin>
	            <groupId>org.apache.maven.plugins</groupId>
	            <artifactId>maven-resources-plugin</artifactId>
	            <version>3.1.0</version>
	            <configuration>
	                <encoding>utf-8</encoding>
	                <!-- 是否使用默认占位符@@ -->
	                <useDefaultDelimiters>true</useDefaultDelimiters>
	            </configuration>
	        </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定该 Main Class 为全局的唯一入口 -->
                    <mainClass>cn.test.App</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                        <!-- 将依赖到的包都放进去 -->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3.3 Plug-in details

The plugin provides 6 maven goal:

  • build-info: Generate the build information file for the projectbuild-info.properties
  • help: spring-boot-maven-pluginHelp information for display. Use the command line mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name>to display the parameter description information of goal.
  • repackage: Can generate executable jar package or war package. The core goal of the plugin.
  • run: Run the Spring Boot application
  • start: During the integration testing phase, control the life cycle
  • stop: During the integration testing phase, control the life cycle

3.3.1 Packaging repackage

Introduce and execute the command to create a jar package (the plug-in creates a jar package by default), and the in spring-boot-maven-pluginthe folder is the executable jar package. Packaging is mainly used , it is set for plugins . This is bound to the life cycle of , the full command is . After mvn package performs packaging, repackage packages again to generate an executable jar package or war package. By default, the generated package has the same name as the original generated package, which was renamed topommvn packagetarget*.jar
repackage goalspring-boot-starter-parent默认goalgoalmavenpackagemvn package spring-boot:repackage
repackagemvn package*.origin

repackageThe package generated by the command will contain all the dependencies introduced by the project scopeby default , including the provieddependencies for . When creating the war package, you should also set the optional of spring-boot-devtools to true or set the scope to provided.
spring-boot-devtoolsspring-boot-devtoolsrepackageexcludeDevtoolstrue

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>repackage</id>
                    <configuration>
                        <excludeDevtools>true</excludeDevtools>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

repackageproperties are Manifestwritten to the file Main-Class and Start-Class. It can be configured via plugins when the defaults don't make the program work properly. ManifestThe file is located in the META-INF folder of .

When creating an executable jar package, the example is as follows:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: beauty
Start-Class: com.demo.beauty.BeautyApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_251
Main-Class: org.springframework.boot.loader.JarLauncher

Corresponding plugin

build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.demo.beauty.BeautyApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

It can be seen that when the executable jar package is opened, spring-boot-maven-pluginthe mainClassparameter corresponds to the attribute Manifestin the file Start-Class, which is the startup class of the project.

3.3.2 layout tag

ManifestThe attribute in the file is determined Main-Classby the plugin's layout. layoutThe attribute value defaults to jar/war. layoutThe types are:

  • JAR, the usual executable jar.Main-Class : org.springframework.boot.loader.JarLauncher
  • WAR, the usual executable war. Main-Class : org.springframework.boot.loader.warLauncher. In order to avoid possible conflicts when the war package is deployed and run in the container, providedall types of dependencies are placed in the folder warof the executable package , including the built-in container required to run the war directly.WEB-INF/lib-provided
  • ZIP, can also be used as DIR, similar to JAR.Main-Class : org.springframework.boot.loader.PropertiesLauncher
  • NONE, to package all dependent libraries and project resources, but not to package any starters. It can be seen that layoutwhen it is NONE, the org folder in the exported package is gone, there is no Start-Class attribute in the Manifest file, and the Main-Class attribute value is the startup class of the project.
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: beauty
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_251
Main-Class: com.demo.beauty.BeautyApplication

Guess you like

Origin blog.csdn.net/qq_38747892/article/details/130521352