Maven Plugins for Java Programmers
Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.
Maven holding a specified position in build tool for Java applications.Although there is Gradle.
Maven makes dependency management for us but. There are plenty of plugins that let you customize your build-up to the level you can do with ANT.
Thease plugins help Maven to automate most of the build related task e.g. compilation, packaging, deployment, testing, deployment, and committing the source code into the remote source repository like GitHub or Bitbucket.
The developers, use maven on daily for project development but we should know their plugins to use these plugins effectively.
1). spring-boot-maven-plugin
This is one of the useful plugins to develop Java web applications using Spring Boot.
These plugins collect all jar files on the classpath and make a single jar which is ready to execute. It automatically search the main method to mark as a runnable class.
To use the Spring Boot Maven Plugin, you have to include the appropriate XML in the plugins section of your pom.xml, as given below.
<pre class="code-pre"><span style="font-size: 14px;"> <code class="codeblock">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<!--...-->
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
<version>2.3.0.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
Also, you need to add the appropriate plugin repository elements, as shown in the following listing:
<pluginrepositories>
<pluginrepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginrepository>
<pluginrepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginrepository>
</pluginrepositories>
2). 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(WAR) file from the compiled project classes, resources, and web.xml.
We need this plugin while building Java web application as they are deployed inside the Servlet container like Tomcat, GlassFish,Jetty, TomEE, etc.
It has the following goals
-
war: war
This is the default goal invoked during the package phase for projects with a packaging type of war. It builds a WAR file. -
war: exploded
This is generally used to speed up testing during the development phase by creating an exploded webapp in a specified directory. -
war:inplace
This is another variation of war: explode where the web app is instead generated in the web application source directory, which is src/main/webapp by default.
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<!--...-->
<build>
<sourcedirectory>src</sourcedirectory>
<plugins>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<version>3.2.0</version>
<configuration>
<warsourcedirectory>WebContent</warsourcedirectory>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>
3). maven-jar-plugin
This plugin provides the capability to build jars Java Archive (JAR) file from the compiled project classes and resources.
We frequently needs these plugins to create JAR files.It has the following goals
-
jar: jar
create a jar file for your project classes inclusive resources. -
jar:test-jar
create a jar file for your project test classes.
We need to add this plugin in pom.xml as,
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<!--...-->
<build>
<sourcedirectory>src</sourcedirectory>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-jar-plugin</artifactid>
<version>3.2.0</version>
<configuration>
<archive>
<manifestfile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestfile>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
</project>
4). maven-assembly-plugin
The Assembly Plugin for Maven builds distribution "assemblies" using one of the prefabricated assembly descriptors. It handles many common operations, such as packaging a project's artifact along with dependencies, modules, site documentation, and other files. into a single zip archive.
Currently, it can create distributions in the following formats: zip tar tar.gz (or tgz) tar.bz2 (or tbz2) tar.snappy tar.xz (or txz) jar dir war
To use the Assembly Plugin in Maven, We need it.
- choose or write the assembly descriptor to use,
- configure the Assembly Plugin in your project's pom.xml, and
- run "mvn assembly: single" on your project.
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-assembly-plugin</artifactid>
<version>3.3.0</version>
</plugin>
5). maven-surefire-plugin
The Maven Surefire plugin is the plugin used for running unit tests.
The Surefire Plugin has only one goal
surefire: test
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<version>3.0.0-M4</version>
<dependencies>
<dependency>
<groupid>org.apache.maven.surefire</groupid>
<artifactid>surefire-junit47</artifactid>
<version>3.0.0-M4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
6). maven-compiler-plugin
The Compiler Plugin is used to compile the sources of your project. 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.
We can customize this plugin to compile java code for java 7 or java 8
For that, we have to do like. for java 8
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
and for java 7
<properties>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
</properties>
It has the following goals
-
compiler: compile
This is used during the compile phase and is used to compile the main source files. -
compiler:testCompile
This is used during the test-compile phase and is used to compile the test source files
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version>3.6.1</version>
<configuration>
<source></source>1.8
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
7). maven-jetty-plugin
This is the favorite plugin among the java developers. As it makes developers life much easier. For Java web project we need to create a jar file and deploy to Tomcat or Other servlet containers.But This task is done by the jetty plugin itself.
We need to just run
mvn jetty: runIt creates a war file for us and deploys it inside an embedded Jetty Servlet container.
<plugin>
<groupid>org.eclipse.jetty</groupid>
<artifactid>jetty-maven-plugin</artifactid>
<version>9.4.29.v20200521</version>
</plugin>
It has a number of goals jetty:run jetty:run-war jetty:run-exploded jetty:deploy-war jetty:run-forked jetty:run-distro jetty:start jetty:stop jetty:effective-web-xml
Please follow this tutorials about maven jetty Plugin.
8). 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.
It is used to debug or understand a POM and how you get some dependency (transitively).
It has a number of goals
-
dependency: analyze
It analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared. -
dependency:build-classpath
It 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 -
dependency:list
This alias for resolve that lists the dependencies for this project. -
dependency:list-repositories
This displays all project dependencies and then lists the repositories used. -
dependency:properties
This set a property for each project dependency containing the to the artifact on the file system. dependency:purge-local-repository tells Maven to clear dependency artifact files out of the local repository, and optionally re-resolve them. -
dependency:resolve
This tells Maven to resolve all dependencies and displays the version. JAVA 9 NOTE: will display the module name when running with Java9. -
dependency:resolve-plugins
This tells Maven to resolve plugins and their dependencies. -
dependency:sources
This tells Maven to resolve all dependencies and their source attachments, and displays the version. -
dependency:tree
It displays the dependency tree for this project. -
dependency:unpack
This is like copy but unpacks. -
dependency:unpack-dependencies
This is like copy-dependencies but unpacks.
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-dependency-plugin</artifactid>
<version>3.1.2</version>
</plugin>
9). maven-deploy-plugin
The deploy plugin is mainly used during the deployment phase, to add your artifact(s) to a remote repository for sharing with other developers and projects. This is usually done in an integration or release environment.
It can also be used to deploy a particular artifact (e.g. a third party jar.
As a repository contains more than the artifacts (POMs, the metadata, MD5, and SHA1 hash files...), deploying means not only copying the artifacts but making sure all this information is correctly updated. It's the responsibility of the deploy plugin.
To work, the deployment will require:
- Information about the repository: its location, the transport method used to access it (FTP, SCP, SFTP...) and the optional user specific required account information
- Information about the artifact(s): the group, artifact, version, packaging, classifier.
- A deployer: a method to actually perform the deployment. This can be implemented as a wagon transport (making it cross-platform), or use a system specific method.
It has the following goals
-
deploy: deploy
This is used to automatically install the artifact, its pom and the attached artifacts produced by a particular project. Most if not all of the information related to the deployment is stored in the project's pom. -
deploy:deploy-file
This is used to install a single artifact along with its pom. In that case the artifact information can be taken from an optionally specified pomFile, but can be completed/overriden using the command line.
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-deploy-plugin</artifactid>
<version>3.0.0-M1</version>
</plugin>
10). maven-resource-plugin
This Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: the main resources and test resources.
The difference is that the main resources are the resources associated with the main source code while the test resources are associated with the test source code.
Thus, this allows the separation of resources for the main source code and its unit tests.
It has the following goals
-
resources: resources
It copies the resources for the main source code to the main output directory. -
resources:testResources
It copies the resources for the test source code to the test output directory. -
resources:copy-resources
It copies resources to an output directory.
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-resources-plugin</artifactid>
<version>3.1.0</version>
</plugin>
That's it. In this article, we have seen the most used Maven Plugins for Java Programmers.
0 Comments
Post a Comment