In Maven, there are three main plug-ins that can be packaged:
- Maven-jar-plugin (maven-jar-plugin) is the default package plugin used for the normal project JAR package.
- Maven-shade-plugin for executable JARS, also known as fat Jars;
- Maven-assembly-plugin supports custom packaging structures, custom dependencies, etc.
Maven-assembly-plugin is the most commonly used in our daily use, because there are many shell scripts, SQL scripts,.properties and.xml configuration items in big data projects. Using the Assembly plug-in can make the output structure clear and standardized. To use the plug-in, add the following to the project POM file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}<version>
<executions>
<execution>
<id>make-assembly</id>
<! -- Bind to package lifecycle -->
<phase>package</phase>
<goals>
<! -- Run only once -->
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<! -- Configure descriptor file -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
<! You can also use Maven's pre-configured descriptor <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> -->
</configuration>
</plugin>
</plugins>
</build>
Copy the code
The way an Assembly plug-in is packaged is defined by descriptor. Maven predefined descriptors include bin, SRC, project, jar-with-dependencies, and so on. Jar-with-dependencies add all external dependencies to the generated JAR package. But to really achieve the effect of custom packaging, you need to write your own descriptor file in XML format. The following is a common configuration in our project.
<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>
Copy the code
Id and formats Are the package file formats supported by the Assembly plug-in. There are zip, tar, tar.gz, tar.bz2, JAR, and war. Multiple formats can be defined at the same time. The ID is the identifier added to the package file name as a suffix. In other words, if the preceding configuration is used, the generated file is a R tI fa C T I D −{artifactId} -artifacTID −{version}-assembly.tar.gz.
FileSets /fileSet is used to set the properties of a group of files when they are packed.
Directory: indicates the path of the source directory. Includes/Excludes: Sets which files to include or exclude, and supports wildcards. FileMode: specifies the file properties in this directory, using Unix octal notation. The default value is 0644. OutputDirectory: indicates the path of the generated directory.
Files/File is much the same as fileSets, except that you specify a single file, and you can also use the destName attribute to set a different name from the source file. DependencySets/dependencySet used to set properties for engineering based on documents in the package. Also roughly the same as fileSets, but with two special configurations:
Unpack: Boolean value, false to pack dependencies in their original JAR form, true to pack dependencies in a directory structure unpacked into *.class files. Scope: indicates which scope dependencies are packaged. Compile and provided do not matter, usually write runtime.
After the package is packaged according to the preceding configuration, upload the. Tar. gz file to the server. After decompressing the file, you can obtain a standardized directory structure, such as bin, conf, and lib.