Build Lifecycle Management
About
Build Lifecycle Management Plugins control the different phases of the Maven build lifecycle—such as compilation, testing, packaging, deployment, and installation. These plugins ensure that Maven can consistently build, test, and distribute projects by handling specific tasks at different lifecycle stages.
Maven defines three default lifecycles:
Clean – Removes previous build artifacts.
Default – Handles compilation, testing, packaging, and deployment.
Site – Generates project documentation.
Each lifecycle consists of phases, and various plugins execute specific goals within these phases.
How Plugins Fit into the Maven Build Lifecycle?
Each plugin executes specific goals, and these goals are bound to phases in the Maven build lifecycle.
For example:
compile
→ maven-compiler-plugin (compile
)test
→ maven-surefire-plugin (test
)package
→ maven-jar-plugin (jar
)verify
→ maven-failsafe-plugin (integration-test
)install
→ maven-install-plugin (install
)deploy
→ maven-deploy-plugin (deploy
)
1. Compilation & Code Processing Plugins
maven-compiler-plugin
maven-compiler-plugin
The maven-compiler-plugin
is responsible for compiling Java source files. It ensures that your project's source code is compiled using the correct Java version and compiler settings.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>-Xlint:deprecation</arg>
</compilerArgs>
<skip>false</skip>
</configuration>
</plugin>
This configuration compiles Java code using Java 17.
Configuration Options
source
Specifies the Java source version
1.6
target
Specifies the Java target bytecode version
1.6
compilerArgs
Additional compiler arguments
None
debug
Enables debug symbols
true
failOnError
Stops the build if compilation fails
true
verbose
Prints detailed compiler output
false
skip
Skips compilation phase
None
Compiler Arguments -Xlint:unchecked
enable warnings for unchecked operations.
None
Compiler Arguments -Xlint:deprecation
enable warnings for deprecated operations.
None
maven-resources-plugin
maven-resources-plugin
The maven-resources-plugin
is responsible for copying project resources (such as .properties
, .xml
, and .json
files) into the appropriate build directories.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
// Different configuration use case
// Copy Specific Files - This only copies config.properties instead of all resources.
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>config.properties</include>
</includes>
</resource>
</resources>
</configuration>
// Enable Variable Substitution in Resource
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
This plugin executes in the process-resources
phase to copy files from src/main/resources
to target/classes
.
Configuration Options
resources
Specifies the directories for resource files
src/main/resources
includes
Specifies which files to include
**/*
excludes
Specifies which files to exclude
None
filtering
Allows variables in resources (${}
placeholders)
false
maven-enforcer-plugin
maven-enforcer-plugin
The maven-enforcer-plugin
ensures that projects adhere to rules, such as enforcing Java versions, dependency versions, and best practices.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[17,)</version> // This enforces Java 17 or higher.
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
// Different configuration use case
// Enforce a Specific Maven Version
<rules>
<requireMavenVersion>
<version>[3.6,)</version>
</requireMavenVersion>
</rules>
// Ban Specific Dependencies
<rules>
<bannedDependencies>
<excludes>
<exclude>org.slf4j:slf4j-log4j12</exclude>
</excludes>
</bannedDependencies>
</rules>
Configuration Options
requireJavaVersion
Ensures a minimum Java version
requireMavenVersion
Ensures a minimum Maven version
requireOS
Restricts build execution to specific OS
bannedDependencies
Blocks unwanted dependencies
bannedPlugins
Blocks unwanted plugins
2. Testing Plugins
maven-surefire-plugin
maven-surefire-plugin
The maven-surefire-plugin
runs unit tests during the test
phase of the Maven lifecycle. It executes JUnit, TestNG, and other testing frameworks.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
// Different configuration use case
// Running a Specific Test Class
<configuration>
<test>com.example.MyTest</test>
</configuration>
// Running Tests in Parallel
<configuration>
<forkCount>2</forkCount>
<parallel>methods</parallel>
</configuration>
// Skipping Tests
<configuration>
<skipTests>true</skipTests>
</configuration>
By default, it runs tests from src/test/java
.
Configuration Options
includes
Specify which test classes to run
**/Test*.java
excludes
Specify which tests to exclude
None
test
Runs a specific test class
None
forkCount
Number of JVMs to run tests in parallel
1
parallel
Enables parallel execution (methods
, classes
, both
)
None
skipTests
Skips all tests
false
failIfNoTests
Fails the build if no tests are found
true
maven-failsafe-plugin
maven-failsafe-plugin
The maven-failsafe-plugin
is designed for integration tests (ITs) and runs tests after the application is built and deployed.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
// Different configuration use case
// Running Only Integration Tests. This runs only test files ending with IT.java (e.g., MyServiceIT.java)
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
// Allow Integration Test Failures Without Stopping the Build
<configuration>
<errorFailingTests>false</errorFailingTests>
</configuration>
Failsafe ensures that even if an integration test fails, Maven doesn't stop the build immediately but allows post-build cleanup.
Configuration Options
includes
Specify test classes to run
**/IT*.java
excludes
Specify test classes to exclude
None
errorFailingTests
Set to false
to allow failures without stopping the build
true
skipITs
Skip integration tests
false
parallel
Run tests in parallel
None
maven-verifier-plugin
maven-verifier-plugin
The maven-verifier-plugin
is useful for functional testing of Maven projects. It verifies that the output of a Maven build meets the expected criteria.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-verifier-plugin</artifactId>
<version>1.1</version>
</plugin>
// Different configuration use case
// Verify Expected Build Output. This verifies that target/output.txt contains SUCCESS.
<configuration>
<expectedFile>target/output.txt</expectedFile>
<expectedContent>SUCCESS</expectedContent>
</configuration>
3. Packaging Plugins
maven-jar-plugin
maven-jar-plugin
The maven-jar-plugin
is used to package Java applications into a JAR (Java ARchive) file. It includes compiled .class
files, META-INF
, and resource files.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
// Different configuration use case
// Custom JAR Name. This generates my-custom-app.jar instead of the default name.
<configuration>
<finalName>my-custom-app</finalName>
</configuration>
// Creating a Sources JAR. This generates artifactId-version-sources.jar containing only source code.
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>sources</classifier>
</configuration>
</execution>
Configuration Options
finalName
Name of the generated JAR file
project.artifactId-version.jar
classifier
Adds a suffix (e.g., -sources
)
None
excludes
Files to exclude
None
includes
Files to include
None
maven-war-plugin
maven-war-plugin
The maven-war-plugin
is used to package web applications into a WAR (Web Application Archive) for deployment on Java web servers.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
// Different configuration use case
// Creating a Custom WAR Name. This generates my-app.war instead of the default name.
<configuration>
<warName>my-app</warName>
</configuration>
// Skipping Web XML Validation
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
Configuration Options
failOnMissingWebXml
Whether to fail if web.xml
is missing
true
warName
Name of the WAR file
project.artifactId-version.war
outputDirectory
Where to place the WAR file
target/
maven-ear-plugin
maven-ear-plugin
The maven-ear-plugin
is used to create EAR (Enterprise Archive) files, which bundle multiple Java EE components (JARs and WARs).
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>3.2.0</version>
</plugin>
// Different configuration use case
// Skipping application.xml. This allows EAR packaging without application.xml.
<configuration>
<generateApplicationXml>false</generateApplicationXml>
</configuration>
Configuration Options
defaultJavaBundleDir
Directory for JAR modules
lib/
failOnMissingApplicationXml
Fail if application.xml
is missing
true
generateApplicationXml
Generate application.xml
automatically
true
maven-assembly-plugin
maven-assembly-plugin
The maven-assembly-plugin
allows custom packaging formats such as ZIP, TAR, TAR.GZ, and self-contained archives.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
</plugin>
// Packaging JAR with Dependencies. This packages the JAR with all dependencies included.
<execution>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRef>jar-with-dependencies</descriptorRef>
</configuration>
</execution>
// Creating a ZIP Archive. This creates a ZIP archive of the project.
<execution>
<goals>
<goal>single</goal>
</goals>
<configuration>
<formats>
<format>zip</format>
</formats>
</configuration>
</execution>
Configuration Options
descriptorRef
Predefined package types (jar-with-dependencies
, etc.)
None
finalName
Name of the output archive
project.artifactId-version
formats
Output formats (zip
, tar.gz
, etc.)
zip
maven-shade-plugin
maven-shade-plugin
The maven-shade-plugin
is used to create a fat JAR (Uber JAR) that includes all dependencies.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
</plugin>
// Creating a Fat JAR. This creates fat-jar.jar with all dependencies included.
<configuration>
<finalName>fat-jar</finalName>
</configuration>
Configuration Options
finalName
Name of the output JAR
project.artifactId-version.jar
shadedArtifactAttached
Attach shaded JAR to build
false
4. Deployment & Installation Plugins
maven-install-plugin
maven-install-plugin
The maven-install-plugin
is used to install project artifacts (JAR, WAR, EAR, etc.) into the local repository (~/.m2/repository
). This allows other projects on the same system to reference them.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
</plugin>
// Installing a Custom Artifact. This installs the artifact using a legacy repository layout.
<execution>
<goals>
<goal>install</goal>
</goals>
<configuration>
<repositoryLayout>legacy</repositoryLayout>
</configuration>
</execution>
// Skipping Installation in a Profile. To activate this profile, run: mvn clean package -DskipInstall=true
<profiles>
<profile>
<id>skip-install</id>
<activation>
<property>
<name>skipInstall</name>
<value>true</value>
</property>
</activation>
<properties>
<install.skip>true</install.skip>
</properties>
</profile>
</profiles>
Configuration Options
skip
Skip the installation step
false
repositoryLayout
Define repository structure
default
maven-deploy-plugin
maven-deploy-plugin
The maven-deploy-plugin
is used to deploy project artifacts to a remote repository (e.g., Nexus, Artifactory, or Maven Central) for sharing across teams or organizations.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
</plugin>
// Deploying to a Remote Repository. This deploys artifacts to https://repo.mycompany.com/releases.
<distributionManagement>
<repository>
<id>my-repo</id>
<url>https://repo.mycompany.com/releases</url>
</repository>
</distributionManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
</plugin>
Configuration Options
url
The repository URL for deployment
None
(Must be provided)
repositoryId
The ID of the target repository
None
altDeploymentRepository
Alternative repository specification
None
wagon-maven-plugin
wagon-maven-plugin
The wagon-maven-plugin
is a Maven plugin provided by the Codehaus Mojo project that enables automated file transfers as part of a Maven build lifecycle. It leverages the Maven Wagon transport framework to upload or download files using various protocols such as SCP, FTP, HTTP, and more.
This plugin is ideal for:
Deploying build artifacts to non-Maven servers or directories
Automating backup, distribution, or deployment steps
Transferring files as part of custom CI/CD pipeline
Features
Supports standard Maven Wagon protocols (e.g.,
scp
,ftp
,http
,file
)Can be bound to any Maven phase (like
deploy
orinstall
)Simple integration using server credentials from
settings.xml
Fine-grained control over what files are transferred (via
includes
,excludes
,fromDir
, etc.)
Syntax
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>2.0.0</version>
</plugin>
// Deploying to an FTP Server. This uploads my-app.jar to an FTP server.
<execution>
<goals>
<goal>upload</goal>
</goals>
<configuration>
<url>ftp://ftp.example.com/deploy/</url>
<fromFile>target/my-app.jar</fromFile>
<toFile>my-app.jar</toFile>
</configuration>
</execution>
Configuration Options
url
Remote location URL
Required
fromFile
Local file to upload
Required
toFile
Remote destination file
Required
maven-scm-plugin
maven-scm-plugin
The maven-scm-plugin
provides integration with Git, SVN, Mercurial, CVS, and other SCM tools for tagging and releasing.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.2</version>
</plugin>
// Tagging a Release in Git. This creates a Git tag (v1.0.0) for the current project.
mvn scm:tag -Dtag=v1.0.0
Configuration Options
connectionUrl
SCM repository URL
Required
developerConnectionUrl
SCM developer URL
Required
tagBase
Base tag URL
Optional
maven-release-plugin
maven-release-plugin
The maven-release-plugin
automates the release process, including versioning, tagging, and deployment.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M8</version>
</plugin>
Configuration Options
tagNameFormat
Format for tags
project.artifactId-version
releaseProfiles
Additional Maven profiles to use
None
5. Clean & Site Plugins
maven-clean-plugin
The maven-clean-plugin
is responsible for removing the target/
directory created during the build. This ensures that stale or obsolete files don’t affect the next build process.
Deletes all files generated by the previous build in the
target/
directory.
mvn clean
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.1</version>
</plugin>
// Clean Additional Directory
<configuration>
<filesets>
<fileset>
<directory>${project.basedir}/logs</directory>
</fileset>
</filesets>
</configuration>
Configuration Options
filesets
Allows you to specify additional files/folders to delete
None
excludeDefaultDirectories
If true
, does not delete the default target/
dir
false
maven-site-plugin
maven-site-plugin
The maven-site-plugin
is used to generate a complete website for your project using metadata from the pom.xml
and additional documentation resources (Markdown, APT, etc.).
Generates HTML pages for project reports, Javadoc, changelogs, dependency reports, etc.
Use
mvn site
This creates the site in target/site/index.html
.
Syntax
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M9</version>
</plugin>
// Customize Site Output Directory
<configuration>
<outputDirectory>${project.build.directory}/custom-site</outputDirectory>
</configuration>
Configuration Options
reportPlugins
Specifies which reporting plugins to include
Default reports
outputDirectory
Directory where site files will be generated
target/site
inputEncoding
File encoding
UTF-8
Last updated
Was this helpful?