Build Lifecycle: Phases and Plugins

Build Lifecycle: Phases and Plugins in Java: does far more than its name suggests. Maven's lifecycle is a fixed sequence of phases, and naming one runs it and…

Concept Explanation

mvn package does far more than its name suggests. Maven's lifecycle is a fixed sequence of phases, and naming one runs it and everything before it.


The Default Lifecycle

validate → compile → test → package → verify → install → deploy
PhaseWhat it does
validatecheck the project is well-formed
compilecompile src/main/java
testrun unit tests (Surefire)
packagebuild the jar or war
verifyrun integration tests and checks (Failsafe)
installcopy to the local ~/.m2 repository
deploypublish to a remote repository

So mvn package compiles and tests first. mvn install does all of that plus installs. Running mvn clean install is the common "rebuild everything from scratch" incantation.

Useful variations:

mvn clean                     # delete target/
mvn test                      # stop after unit tests
mvn package -DskipTests       # skip running tests (still compiles them)
mvn verify                    # unit + integration tests
mvn -o package                # offline: no network lookups

Plugins Do the Actual Work

Phases are just names. Plugins bind goals to phases and perform the work:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes><exclude>**/*IT.java</exclude></excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals><goal>prepare-agent</goal></goals>
                </execution>
                <execution>
                    <id>coverage-check</id>
                    <phase>verify</phase>          <!-- bound to verify -->
                    <goals><goal>check</goal></goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The convention above is worth adopting: *Test.java runs at test (fast, every build), *IT.java runs at verify (slow, before release).


Gradle's Task Graph

Gradle takes a different approach — a directed graph of tasks rather than a fixed sequence:

./gradlew build          # assemble + check
./gradlew test
./gradlew tasks          # list every available task
./gradlew build --scan   # detailed performance report
tasks.register('printVersion') {
    doLast { println "Version: ${project.version}" }
}

tasks.named('test') {
    useJUnitPlatform()
    maxParallelForks = Runtime.runtime.availableProcessors() - 1
}

Gradle's incremental builds and build cache make it substantially faster on large projects; Maven's rigidity makes it more predictable on small ones.


Profiles

Different settings per environment, without editing the POM:

<profiles>
    <profile>
        <id>ci</id>
        <properties>
            <skipITs>false</skipITs>
        </properties>
    </profile>
</profiles>
mvn verify -Pci

Common Mistakes

  1. -DskipTests habitually: The build stops telling you anything useful.
  2. mvn clean on every build: Throws away incremental compilation for no reason. Clean when something is genuinely stale.
  3. Integration tests in Surefire: A ten-minute unit test phase means people stop running it.
  4. Unpinned plugin versions: The build changes behaviour when a plugin releases. Pin them.
  5. Confusing install and deploy: install is local only; deploy publishes to a shared repository.

Best Practices

  • Name tests by speed: *Test fast and always, *IT slow and before release.
  • Pin every plugin version for reproducible builds.
  • Keep the default build fast — under a couple of minutes, or people work around it.
  • Use the wrapper so CI and every developer share one build tool version.
  • Bind quality gates to verify: coverage, formatting, and vulnerability checks belong in the build, not in a wiki.

Practice Exercises

  1. The Phase Trace: Run mvn package on a project and list, from the output, every phase that executed before packaging.
  2. The Split: Configure Surefire to run *Test and Failsafe to run *IT, and confirm mvn test skips the integration tests.
  3. The Custom Task: Write a Gradle task that fails the build if a required environment variable is missing.