I already mentioned earlier, maven is just a framework of plugins. This article talks about the essential maven plugins, handy for every developer. There are Maven plugins for almost every task. Make sure you have read my previous article on Maven plugins in-depth.

In Effective Pom, I showed you that by default there are several maven plugins that get inherited from the parent pom. But, occasionally you may need to configure them or add a new plugin based on your need. This is what is discussed in this tutorial. Lifecycle plugins are by default available in any maven project and they are discussed below.

The complete plugin list is available on the official maven site for reference, but I am discussing a few of the important ones.

1. Maven clean plugin

  • Maven clean plugin is associated with the mvn clean goal (clean lifecycle).
  • It removes the generated files during build process so that the fresh build happens correctly. By default, it removes the /target directory which is the root folder for generated files.
  • Generated files are class files, zip files, jar or war files, or any other files that are important for the application to run.
...
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...

Maven Clean lifecycle is already covered in detail earlier.

2. Maven compiler plugin

  • Maven compiler plugin is associated with the Default lifecycle.
  • You can specify the specific java version it should use to compile your code.
  • This plugin is associated with 2 goals, mvn compiler:compile and mvn compiler:testCompile.
  • The below XML is from the effective pom, you can override the default values as and when needed.
...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
...

3. Maven Resources Plugin

  • Maven resources plugin is also associated with the Default build lifecycle.
  • Associated with 3 goals, mvn resources:resources, mvn resources:testResources and mvn resources:copy-resources.
  • This plugin copies the project resources to the /target/ directory.
  • This plugin can be configured to copy specific files to a specific directory.

In the effective pom, it looks as below.

...
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...

4. Maven Surefire Plugin

  • This plugin is also associated with the Default lifecycle.
  • It has one goal, mvn surefire:test.
  • This plugin is used to run the unit test cases of the projects.
  • It can run JUnit, TestNG, Cucumber etc test cases.
  • It basically runs the classes named like **/Test*.java, **/*Test.java, **/*Tests.java, **/*TestCase.java.
...
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...

5. Maven jar plugin

  • Associated with the Default lifecycle.
  • It has 2 goals, mvn jar:jar and mvn jar:test-jar.
  • This plugin builds jars from the compiled artifacts and project resources.
  • It can be configured for creating executable jars.

The default configuration looks as below. I have also discussed a few more essential maven plugins after that.

...
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...

6. Maven deploy plugin

  • Associated with the Default build Lifecycle.
  • It has 2 goals, mvn deploy:deploy and mvn deploy:deploy-file.
  • This plugin mainly deploys the project artifacts to remote Maven repositories.
  • This configuration is often customized to release the project artifacts in internal maven repositories.

The default maven-deploy-plugin configuration looks as below.

...
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
...

7. Maven site plugin

  • Associated with the Site Build lifecycle.
  • This plugin generates project documentation, detail usage is discussed in the Maven Site article.
  • It has 7 goals as below.
    mvn site:site – Generates documentation site for Project,
    mvn site:deploy – Deploys the doc site,
    mvn site:run – Runs the generated doc in the local jetty,
    mvn site:stage – Generates the doc in a stagging directory,
    mvn site:stage-deploy – Deploys the site to remote stage directory,
    mvn site:attach-descriptor – Adds site.xml for Search Engine Optimization,
    mvn site:jar – Bundles generated site into a jar that is deployed to a repository.
    mvn site:effective-site – Generates site.xml file.

8. Maven war plugin

  • This plugin has 3 goals, mvn war:war – Default goal to package the projects to war file,
    mvn war:exploded – Used to create an exploded webapp in a specified directory to speed up testing and
    mvn war:inplace – Creates webapp in src/main/webapp.
  • 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.
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

9. Maven Jetty Plugin

  • Useful for rapid development and testing your web-app by deploying it in the Jetty server.
  • You need to add it as a dependency and then run mvn jetty:run to start the jetty server.
  • For any help, run mvn jetty:help.
  • Detail information is available on the Official Jetty site.
...
<plugins>
  <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.4.21.v20190926</version>
  </plugin>
...

The essential Maven plugins list is quite subjective to your project requirement. Say if you are building a web application from scratch, then it makes sense to explore and see what suites the best. On the other hand, if you are using Spring framework or anything similar, the guideline is usually dictated by the framework. You will just need to follow the framework guide and understand the plugins that the framework recommends.

By |Last Updated: October 21st, 2019|Categories: Java™, Maven|