TExcelTAD
Home › Java Builds: Maven & Gradle › Plugins, Executions & Configuration

Plugins, Executions & Configuration

Maven core knows the lifecycle and how to resolve artifacts. Everything else (compiling, testing, packaging, copying, signing, generating code) is done by plugins, which are ordinary artifacts in a repository, with coordinates and versions. A build engineer's Maven work is mostly about choosing plugins, pinning their versions and binding their goals to phases.

Asking a plugin what it does

Every plugin documents itself. help:describe lists its goals, and with -Ddetail every parameter, including the user property that sets it from the command line or a <properties> entry:

bash · try it yourself
mkdir -p p && cd p
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>p</artifactId>
  <version>1.0</version>
</project>
POM
mvn -o help:describe -Dplugin=compiler | sed -n '/^Name:/,/^This plugin/p'
mvn -o help:describe -Dplugin=compiler -Dgoal=compile -Ddetail | grep -A2 -E '^ +release
  

Name: Apache Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
  project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.15.0
Goal Prefix: compiler

This plugin has 3 goals:
    release
      User property: maven.compiler.release
      The -release argument for the Java compiler, supported since Java9 See

So <maven.compiler.release>21</maven.compiler.release> in <properties> works because the compiler plugin's release parameter reads the user property maven.compiler.release. Most of the "magic" properties in POMs are exactly that.

Pin every plugin version

Without a version, Maven uses the defaults built into the Maven release you happen to run: compiler 3.15.0 with Maven 3.9.16, other versions with other releases. Upgrade Maven on a CI agent and your build silently changes. The fix is a <pluginManagement> section that pins every plugin, usually in a parent POM:

  • <build><pluginManagement><plugins> declares versions and default configuration. It does not add the plugin to the build.
  • <build><plugins> makes a plugin take part in this module's build. A plugin listed here takes its version and configuration from pluginManagement if one is there.

Executions: binding goals to phases

An execution binds one or more goals of a plugin to a phase. Its id names it; its configuration applies only to it. The session below pins the default plugins, then adds two executions: the source plugin packs a -sources.jar at package, and the dependency plugin copies runtime dependencies into target/lib:

bash · try it yourself
mkdir -p plug/src/main/java/demo && cd plug
cat > src/main/java/demo/App.java <<'SRC'
package demo;

public class App {
    public static void main(String[] args) {
        System.out.println(org.apache.commons.lang3.StringUtils.reverse("nivek"));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>plug</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.21.0</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin><artifactId>maven-clean-plugin</artifactId><version>3.5.0</version></plugin>
        <plugin><artifactId>maven-resources-plugin</artifactId><version>3.5.0</version></plugin>
        <plugin><artifactId>maven-compiler-plugin</artifactId><version>3.16.0</version></plugin>
        <plugin><artifactId>maven-surefire-plugin</artifactId><version>3.6.0</version></plugin>
        <plugin><artifactId>maven-jar-plugin</artifactId><version>3.5.1</version></plugin>
        <plugin><artifactId>maven-source-plugin</artifactId><version>3.4.0</version></plugin>
        <plugin><artifactId>maven-dependency-plugin</artifactId><version>3.11.0</version></plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals><goal>jar-no-fork</goal></goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-libs</id>
            <phase>package</phase>
            <goals><goal>copy-dependencies</goal></goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <includeScope>runtime</includeScope>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
POM
mvn clean package | grep -E -- '--- |Copying'
ls target target/lib
java -cp 'target/plug-1.0.jar:target/lib/*' demo.App
[INFO] --- clean:3.5.0:clean (default-clean) @ plug ---
[INFO] --- resources:3.5.0:resources (default-resources) @ plug ---
[INFO] --- compiler:3.16.0:compile (default-compile) @ plug ---
[INFO] --- resources:3.5.0:testResources (default-testResources) @ plug ---
[INFO] --- compiler:3.16.0:testCompile (default-testCompile) @ plug ---
[INFO] --- surefire:3.6.0:test (default-test) @ plug ---
[INFO] --- jar:3.5.1:jar (default-jar) @ plug ---
[INFO] --- source:3.4.0:jar-no-fork (attach-sources) @ plug ---
[INFO] --- dependency:3.11.0:copy-dependencies (copy-libs) @ plug ---
target:
classes
generated-sources
lib
maven-archiver
maven-status
plug-1.0-sources.jar
plug-1.0.jar

target/lib:
commons-lang3-3.21.0.jar
kevin

Read the goal headers:

  • The default plugins now run in the pinned versions (compiler:3.16.0, surefire:3.6.0).
  • source:3.4.0:jar-no-fork (attach-sources) runs at package although the execution names no phase: the goal declares a default phase (package), used when the execution doesn't set one.
  • Both new executions are bound to package and run after jar:jar. Within one phase, goals bound by the packaging come first, then executions in POM order.

Overriding and switching off a default execution

Default bindings are executions too, with ids such as default-compile, default-testCompile, default-test, default-jar. You can configure one by its id, or remove it by moving it to a phase that doesn't exist:

xml
<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <executions>
    <execution>
      <id>default-jar</id>
      <configuration>
        <archive><manifest><mainClass>demo.App</mainClass></manifest></archive>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <executions>
    <execution><id>default-test</id><phase>none</phase></execution>  <!-- tests never run -->
  </executions>
</plugin>

Configuration placed directly under <plugin> (outside <executions>) applies to all executions of that plugin, including command-line ones (default-cli); configuration inside an <execution> applies only to it.

Test yourself

What does declaring a plugin in <pluginManagement> (and nowhere else) do?

  1. Runs it in every build
  2. Sets its version and default configuration, but doesn't add it to the build
  3. Downloads it without running it
  4. Nothing at all

An execution lists a goal but no <phase>. When does it run?

  1. Never
  2. At the goal's default phase, if the goal declares one
  3. At validate
  4. Only from the command line

Why should a POM pin versions of the default plugins (compiler, surefire, jar…)?

  1. Maven refuses to run otherwise
  2. Without pins the versions depend on the Maven release running the build, so upgrading Maven changes the build
  3. Pinned plugins run faster
  4. To enable offline mode