Careeroza
Maven Lifecycle and Wrapper
The Default Lifecycle
Maven runs phases in a fixed order. Running a phase runs every phase before it too.
validate -> compile -> test -> package -> verify -> install -> deploy
| | | | | |
check pom javac src run JUnit build jar copy to upload to a
target/*.jar ~/.m2 repo remote repo| Command | What it does |
|---|---|
mvn clean | delete the target/ folder |
mvn compile | compile main code |
mvn test | compile + run unit tests |
mvn package | compile + test + build target/shopease-0.0.1-SNAPSHOT.jar |
mvn spring-boot:run | run the app directly (plugin goal, not a phase) |
mvn package -DskipTests | build without running tests (use carefully) |
Phases Run in Order
Example
Simulating Maven: asking for package runs every earlier phase first.
import java.util.List;
public class Main {
static final List<String> LIFECYCLE =
List.of("validate", "compile", "test", "package", "verify", "install", "deploy");
static void mvn(String phase) {
System.out.println("$ mvn " + phase);
for (String p : LIFECYCLE) {
System.out.println(" [INFO] --- " + p + " ---");
if (p.equals(phase)) break;
}
System.out.println(" [INFO] BUILD SUCCESS");
}
public static void main(String[] args) {
mvn("compile");
mvn("package");
}
}Output
$ mvn compile [INFO] --- validate --- [INFO] --- compile --- [INFO] BUILD SUCCESS $ mvn package [INFO] --- validate --- [INFO] --- compile --- [INFO] --- test --- [INFO] --- package --- [INFO] BUILD SUCCESS
The Maven Wrapper (mvnw)
Projects from Spring Initializr include mvnw (Linux/Mac), mvnw.cmd (Windows) and a .mvn/ folder. The wrapper downloads the exact Maven version the project needs, so nobody has to install Maven and everyone (including CI servers) uses the same version.
Example
Build the jar with the wrapper (on Windows use mvnw.cmd or .\mvnw):
./mvnw clean packageExample output
[INFO] Scanning for projects... [INFO] ---------------------< com.shopease:shopease >---------------------- [INFO] Building shopease 0.0.1-SNAPSHOT [INFO] --- maven-compiler-plugin:3.14.0:compile (default-compile) @ shopease --- [INFO] --- maven-surefire-plugin:3.5.4:test (default-test) @ shopease --- [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] --- maven-jar-plugin:3.4.2:jar (default-jar) @ shopease --- [INFO] --- spring-boot-maven-plugin:3.5.6:repackage (repackage) @ shopease --- [INFO] BUILD SUCCESS [INFO] Total time: 6.412 s
Tip: Commit mvnw, mvnw.cmd and .mvn/ to Git. Never commit target/ (add it to .gitignore).