In the last couple of years, Maven has become the de-facto build tool for Java applications. Though there are challenges that exist from tools like Gradle, 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 tasks like 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 they do will help you to understand how Maven works in general and let you better control your build process.
Btw, if you are just starting with Maven, I suggest you to first go through a comprehensive Maven course like Apache Maven: Beginner to Guru to learn some fundamentals. It will not only help you to build and deploy your Java and Spring Boot projects using Maven but also learn Maven itself in depth.
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.
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 that you are most likely to encounter and use 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 the Maven environment like how your project gets compiled or how unit tests are run in the Maven build environment.
This knowledge also helps you to customize a particular phase of the Maven lifecycle to suit your project's needs.
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:
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 they do will help you to understand how Maven works in general and let you better control your build process.
Btw, if you are just starting with Maven, I suggest you to first go through a comprehensive Maven course like Apache Maven: Beginner to Guru to learn some fundamentals. It will not only help you to build and deploy your Java and Spring Boot projects using Maven but also learn Maven itself in depth.
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 the pom.xml file. For example, in the following pom.xml file we are telling maven-compiler-plugin to expect Java 7 source, and output Java 7 specific classes.<build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>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 the 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 that allows you to create your project distribution in zip or tar format, suitable for deployment in the 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 an embedded Jetty Servlet container to run your Java web application.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 the 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 that 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.17. 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 applications as they are deployed into Servlet containers like Tomcat or enterprise Server as WAR files.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 the deploy plugin is 2.8.2 released in August 2014. See Maven Essentials to learn more about key maven concepts like local and remote repositories.9. maven-resource-plugin
This Maven resource plugin is responsible for the 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 applications using the 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 that you are most likely to encounter and use 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 the Maven environment like how your project gets compiled or how unit tests are run in the Maven build environment.
This knowledge also helps you to customize a particular phase of the Maven lifecycle to suit your project's needs.
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 the difference between Maven, ANT, and Jenkins? (answer)
- How to increase the heap size of Maven? (steps)
- How to fix the 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.xml in ANT? (tutorial)
How can you forget shade plugin in the top 10?
ReplyDeleteFor me, the killer maven plugin, is the release plugin. It has brought sense to chaos and saved my employers millions.
ReplyDeleteOne 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
ReplyDelete