maven Practical Tips
- Multithreading and Skipping Tests
# Build with four threads and allocate one thread per core based on the number of CPU cores $ mvn -T 4 clean install $ mvn -T 1C clean install -DskipTests # Do not execute the test case, but compile the test case class to generate the corresponding class file under target/test-classes -Dmaven.test.skip=true # Do not execute or compile test case classes # In conjunction with `Parallel Execution'above $ mvn -T 1C clean install -Dmaven.test.skip=true # By specifying the module name that failed before, you can continue with the previous compilation $ mvn -rf :moduleName clean install
maven--plug-in section (assembly plug-in)
maven-assembly-plugin
Simply put, maven-assembly-plugin is used to help package, such as what kind of package to type out, what to include in the package, and so on.
- Introduce the plug-in in the parent project's pom
<properties> <maven.assembly.plugin.version>2.3</maven.assembly.plugin.version> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>${maven.assembly.plugin.version}</version> </plugin> </plugins> </pluginManagement> </build>
- Then reintroduce the plug-in in the pom file in the distribution module responsible for packaging and distribution, as follows:
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <!-- Configuration Executor --> <id>assemble</id> <phase>package</phase> <!-- Bind to package In the life cycle phase --> <goals> <goal>single</goal> <!-- Run once only --> </goals> <configuration> <finalName>flowmgr-${project.version}</finalName> <!--Main class entries, etc.--> <descriptors> <descriptor>src/assembly/bin-common.xml</descriptor> <!--Configuration Description File Path--> </descriptors> <tarLongFileMode>gnu</tarLongFileMode> </configuration> </execution> </executions> </plugin> </plugins> </build>
It mainly involves the definition of two parameters, goal and descriptors
- The parameter single of goal indicates that the module runs only once and that the following commands can be executed to complete operations such as packaging.
1. mvn assembly:single & 2. mvn clean package #Need to bind to the life cycle of the package
- descriptors refers to the packaging executor.Only custom executors are described here.
In general, the built-in assembly descriptor does not meet the diversity requirements, so you need to write your own implementation of the assembly descriptor.With descriptors, specify the package file src/assembly/bin-common.xml, that is, specify the package operation within the configuration file to use this custom assembly descriptor (configured in the custom xml), which requires the following configuration, that is, to introduce a description file:
<configuration> <finalName>flowmgr-${project.version}</finalName> <!--Main class entries, etc.--> <descriptors> <descriptor>src/assembly/bin-common.xml</descriptor><!--Configuration Description File Path--> </descriptors> <tarLongFileMode>gnu</tarLongFileMode> </configuration>
descriptor instance:
<?xml version='1.0' encoding='UTF-8'?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>demo</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly>
The definition is simple:
- ID <id>distribution</id>.ID identifier, a suffix character added to the name of the generated file.If an ID is specified, the target file is ${artifactId}-${id}.tar.gz
- format: specify the packaging type; maven-assembly-plugin supports zip, tar, tar.gz (or tgz), tar.bz2 (or tbz2), jar, dir, war, and can specify multiple packaging formats at the same time
- IncudeBaseDirectory: Specifies whether to include a package layer directory (for example, finalName is output, if the value is true, all files are placed in the output directory, otherwise they are placed directly in the package's root directory);
- fileSets: Specify the set of files to be included, you can define multiple fileSet s;
- Directory: specify the directory to be included;
- outputDirectory: Specifies the destination of the directory currently to be included.
Back in the pom configuration, a custom configuration configuration generates a demo-demo.jar file in the directory output, with the first demo from finalName and the second demo from the id in the assembly descriptor, which is similar to the default packaged jar.
If you only want finalName, add configuration:
<appendAssemblyId>false</appendAssemblyId>
The detailed configuration of the configuration node in assembly.xml, which is the element of the description file, is summarized here, see below.
- dependencySets
Used to customize how a project depends on jar packages, the core elements are shown in the table below.
element | type | Effect |
---|---|---|
outputDirectory | String | Specifies the package dependency directory, which is relative to the root directory |
includes | List<String> | Contains dependencies |
excludes | List<String> | Exclude dependencies |
Example:
<dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <excludes> <exclude>${project.groupId}:${project.artifactId}</exclude> </excludes> </dependencySet> <dependencySet> <outputDirectory>/</outputDirectory> <includes> <include>${project.groupId}:${project.artifactId}</include> </includes> </dependencySet> </dependencySets>
versions-maven-plugin plugin
Plug-ins that modify the project version as a whole
First add in the parent project's pom.xml file
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/versions-maven-plugin --> <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.3</version> </dependency>
Then execute
mvn versions:set -DnewVersion=x.y.z //perhaps mvn clean org.codehaus.mojo:versions-maven-plugin:2.3:set -DnewVersion=x.y.z
The upcoming version is modified to x.y.z and the pom files for each project are backed up. If the new version is incorrect, the new version number is undone
mvn versions:revert
Submit changes when you are sure there are no errors
mvn versions:commit
maven-enforcer-plugin plugin
maven plugin enforcer resolves conflicts and class conflicts between jar s
enforce configuration instance
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.4.1</version> <executions> <execution> <id>enforce-dependencies</id> <goals> <goal>display-info</goal> <goal>enforce</goal> </goals> </execution> </executions> <configuration> <rules> <requireMavenVersion> <version>[3.0.3,)</version> </requireMavenVersion> <requireJavaVersion> <version>1.7.0</version> </requireJavaVersion> </rules> <fail>true</fail> </configuration> </plugin>
The plug-in is bound by default in the validate phase and has three targets, display-info and enforce, help
- display-info is used to display basic environment information
- enforce for rule checking
- Help help help information, MVN enforcer:help-Ddetail=true-Dgoal=<goal-name> The plug-in has a complete list of rules built in:
Execute Command
- enforcer:display-info enforcer:enforce
- enforcer:help -Ddetail=true -Dgoal=enforce
enforce target
Three options are supported:
-skip - a quick way to skip checks via a profile or using -Denforcer.skip=true from the command line.
- fail- if the goal should fail the build when a rule fails. The default is true. If false, the errors will be logged as warnings.
- failFast - if the goal should stop checking after the first failure. The default is false.
maven-shade-plugin
Used to group multiple jar packages into one jar package Java projects typically rely on other third-party jar packages, and when they are finally packaged, you want to include the other jar packages in one jar package. Similar to assembly, use assembly.The assembly is detailed below.
maven-install-plugin
http://rogerming.iteye.com/blog/1980992
https://www.baeldung.com/install-local-jar-with-maven/
Reference resources:
- This article refers to the blog Maven Advanced Play