Maven assembly plugin for custom builds

As we all know, Maven is a java build tool whose convention is better than configuration. Usually, we only need to define very little content, and then we can build the related content of the generated jar and war package according to the properties of the package tag.

 

If you want to package the dependencies in maven together, you need to use maven-assembly-plugin to achieve this. For this plugin, the basic configuration is as follows:

 

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.xxx.Main</mainClass>
                        </manifest>
                    </archive>
 
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
</plugin>
 
 

 

 

In this way, after executing the clean package, a relatively complete jar package will be built. The jar package is suffixed with jar-with-dependencies and contains all the .class files it depends on.

 

However, if we want to do a more complex build in Maven, it is definitely not enough, and we need to configure it differently according to the actual situation.

 

All optional configurations in the maven-assembly-plugin plugin are described in detail in the link. Here are some simple configurations we do according to the needs of the project.

 

We need to specify the specific package configuration file under configuration:

 

<descriptors>                                  
       <descriptor>src/main/assemble/package.xml</descriptor>
</descriptors>
 

 

 

For descriptions of specific elements in the configuration file, you can refer to the following links:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_binaries

 

<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>package</id>
    <formats>
        <format>jar</format>
</formats>

 

 

In package.xml, we first specify its id and format. The id can specify the appendAssemblyId tag under the corresponding configuration in the pom.xml file, and append the id to the generated jar package as a suffix.

<appendAssemblyId>true</appendAssemblyId>

  

There are many dependencies in the project, and many of them exist at the time of execution, and do not need to be packaged into the actual environment (such as the dependent hadoop package). After we sub-module the entire project with maven, the jar packages in the dependent modules need to be packaged together to avoid resetting HADOOP_CLASSPATH.

 

This requires using the dependencySets tag, adding includes, and packaging the specific dependent jar package into the specific generated jar package.

 

<dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <includes>
                <include>${groupId}:${artifactId}:${package type}:${version}</include>
<!--Example: -->
                <include>com.sun.mail:javax.mail:jar:1.5.2</include>
            </includes>
        </dependencySet>
    </dependencySets>

 

The unpack attribute controls whether the dependent jar package is decompressed or placed directly in the root directory. If outputDirectory is set, put these files in this directory. For example, if outputDirectory is set to lib, all dependent files are placed in this directory.

 

If the decompressed file is placed in a specific directory, the jar package may not be used, and a specific CLASSPATH needs to be not specified in the specific MANIFEST.MF file, otherwise the package will not find the specific class. For example, the specific format is as follows:

Manifest-Version: 1.0
Main-Class: windows.VideoWindow
Class-Path: lib\org.eclipse.swt_3.3.0.v3346.jar lib\org.eclipse.swt.win32.win32.x86_3.3.0.v3346.jar
 

 

Class-Path: The following writing method, separated by spaces, must not have a semicolon, and there must be a newline at the end. This file has strange line breaks and whitespace requirements that need to be taken care of when writing it.

 

How to attach a custom MANIFEST.MF file to the packaged jar package, which requires the use of the archieve tag in the configuration.

<archive>                     
      <manifestFile>src/main/assemble/MANIFEST.MF</manifestFile>
</archive>

 

 

At this point, the jar package used in the project can be built normally. During jar package construction, two jar commands are very useful:

jar –tf target/**.jar

  

This command can list all the files in the jar package.

 

jar –xf target/**.jar (specified file)

 

This command can extract all/specified files in the jar package to the current folder.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326554628&siteId=291194637