Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 15 / Topic 4 of 7

Running Maven for real

mvn itself is installed and pinned on this machine:

$ mvn --version
Apache Maven 3.9.16 (2bdd9fddda4b155ebf8000e807eb73fd829a51d5)
Maven home: /opt/homebrew/Cellar/maven/3.9.16/libexec
Java version: 21.0.11, vendor: Amazon.com Inc., runtime: /Library/Java/JavaVirtualMachines/amazon-corretto-21.jdk/Contents/Home

The first time mvn compile/mvn package runs for a brand-new project, Maven has to download the plugins that do the actual work (maven-compiler-plugin, maven-jar-plugin, maven-surefire-plugin) and any declared dependency (here, junit-jupiter) from a remote repository — they are not bundled with the mvn installation itself. Expect that first run to take noticeably longer than every run after it; once those jars sit in your local repository (~/.m2/repository), later builds reuse them and run fast, fully offline.

$ cd examples/maven-project
$ mvn -B package
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.acme.billsplit.BillCalculatorTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 s -- in com.acme.billsplit.BillCalculatorTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- jar:3.4.2:jar (default-jar) @ bill-splitter ---
[INFO] Building jar: .../examples/maven-project/target/bill-splitter-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
$ java -jar target/bill-splitter-1.0.0.jar 2400 4 10
Bill: Rs 2400.00
Tip (10.0%): Rs 240.00
Grand total: Rs 2640.00
Split 4 ways: Rs 660.00 per person
$ unzip -p target/bill-splitter-1.0.0.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.4.2
Build-Jdk-Spec: 21
Main-Class: com.acme.billsplit.BillSplitter

mvn package ran validate → compile → test → package (section 3's lifecycle) in one command — including BillCalculatorTest (section 3's src/test/java), which had to pass before Maven would even attempt the package phase. The jar it produces has the same Main-Class manifest entry section 2's hand-built jar has — maven-jar-plugin's <mainClass> configuration writes it for you — so java -jar works identically either way; only the Created-By line in the manifest gives away which tool actually built it.

ℹ️ If you only want to confirm the tests, without a fresh jar: mvn test stops one phase earlier than package (section 3's lifecycle table) — useful while you're still iterating on the code itself.

Full project: examples/maven-project/.

Done here? The practice set for Part 15 — including the solutions — is on the part page.

Search the guide