Sub topic 15
Customizing Builds with Plugins
In Maven and Gradle, almost all of the real work is done by plugins. A plugin is a package of build code that adds new abilities to your build tool. Compiling code, running tests, and making a JAR file are all done by plugins. You have already used some without noticing.
In this chapter you will learn what plugins do in each tool. You will also learn how to configure them and how to add new ones.
Why Plugins Matter
On its own, a build tool knows very little. Maven knows how to read a pom.xml and walk through lifecycle phases. Gradle knows how to read a build script and run tasks in order. Neither one knows how to compile Java until a plugin teaches it.
This design has two benefits:
- You can change behavior without changing the tool. If you need a different Java version or a custom JAR manifest, you configure a plugin.
- You can add new jobs. Plugins exist for code coverage, code formatting, Docker images, and much more.
How Plugins Work in Maven
A Maven plugin contains one or more goals. A goal is a single job, such as compiler:compile or surefire:test. Maven attaches goals to lifecycle phases. This attachment is called a binding. When you run a phase, Maven runs every goal bound to it.
Maven binds the common plugins for you, based on your project's packaging. For jar packaging, the compiler, Surefire, and JAR plugins are already bound. You only list them in pom.xml when you want to pin a version or change their settings.
Configuring a Maven Plugin
You configure a plugin inside <build><plugins>. Each plugin has coordinates, which are the same kind of groupId, artifactId, and version used for dependencies. Its settings go in a <configuration> element.
This example configures two plugins. The compiler plugin turns on all warnings. The JAR plugin writes the main class into the JAR's manifest. The manifest is a small file inside the JAR that tells Java which class to start.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>17</release>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>After mvn package, you can now start the program with -jar:
mvn package
java -jar target/hello-1.0-SNAPSHOT.jarBefore this change, you needed java -cp ... com.example.App, because the JAR did not say where to start.
Adding a New Goal with Executions
Some plugins are not bound to any phase by default. You bind them yourself with an <executions> block. This example uses the source plugin to build a second JAR that holds your .java files. It runs during the package phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>Run mvn package and look in target/. You will find hello-1.0-SNAPSHOT-sources.jar next to the normal JAR.
How Plugins Work in Gradle
A Gradle plugin adds tasks, settings, and conventions to your project. A convention is a sensible default, such as "source code lives in src/main/java." When you apply the java plugin, Gradle gains tasks like compileJava, test, and jar. The application plugin builds on java and adds the run task.
You apply plugins in the plugins { } block at the top of build.gradle.kts. There are two kinds:
| Kind | Example | Needs a version? | Where it comes from |
|---|---|---|---|
| Core plugin | `java`, `application` | No | Shipped inside Gradle |
| Community plugin | `id("com.diffplug.spotless")` | Yes | Gradle Plugin Portal |
Core plugins are tied to your Gradle version, so they need no version number. Community plugins are downloaded, so you must name a version:
plugins {
application
id("com.diffplug.spotless") version "6.25.0"
}Configuring Plugin Tasks in Gradle
In Gradle, you configure a plugin mostly by configuring the tasks it adds. The following build.gradle.kts does the same jobs as the Maven example above. It turns on compiler warnings, writes the main class into the manifest, and builds a sources JAR. It also prints each test result.
plugins {
application
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
withSourcesJar()
}
application {
mainClass = "com.example.App"
}
tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("-Xlint:all")
}
tasks.jar {
manifest {
attributes("Main-Class" to "com.example.App")
}
}
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}Build and run the JAR:
./gradlew build
java -jar build/libs/hello.jarThe JAR name comes from the project folder name. If your folder is not called hello, look in build/libs/ for the actual file name.
Here is what each part does:
java { withSourcesJar() }adds asourcesJartask and runs it duringbuild.tasks.withType<JavaCompile>().configureEachchanges every Java compile task, for both main and test code.tasks.jar { manifest { ... } }changes the manifest of the main JAR.testLoggingmakes thetesttask print each test's result.
Comparing the Two Approaches
| Question | Maven | Gradle |
|---|---|---|
| What does a plugin add? | Goals | Tasks, settings, conventions |
| How does it run? | Goals bound to phases | Tasks in the task graph |
| Where do settings go? | `<configuration>` in `pom.xml` | Kotlin blocks in `build.gradle.kts` |
| How do you add a new step? | `<executions>` with a `<phase>` | Apply a plugin or register a task |
| Version required? | Yes (always pin it) | Only for community plugins |
Both tools follow the same idea: the tool runs the steps, and plugins supply them. Maven settings are declarative, which means you describe what you want in XML. Gradle settings are written as code, so you can use logic such as withType to change many tasks at once.
Common Mistakes
- Putting a plugin in
<dependencies>. Maven plugins go in<build><plugins>, not in<dependencies>. A plugin is build code, not library code for your app. - Configuring a task before its plugin is applied. In Gradle,
tasks.jaronly exists after thejavaorapplicationplugin is applied. Keep thepluginsblock at the top. - Wrong phase in an execution. If you bind a goal to
verify, it will not run duringmvn package, becauseverifycomes afterpackage. - Missing version for a community Gradle plugin. Gradle stops with "plugin was not found" if it cannot tell which version to download.
Exercise
You run mvn package and then java -jar target/hello-1.0-SNAPSHOT.jar. Java prints "no main manifest attribute". Which plugin do you configure, and which setting do you add?
Show answer
Configure maven-jar-plugin. Inside its configuration, add archive > manifest > mainClass with the value com.example.App. Then run mvn package again so the manifest includes the Main-Class entry.
Exercise
In Maven, you add the maven-source-plugin with an execution bound to the verify phase. You run mvn package. Why is there no sources JAR in target/?
Show answer
The verify phase comes after package. Running mvn package stops at package, so goals bound to verify never run. Run mvn verify, or bind the execution to the package phase.
Exercise
Why does plugins { application } in Gradle need no version, while id("com.diffplug.spotless") needs one?
Show answer
application is a core plugin shipped inside Gradle, so its version always matches your Gradle version. Spotless is a community plugin downloaded from the Gradle Plugin Portal, so Gradle needs to know which version to fetch.
Exercise
Write a Gradle Kotlin DSL snippet that adds the -Xlint:all compiler option to every Java compile task in the project.
Show answer
tasks.withType<JavaCompile>().configureEach { options.compilerArgs.add("-Xlint:all") }