Maven gets its functionality from plugins, so getting to know the common ones is required study for anyone who wants to use Maven efficiently.
maven-compiler-plugin
Controls Java compilation behavior:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<parameters>true</parameters> <!-- keep parameter names, for reflection -->
</configuration>
</plugin>
You can also set these globally with properties:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
maven-surefire-plugin
Runs unit tests (bound to the test phase):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
Skipping tests:
mvn package -DskipTests # compile the tests but do not run them
mvn package -Dmaven.test.skip=true # do not even compile the tests
maven-jar-plugin
Controls jar generation:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Excluding files from the package:
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes>
</configuration>
maven-assembly-plugin
Building a fat jar (one that contains all dependencies):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
maven-resources-plugin
Controls the copying and filtering (variable substitution) of resource files:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Once resource filtering is enabled, ${xxx} in files under src/main/resources/ is replaced with the value of the corresponding Maven property. You have to turn it on in <build>:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
maven-clean-plugin
Customizing the clean behavior:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<filesets>
<fileset>
<directory>logs/</directory>
<includes>
<include>**/*.log</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
A small step forward every day is a new height!