> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Maven Plugins
- URL: https://www.javahandbook.com/maven-course/maven-plugins/
- Published: 2025-08-17T06:55:45.000Z
- Updated: 2025-08-17T06:55:45.000Z
- Description: 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.
- Author: Gopi Gorantala
- Tags: Maven Project, #docs, #maven-course

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`**

```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**

```xml
<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:

```bash
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**

```xml
<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:

```bash
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
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.