Maven Plugins
Learn how to use Maven plugins like the Compiler Plugin, Surefire Plugin, and Shade Plugin. Configure Maven to compile code, run tests, and create fat JARs in Java projects.
Apache Maven is powerful on its own, but its real flexibility comes from plugins.
Plugins allow Maven to perform specific tasks such as compiling code, running tests, creating JAR files, or even building an entire deployment package.
In this lesson, we’ll focus on three essential plugins every Java developer should know:
- Maven Compiler Plugin (compiles your Java code)
- Maven Surefire Plugin (runs unit tests)
- Maven Shade Plugin (creates executable fat JARs)
1. Maven Compiler Plugin
By default, Maven may not always use the correct Java version to compile your project.
The Maven Compiler Plugin lets you configure the Java source and target versions.
Example: Compiler Plugin in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
✅ Ensures your code compiles with the correct Java version (e.g., Java 17).
✅ Prevents UnsupportedClassVersionError
during runtime.
2. Maven Surefire Plugin
The Surefire Plugin is responsible for running unit tests in Maven.
It works with popular testing frameworks such as JUnit and TestNG.
Example: Surefire Plugin Configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
✅ Automatically detects and runs test classes like MyServiceTest.java
.
✅ Generates reports in the target/surefire-reports/
folder.
✅ Can skip or include specific test patterns.
Run tests with:
mvn test
3. Maven Shade Plugin (Fat JARs)
When building Java applications, you often need to package dependencies inside a single JAR.
The Maven Shade Plugin creates an uber/fat JAR, which includes your compiled code plus all dependencies.
Example: Shade Plugin Configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After running:
mvn clean package
You’ll find a fat JAR (e.g., my-app-1.0-SNAPSHOT-shaded.jar
) inside the target/
folder, which you can run directly:
java -jar target/my-app-1.0-SNAPSHOT-shaded.jar
✅ Perfect for standalone applications.
✅ Removes the need to ship separate JARs for dependencies.
Key Takeaways
- Compiler Plugin → Controls the Java version for compiling source code.
- Surefire Plugin → Runs tests and generates reports.
- Shade Plugin → Packages all dependencies into a single executable fat JAR.
Together, these plugins cover the build, test, and packaging lifecycle for most Maven-based Java applications.