maven-assembly-plugin plug-in learning in pom files

Posted by phpshift on Tue, 07 May 2019 14:45:04 +0200

Source of the article: https://blog.csdn.net/cx1110162/article/details/78647164?locationNum=2&fps=1

I. Use scenarios

If the project is a micro-service architecture, the probability of using this plug-in is relatively high. Normally, ordinary projects do not need such an implementation.

If a part of the common functionality in the project does not need to be referenced one by one, then the common functionality part needs to be achieved in the jar package.

Maven-assembly-plugin function
1. Function: To build a written program into a package with the jar package it relies on, it is the standard plug-in provided by maven for packaging tasks.

2. Other roles:

1) Provide a single archive that stores engineering dependent elements, modules, website documents and other files.

2) packaged into a specified format distribution package, supporting various mainstream formats such as zip, tar.gz, jar and war, which files are highly controllable.

3) Ability to customize the directory or file that contains/excludes the specified directory.

Overall, there are two steps to implement the plug-in maven-assembly-plugin:

Step 1: Configure maven-assembly-plugin in the pom.xml file and specify the description file

Step 2: Describe the file configuration parameters

The configuration of pom.xml in the corresponding step 1 - --> project is as follows:

<?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">
	<modelVersion>4.0.0</modelVersion>
 
	<groupId>com.study</groupId>
	<artifactId>center</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/assembly/package.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

The single line explanation is as follows:

<plugin>
   <artifactId>maven-assembly-plugin</artifactId> <!--Plug-in reference-->
   <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>  <!--Describe file paths-->
         <descriptor>src/main/assembly/package.xml</descriptor> 
      </descriptors>

   </configuration>
   <executions>
      <execution>
         <id>make-assembly</id>  <!--Arbitrary name -->
         <phase>package</phase>  <!-- Bound to package Life cycle stage -->
         <goals>
            <goal>single</goal>  <!-- Run only once --> 
         </goals>
      </execution>
   </executions>
</plugin>

After configuring step 2 - --> in this way, under the corresponding path of the project, find the package.xml file (description file) (I am new package.xml file in eclipse)


The package.xml file (description file) is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>package</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

Descriptor file description:

1,ID

ID identifier, which is added to the suffix character of the generated file name. If the ID is specified, the target file is ${artifactId}-${id}.tar.gz

<id>package</id>

2,formats

The packaging formats supported by maven-assembly-plugin are 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>dir</format>
    ... Configurable multiple
</formats>

3,dependencySets

To customize the packaging of Engineering dependencies on jar packages, the core elements are as follows:

element type Effect
outputDirectory
String
Specify the package dependency directory, which is relative to the root directory
includes/include*
List<String> Include dependency
excludes/exclude*
List<String> Exclude dependence
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <scope>runtime</scope>
        <excludes>
            <exclude>${groupId}:${artifactId}</exclude>
        </excludes>
    </dependencySet>
</dependencySets>

4,fileSets

Manage the location of a set of files. The core elements are as follows:

element type Effect
outputDirectory
String
Specify the package dependency directory, which is relative to the root directory
includes/include*
List<String> Include dependency
excludes/exclude*
List<String> Exclude dependence
<fileSets>
    <fileSet>
        <directory>src/main/resources</directory>
        <output Directory>/config</output Directory><!--resource file output path-->
    </fileSet>
    <fileSet>
        <directory>src/main/bin</directory>
        <output Directory>/</output Directory><!--bin directory file output path-->
    </fileSet>
    <fileSet>
        <directory>${project.build.directory} </directory> <!--jar file output path-->
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>


Finding the generated package

After these two steps are implemented, under the project path / target directory, there are jar packages as follows:


Open the center-0.0.1-SNAPSHOT file, and the output format conforms to the configuration in the description file. As follows:


Topics: Maven xml Apache encoding