Top Maven Plugins for Java Developers

1. Overview

Here is my list of useful and essential maven plugins every Java developer should be aware of. There is a good chance that you might be using many of them without knowing much about them, this article will help you to explore them better.

1. maven-compiler-plugin

The Compiler Plugin is used to compile the sources of your project. Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javaccan accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.
The Compiler Plugin has two goals:
  • 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.

Usage

To compiling Java Sources:
mvn compile
To compile test sources:
mvn test-compile
Example: This is default configuration of compiler plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
  <source>1.8</source>
  <target>1.8</target>
</configuration>
</plugin>
The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats:

2. Maven Surefire Plugin

Plain text files (*.txt) XML files (.xml) By default, these files are generated in ${basedir}/target/surefire-reports/TEST-.xml. Example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
  .....
</configuration>
</plugin>
To skip running the tests for a particular project, set the skipTests property to true.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
  <skipTests>true</skipTests>
</configuration>
</plugin>

3. maven-assembly-plugin

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.
Maven Assembly descriptor can be created and configured in two ways:
  • Pre-defined Descriptor Files
  • Write your own custom descriptor file

4. maven-dependency-plugin

The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location. his plugin has several goals but some of the most useful are following:
  • dependency:analyze - analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.
  • dependency:tree - displays the dependency tree for this project
  • dependency:build-classpath tells Maven to output the path of the dependencies from the local repository in a classpath format to be used in java -cp. The classpath file may also be attached and installed/deployed along with the main artifact.

5. maven-war-plugin

The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive. Example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
  <webResources>
 <resource>
   <!-- this is relative to the pom.xml directory -->
   <directory>resource2</directory>
 </resource>
  </webResources>
</configuration>
</plugin>

6. maven-deploy-plugin

This plugin is responsible for uploading the project artifacts e.g. JAR, WAR, source code, Java docs etc to the internal remote repository. https://maven.apache.org/plugins/maven-deploy-plugin/

7. maven-resource-plugin

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code. Resources Plugin has three goals:
  • resources:resources copies the resources for the main source code to the main output directory.
  • resources:testResources copies the resources for the test source code to the test output directory.
  • resources:copy-resources copies resources to an output directory. Example:
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-resources-plugin</artifactId>
 <version>3.1.0</version>
 <configuration>
   ...
   <encoding>UTF-8</encoding>
   ...
 </configuration>
</plugin>

8. Apache Maven Site Plugin

The Site Plugin is used to generate a site for the project. The generated site also includes the project's reports that were configured in the POM. In Maven, you can use “mvn site” to generate a documentation site for your project information.
mvn site
The generated site is under your project “target/site” folder.

9.Apache Maven PMD Plugin

The PMD Plugin allows you to automatically run the PMD code analysis tool on your project's source code and generate a site report with its results. It also supports the separate Copy/Paste Detector tool (or CPD) distributed with PMD. To include a report with default rule sets and configuration in your project site, set the following in the section of your POM:
<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-pmd-plugin</artifactId>
       <version>3.10.0</version>
</plugin>

10.Apache Maven Checkstyle Plugin

The Checkstyle Plugin generates a report regarding the code style used by the developers.
To generate the Checkstyle report as part of the Project Reports, add the Checkstyle Plugin in the section of your pom.xml.
<project>
  ...
   <reporting>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.0.0</version>
          <reportSets>
            <reportSet>
              <reports>
                <report>checkstyle</report>
              </reports>
            </reportSet>
          </reportSets>
        </plugin>
      </plugins>
    </reporting>
  ...
</project>
Then, execute the site phase to generate the report.
mvn site

11. Apache Maven Clean Plugin

The Clean Plugin is used when you want to remove files generated at build-time in a project's directory. We can run the Clean Plugin automatically during a build. Example: Cleaning a Maven project using the command-line
The Clean Plugin can be called to execute in the command-line without any additional configurations. Like the other plugins, to run the Clean Plugin, you use:
  mvn clean:clean

12. Apache Maven Source Plugin

The Source Plugin creates a jar archive of the source files of the current project. The jar file is, by default, created in the project's target directory. To generate the jar from the command line, use the following command:
mvn source:jar

2. Conclusion

In this post, we have seen different important maven plugins. You can use these plugins your maven projects.
Reference : All the maven plugins available on https://maven.apache.org/plugins/index.html

Comments