Maven plugins for spring boot application developers

Published On: 2020/11/29

Maven is a well known build tool used by many of the java programmers. This tool uses the pom.xml file added in each project to pull the dependencies and to build the project artifacts. A maven build lifecycle consists of different phases and each phase is made up of goals defined in the build plugins. As this article is focusing more on the plugins that can be used in a spring boot project, given below a list of 20 useful plugins for your project.

1. Javadoc Plugin

The Javadoc Plugin generates javadocs using the Javadoc tool. If this plugin is added to the reporting section of the pom, the javadocs will be generated as part of the site generation.

	
  <reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>{latest}</version>
				<configuration>
					<bottom>
						<![CDATA[
                    <script>
                    if (typeof useModuleDirectories !== 'undefined') {
                      useModuleDirectories = false;
                    }
                    </script>
                ]]>
					</bottom>
					<additionalJOption>--allow-script-in-comments</additionalJOption>
				</configuration>
			</plugin>
		</plugins>
	</reporting>

To build the standalone javadocs add this plugin to the build section of the pom.

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>{latest}</version>        
      </plugin>
    </plugins>    
</build>
The usage details could be found here

2. Project Info Report Plugin

This plugin is used to generate reports information about the project. No special configuration is required for this plugin and it is will be executed when running mvn site.

To run the reports selectively, you can configure it to include only the reports that you prefer.

<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>{latest}</version>
        <reportSets>
          <reportSet>
            <reports>
              <report>dependencies</report>
              <report>team</report>
              <report>mailing-lists</report>
              <report>ci-management</report>
              <report>issue-management</report>
              <report>licenses</report>
              <report>scm</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
</reporting>

3. Sunfire Plugin

This plugin is used in the test phase of the build lifecycle to execute the unit tests of an application. The test report generated could be found in the ${basedir}/target/surefire-reports/ folder.

<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>{latest}</version>
        </plugin>
      </plugins>
    </pluginManagement>
</build>
This plugin will be invoked by running mvn test. The usage details could be found here

4. Sunfire Report Plugin

This report plugin uses the generated xml file of sunfire plugin to generate the web interface version (html) of the test results.

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>{latest}</version>
      </plugin>
    </plugins>
  </reporting>
This plugin will be invoked when executing mvn site command and the report will be included in the project report menu.
The standalone can also be generated by executing the command mvn surefire-report:report and in this case the report will be generated in ${basedir}/target/site/surefire-report.html.

The usage details could be found here

5. Versions Plugin

This plugin is used to update the versions of artifacts referenced in the pom.xml. The goals of this plugin can be found here.

3 of the goals that I have used in the jenkins script is given below.

  1. versions:set can be used to set the project version (current module) from the command line. Refer here to get the detailed example. mvn versions:set -DnewVersion=1.0.3-SNAPSHOT

  2. versions:update-parent updates the parent section of a project so that it references the newest available version. mvn versions:update-parent -DallowSnapshots=true udate the parent to latest version.

  3. versions:update-property Sets a property to the latest version in a given range of associated artifacts.

mvn versions:update-property -Dproperty=thirdpartylib.version -DnewVersion=[1.0.5]

6. License Plugin

This plugin manages the license of a maven project and its dependencies. The plugin has goals related to licenses in a project which includes validating/updating license header information in source files, updating the LICENSE.txt file, and retrieving license information from dependencies.

  <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>license-maven-plugin</artifactId>
        <version>2.0.0</version>
        <configuration>
          <includes>**/*.java</includes>
          <copyrightOwners>${project.organization.name} and all other contributors</copyrightOwners>
          <processStartTag>---license-start</processStartTag>
          <processEndTag>---license-end</processEndTag>
          <sectionDelimiter>---</sectionDelimiter>
          <addJavaLicenseAfterPackage>false</addJavaLicenseAfterPackage>
          <trimHeaderLine>true</trimHeaderLine>
          <emptyLineAfterHeader>true</emptyLineAfterHeader>
        </configuration>
  </plugin>
The examples to use this plugin could be found here

An alternative to this plugin is Mycila Maven License plugin from mycila.

7. Checkstyle Plugin

This plugin checks the source code against a configurable set of rules. This plugin support both sun style and google java style configurations. You can find the details on the plugin here and more on the checkstyle configuration could be find here

The configuration given below could be added to the build section of your root pom file to enable the checkstyle validation.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <configuration>
          <configLocation>codestyle/checkstyle.xml</configLocation>
          <excludes>target/**/*</excludes>
          <encoding>UTF-8</encoding>
          <consoleOutput>true</consoleOutput>
          <failsOnError>true</failsOnError>
          <violationSeverity>warning</violationSeverity>
          <failOnViolation>true</failOnViolation>
          <linkXRef>false</linkXRef>
        </configuration>
        <executions>
          <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
    </plugin>

If you need to configure checkstyle in IDEA follow the below steps.

  • Install checksyle and saveactions plugins.
  • In the code style section of the Editor settings, load the checkstyle configuration from the codestyle folder.
  • In the settings of SaveAction plugin select the first two check boxes from the General section and first two check boxes from the Formatting actions sections.

8. Compiler Plugin

This plugin is used to compile the sources of your project. This plugin has two goals (compile and testCompile) . The compile is used to compile the main source files and the testCompile is used to compile the test source files.

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>{latest}</version>
          <configuration>
            <compilerArgs>
              <arg>-Xlint:all</arg>
            </compilerArgs>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

9. Dependency Plugin

This plugin copies and/or unpack artifacts from local or remote repositories to a specified location. Add this plugin to the build section of the pom.xml

    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

10. Clean Plugin

This plugin is used when you want to remove files generated at build-time in a project’s directory. This plugin can be called to execute in a command line without any additional configuration. If you want to execute clean during build time without passing it in the command line then add the plugin to the build section of the pom.xml

    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

11. Deploy Plugin

This plugin used in the deploy phase to upload the generated artifacts to a remote repository(like nexus). This is usually done in the release phase of the build pipeline.

    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

12. Install Plugin

This plugin is used to install the artifacts to the local repository. The local repository can be configured in the /.m2/settings.xml.

    <plugin>
      <artifactId>maven-install-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

13. Resources Plugin

This plugin is used to handle the copying of project resources to the output directory. This will handle the copying of resources for the main source code to the main build output directory and copying of resources for the test source code to the test build output directory.

    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

14. Enforcer Plugin

This plugin is used to control the environment constraints like mavn version, jdk version etc. A list of enforcement rules could be found here

    <plugin>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>{latest}</version>
      <executions>
        <execution>
          <id>Project Structure Checks</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>[3.6.0,)</version>
              </requireMavenVersion>
              <requireJavaVersion>
                <version>${java.version}</version>
              </requireJavaVersion>
              <reactorModuleConvergence />
            </rules>
            <fail>true</fail>
          </configuration>
        </execution>
      </executions>
    </plugin>

15. Failsafe Plugin

This plugin executes the integration test in an application.The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests.As the integration-test wont execute the post-integration-test, it is better to invoke verify. The report generated by this plugin could be found in ${basedir}/target/failsafe-reports/TEST-*.xml.

  
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>{latest}</version>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

16. Site Plugin

This plugin is used to generate a site for the project.

  
    <plugin>
      <artifactId>maven-site-plugin</artifactId>
      <version>{latest}</version>
    </plugin>

This site pages contains the information about the project and the reports generated by the maven plugins used in the project.By default, when you execute the command mvn site the resulting site will be in the target/site/ directory.

If you need to deploy the site in a specific location hosted in a remote site then add the site information to the distribution management.

 
  <distributionManagement>
    <site>
      <id>www.yourcompany.com</id>
      <url>scp://www.yourcompany.com/www/docs/project/</url>
    </site>
  </distributionManagement>

17. Flatten Plugin

This plugin generates a flatted version of the pom.xml which does not contain any build specific and development specific elements. The main advantage of flatten plugin which could be useful in your project is, you can have a parent pom in the version control system and no need to release it to use in child projects. Use a revision property in maven config and use that revision in the parent and child projects. By this way you could avoid releasing parent pom to the maven repository and changing the child pom to have the latest version of parent.

Add the below plugin configuration to the build section of the pom. Usage details of this plugin could be found here

 
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>flatten-maven-plugin</artifactId>
      <version>{latest}</version>
      <configuration>
        <updatePomFile>true</updatePomFile>
        <flattenMode>resolveCiFriendliesOnly</flattenMode>
      </configuration>
      <executions>
        <execution>
          <id>flatten</id>
          <phase>process-resources</phase>
          <goals>
            <goal>flatten</goal>
          </goals>
        </execution>
        <execution>
          <id>flatten-clean</id>
          <phase>clean</phase>
          <goals>
            <goal>clean</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

18. Jacoco Plugin

This plugin (Java code coverage) generates a report on the code coverage. Jacoco will run an agent to check which lines of code are executed when the test is running and prepare the report detailing line coverage, branch(code) coverage and cyclomatic complexity (number of paths needed to cover all the possible paths).

Further details of this plugin could be found here

Add the below plugin configuration to the build section of you pom.

 
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>{latest}</version>
      <executions>
        <execution>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <goals>
            <goal>report</goal>
          </goals>
          <phase>verify</phase>
        </execution>
      </executions>
    </plugin>

You could also add rules to the plugin which ensure that the minimum requirements are met. These rules could be added to the execution section of the plugin configuration.

 
    <execution>
      <id>default-check</id>
      <goals>
        <goal>check</goal>
      </goals>
      <configuration>
        <rules>
          <rule>
            <element>BUNDLE</element>
            <limits>
              <limit>
                <counter>COMPLEXITY</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.60</minimum>
              </limit>
            </limits>
          </rule>
        </rules>
      </configuration>
    </execution>

19. Protobuf Plugin

This plugin integrates the protocol buffer compiler into maven lifecycle. The protoc compiler generates the Java source files from .proto (protocol buffer definition) files for the specified project. This plugin attaches the .proto files to the resources of the project so that it will be available in the released artifact. Further details of this plugin could be found here

 
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>{latest}</version>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
      </configuration>
    </plugin>

This plugin depends on the artifact protobuf-java from google. Add the below dependency to the pom.xml to use this plugin

 
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>${protobuf.version}</version>
    </dependency>

20. Spring Boot Plugin

This plugin provides support for Spring Boot in Apache maven. If this plugin is configured in a spring boot maven project, the maven package command invokes this plugin to create executable jar or war archives. Using this plugin you could start spring boot application with the command mvn spring-boot:run. You could read more about this plugin here.

 
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>${spring-boot.version}</version>
      <executions>
        <execution>
          <id>repackage</id>
          <phase>post-integration-test</phase>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>${start-class}</mainClass>
      </configuration>
    </plugin>

Conclusion

In this article, I have listed the most useful 20 maven plugins for your spring boot project. Though we have many more useful plugins, I frequently use the above listed plugins to create micro services in spring boot.

comments powered by Disqus