clean:clean
attempts to clean a project's working directory of the files that we're generated at build-time. By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.The clean plugin can be configured to delete additional files and directories.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>log</directory>
<includes>
<include>**/*.tmp</include>
<include>**/*.log</include>
</includes>
<excludes>
<exclude>**/important.log</exclude>
<exclude>**/another-important.log</exclude>
</excludes>
</fileset>
<fileset>
<directory>${project.basedir}/delete-this-folder</directory>
<includes>
<include>**</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
We can also call the clean plugin automatically during a build.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
compiler:compile
is bound to the compile phase and is used to compile the main source files.compiler:testCompile
is bound to the test-compile phase and is used to compile the test source files.We can instruct the compiler plugin to use the Java version we want.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugins>
[...]
</build>
[...]
</project>
or
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
resources:resources
is bound to the process-resources phase and copies the resources for the main source code to the main output directory.resources:testResources
is bound to the process-test-resources phase and copies the resources for the test source code to the test output directory.resources:copy-resources
copies resources to an output directory. This goal requires that you configure the resources to be copied, and specify the outputDirectory.Here is an example of how to configure and call the copy-resources
goal.
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
surefire:test
is bound to the test phase and runs the unit tests of an application.${basedir}/target/surefire-reports/TEST-*.xml.
Using the surefire plugin, we can run a single test case.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class MySecondTest {
@Test
void thisTestFails() {
Assertions.assertEquals(2, 3);
}
}
$ mvn -Dtest=MySecondTest test
We will need JUnit as a dependency.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
We can instruct Maven to ignore some test files.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>MyFourthTest</exclude>
<exclude>*Second*</exclude>
</excludes>
</configuration>
</plugin>
We can identify specific files and folders, or define patterns to identify them.
The JaCoCo plugin provides code coverage metrics for Java code via integration with JaCoCo, a code coverage library for Java.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
jar:jar
is bound to the package phase and creates a jar file for your project classes inclusive resources.jar:test-jar
creates a jar file for your project test classes.We can customize the Manifest file generated by the plugin. In the example below, we set the main class and add our dependencies to the classpath pointing to their jars in our local maven repository.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${settings.localRepository}</classpathPrefix>
<classpathLayoutType>repository</classpathLayoutType>
<mainClass>it.unibz.inf.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
assembly:single
assembles an application bundle or distribution from an assembly descriptor. This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line).We can automatically create a fat-jar and a source zip (bound to the package phase).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>it.unibz.inf.HelloWorld</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
<descriptorRef>src</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
install:install
is used to automatically install the project's main artifact (the JAR, WAR or EAR), its POM and any attached artifacts (sources, javadoc, etc) produced by a particular project.install:install-file
is mostly used to install an externally created artifact into the local repository, along with its POM. In that case, the project information can be taken from an optionally specified pomFile, but can also be given using command line parameters.deploy:deploy
is used to automatically install the artifact, its pom and the attached artifacts produced by a particular project. Most if not all of the information related to the deployment is stored in the project's pom.deploy:deploy-file
is used to install a single artifact along with its pom. In that case, the artifact information can be taken from an optionally specified pomFile, but can be completed/overriden using the command line.The Javadoc Plugin uses the Javadoc tool to generate javadocs for the specified project.
For details, refer to https://maven.apache.org/plugins/maven-javadoc-plugin/
To start generating Javadoc HTML from you code, add this to your pom.xml
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
</plugin>
Then, simply run:
$ mvn javadoc:javadoc