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
| Phase | What it does |
|---|---|
validate | check the project is well-formed |
compile | compile src/main/java |
test | run unit tests (Surefire) |
package | build the jar or war |
verify | run integration tests and checks (Failsafe) |
install | copy to the local ~/.m2 repository |
deploy | publish 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
-DskipTestshabitually: The build stops telling you anything useful.mvn cleanon every build: Throws away incremental compilation for no reason. Clean when something is genuinely stale.- Integration tests in Surefire: A ten-minute unit test phase means people stop running it.
- Unpinned plugin versions: The build changes behaviour when a plugin releases. Pin them.
- Confusing
installanddeploy:installis local only;deploypublishes to a shared repository.
Best Practices
- Name tests by speed:
*Testfast and always,*ITslow 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
- The Phase Trace: Run
mvn packageon a project and list, from the output, every phase that executed before packaging. - The Split: Configure Surefire to run
*Testand Failsafe to run*IT, and confirmmvn testskips the integration tests. - The Custom Task: Write a Gradle task that fails the build if a required environment variable is missing.