Learning the maven-assembly-plugin plugin in the pom file

1. Usage scenarios

If the project is a microservice architecture, the probability of using this plug-in may be relatively high. Usually, ordinary projects do not need such an implementation.

If some of the common functions in the project do not need to be referenced one by one, you need to make the common functions into a jar package.

 

Second, the role of Maven-assembly-plugin

1. Function: If you want to build the written program and the jar package it depends on into a package, it is a standard plug-in provided by maven for packaging tasks.

 2. Other functions:

1) Provide a way to store project dependent elements, modules, website documentation and other files in a single archive file.

2) Packaged into a distribution package in a specified format, supporting various mainstream formats such as zip, tar.gz, jar and war, etc. The specific files to be packaged are highly controllable.

3) Ability to custom include/exclude specified directories or files.

 

3. In general, the use of the plugin maven-assembly-plugin is divided into 3 steps:

First paste a screenshot of my project path:

 

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

2: The description file configures specific parameters

3: Execute mvn assembly:assembly -Dmaven.test.skip=true on the command line

 

Corresponding to step 1 ------> The configuration of pom.xml in the project is as follows, add it in the build:

 

    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <!-- 描述文件路径-->
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

Describe the content of the file assembly.xml:     

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution</id>
    <formats>
        <format>dir</format>
    </formats>
  <!--  <fileSets>
        <fileSet>
            <directory>${project.basedir}\src\main\resources</directory>
            <outputDirectory>\</outputDirectory>
        </fileSet>

    </fileSets>-->
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
            <excludes>
                <exclude>${org.apache.jmeter}:${ApacheJMeter_java}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets></assembly>

3. On the command line, go to the directory where pom.xml is located, and execute mvn assembly:assembly -Dmaven.test.skip=true

Under the target of the project, you can see the jar and the corresponding directory obtained after packaging.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939855&siteId=291194637