Tuesday, August 2, 2016

Top 10 Maven Plugins Every Java Developer Should Know

In the last couple of years, Maven has become the de-facto build tool for Java applications. Though there are challenges exists from tools like Gradle, but I think the dominance of Maven will help it to win the final battle. When I started with Maven, I was happy with just dependency management but then I come to know about maven plugins which let you customize your build up to the level you can do with ANT. These plugins provide true power to Maven to automate most of the build related task e.g. compilation, testing, packaging, deployment, and even committing the source code into the remote source repository. Many Java developers who use Maven daily are usually not aware of these essential Maven plugins mostly because  Maven just works, or someone has already done the hard work for them.

Maven uses conventions over configuration which means even if you don't know much about Maven you can use it to build your project and manage dependency. In order to bridge that gap, I am going to share 10 essential Maven plugins for Java developers. Knowing these plugins and what the do will help you to understand how Maven works in general and let you better control your build process.

10 Essential Maven Plugins for Java Developers

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
This is the most important maven plugin. You almost always use it unknowingly. This plugin compiles your Java code from the standard location Maven specifies e.g. /src/main/java and /src/main/resources. You can customize the default behavior of maven-compiler-plugin by specifying instructions in pom.xml file. For example, in following pom.xml file we are telling maven-compiler-plugin to expect Java 7 source, and output Java 7 specific classes.

You can also see Maven: The Definitive Guide to learn more about this plugin. 

2. maven-surefire-plugin
The Maven Surefire plugin is the default plugin for running unit tests. You can also customize the behavior of this plugin by specifying configuration in pom.xml. The Surefire Plugin has only one goal: surefire:test runs the unit tests of an application.

3. maven-assembly-plugin
The Maven Assembly plugin is another useful maven plugin which allows you to create your project distribution in zip or tar format, suitable for deployment in Linux environment. We used to do this for our project, where tar file contains all config, binary and resources together. Though it's not a good idea to release binary and config together, sometimes it makes a lot of sense in smaller projects. Btw, all goals except assembly:single have been deprecated of maven-assembly-plugin.

4. maven-jetty-plugin
The maven jetty plugin is pretty awesome for Java web developers as it makes Java web application development much easier. In order to run your Servlet JSP based project, rather than packaging and creating a WAR file and deploying it into tomcat server, you can simply run mvn jetty:run and your web application start. It uses embedded Jetty Servlet container to run your Java web application.

Essential Maven Plugins for Java Applications

5. maven-dependency-plugin
The Maven Dependency Plugin is another mandatory plugin to debug or understand a POM and how you get some dependency (transitively). This 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.

6. maven-jar-plugin
This is the plugin which creates a Java Archive (JAR) file from the compiled project classes and resources. You almost always use this plugin to create JAR files. the latest version of this plugin is 3.0.1

7. maven-war-plugin
This plugin creates a Web Archive (WAR) file from the compiled project classes, resources, and web.xml. You use this plugin while building Java web application as they are deployed into Servlet container like Tomcat or enterprise Server as WAR file.

8. 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. The current version of deploy plugin is 2.8.2 released in August 2014. See Maven Essentials to learn more about key maven concepts like local and remote repository.

Essential Maven Plugins for Java Applications

9. maven-resource-plugin
This Maven resource plugin is responsible for copying of Java projects resources to the output directory. There are two different kinds of resources in Maven projects: main resources and test resources. The difference is that the main resources are used by the main production code while the test resources are associated with the test source code, which is only used while running unit test or integration tests.

10. spring-boot-maven-plugin
The Spring Boot maven plugin is one of the useful plugins to develop Java web application using Spring Boot library. It collects all the jars on the classpath and builds a single, runnable "├╝ber-jar", which makes it more convenient to execute and transport your service. It also searches for the main() method to flag as a runnable class, and most importantly it provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies.

That's all about some of the essential Maven plugins for Java development. There are tens of Maven plugins but you don't use them always, these are the plugins which you are most likely to encounter and use them in your day to day Java development work. Knowing a little bit more about these maven plugins will not only help you to understand your pom.xml file but also how things work in Maven environment e.g. how your project gets compiled or how unit tests are run in Maven build environment. This knowledge also helps you to customize a particular phase of Maven lifecycle to suit your projects need.

Further Learning
Maven Fundamentals by Bryan Hansen
Maven Crash Course
Java Maven :101 Stop Building Java Programs the Hard Way!

Other Maven articles from Javarevisited you may like:
  • What is difference between Maven, ANT, and Jenkins? (answer)
  • How to increase heap size of Maven? (steps)
  • How to fix Maven Eclipse Dependency search not working issue? (solution)
  • How to install Maven in Windows 10? (steps)
  • How to build a Java project using ANT? (article)
  • How to create or modify build.xm in ANT? (tutorial)


Anonymous said...

How can you forget shade plugin in the top 10?

Unknown said...

For me, the killer maven plugin, is the release plugin. It has brought sense to chaos and saved my employers millions.

Allen jeley said...


Anonymous said...

One of the useful Maven plugin you can add into this list is "OWASP dependency check maven plugin". This plugin generates a report and let you know the problematical library version which are vulnerable. You can then upgrade them. Though, it can make your build slow, hence only run during night or once a day. You can read more about it on https://jeremylong.github.io/DependencyCheck/dependency-check-maven/index.html

Post a Comment