Practice: Maven projects
45 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Real Maven trouble is a short list: a pom with no Java version, the wrong scope, a typo in the coordinates, a JAR without Main-Class, and a failing test read in a hurry. This lesson drills each one.
Writing a pom.xml from a project description
A project description turns into a pom in a fixed order: coordinates, packaging, properties, dependencies, build plugins. Below is the complete pom for a Java 17 app that uses Commons Text and MySQL, is tested with JUnit and runs as one JAR.
Example 1 · A complete pom from a description, and its uber JAR running
Runs in: the receipt-app Maven project
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.college</groupId> <artifactId>receipt-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>17</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>26.7.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.14.4</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.6.4</version> <configuration> <mainClass>com.college.app.Main</mainClass> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.5.1</version> <configuration> <archive> <manifest> <mainClass>com.college.app.Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.6.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.college.app.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build></project>$ ls target/*.jar target/original-receipt-app-1.0-SNAPSHOT.jar target/receipt-app-1.0-SNAPSHOT.jar $ java -jar target/receipt-app-1.0-SNAPSHOT.jar Hostel fee receipt Student: Asha Rao Total: Rs 110000 $ java -jar target/receipt-app-1.0-SNAPSHOT.jar "meena IYER" Hostel fee receipt Student: Meena Iyer Total: Rs 110000
What this code does
- Coordinates, Java 17 and UTF-8, three dependencies with three scopes, the exec plugin for development, the jar plugin's main class, and shade for one runnable JAR.
mvn clean packagebuilds the uber JAR;java -jarruns it with or without arguments.
Translate word by word. The owner and name become groupId and artifactId, "Java 17" becomes maven.compiler.release, every library becomes a dependency with a scope, and "runnable as one JAR" becomes the shade plugin with a main class.
Then check the pom before writing any code: mvn -q validate reads it, and mvn dependency:tree shows that every library and scope came out as intended.
Choosing the right scope
Choose a scope with two questions: does src/main/java compile against the library, and must it travel with the app? The tree below shows the scope at the end of every line.
Example 2 · Checking scopes with dependency:tree
Runs in: a terminal in the receipt-app Maven project
mvn dependency:tree$ mvn dependency:tree [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- dependency:3.7.0:tree (default-cli) @ receipt-app --- [INFO] com.college:receipt-app:jar:1.0-SNAPSHOT [INFO] +- org.apache.commons:commons-text:jar:1.15.0:compile [INFO] | \- org.apache.commons:commons-lang3:jar:3.20.0:compile [INFO] +- com.mysql:mysql-connector-j:jar:26.7.0:runtime [INFO] | \- com.google.protobuf:protobuf-java:jar:4.31.1:runtime [INFO] \- org.junit.jupiter:junit-jupiter:jar:5.14.4:test [INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.14.4:test [INFO] | +- org.opentest4j:opentest4j:jar:1.3.0:test [INFO] | +- org.junit.platform:junit-platform-commons:jar:1.14.4:test [INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.2:test [INFO] +- org.junit.jupiter:junit-jupiter-params:jar:5.14.4:test [INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.14.4:test [INFO] \- org.junit.platform:junit-platform-engine:jar:1.14.4:test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.578 s [INFO] Finished at: 2026-10-16T10:17:43+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- The last word of each line is the scope: Commons Text compile, the driver runtime, JUnit test.
- Transitive JARs inherit those scopes, so nothing from JUnit ends up in the uber JAR.
Yes and yes is compile; no and yes is runtime; yes and no is provided; used only by tests is test. The common mistakes are JUnit with the default compile scope, so test code leaks into the app and the uber JAR, and a JDBC driver with compile scope, so vendor classes creep into your code.
Find the bug
Find the wrong scope
Each dependency has one wrong line. Tap it, then check the fix.
JUnit is used only by the tests.
Round 1 of 3Predicting which phases a command runs
To predict a build, find the phase you named in the order validate, compile, test, package, verify, install. Maven runs everything up to it. Check yourself against the goal lines below.
Example 3 · Phases each command really runs
Runs in: a terminal in the receipt-app Maven project
mvn compilemvn testmvn package -DskipTests$ mvn compile [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] BUILD SUCCESS $ mvn test [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.138 s -- in com.college.app.FeeCalculatorTest [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS $ mvn package -DskipTests [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Tests are skipped. [INFO] --- jar:3.5.0:jar (default-jar) @ receipt-app --- [INFO] BUILD SUCCESS
What this code does
- Each command repeats the goals of the earlier phases before its own.
-DskipTestsstill compiles the tests (testCompile) but surefire says "Tests are skipped."
A goal such as exec:java or dependency:tree runs alone, and clean runs first when you write it first. So mvn clean install deletes target, then compiles, tests, packages and installs, and mvn exec:java runs old classes unless compile comes before it.
Fixing broken builds: coordinates, Main-Class, failing tests
Read the first real error line, not the end of the log: the last lines ("To see the full stack trace...", "[Help 1]") are the same for every failure. Three broken builds follow, each with its key line.
Example 4 · Three broken builds and their key lines
Runs in: a terminal in the receipt-app Maven project
mvn compilejava -jar target/receipt-app-1.0-SNAPSHOT.jarmvn package1) artifactId typed as commons-txt $ mvn compile [WARNING] The POM for org.apache.commons:commons-txt:jar:1.15.0 is missing, no dependency information available [ERROR] Failed to execute goal on project receipt-app: Could not resolve dependencies for project com.college:receipt-app:jar:1.0-SNAPSHOT [ERROR] dependency: org.apache.commons:commons-txt:jar:1.15.0 (compile) [ERROR] Could not find artifact org.apache.commons:commons-txt:jar:1.15.0 in central (https://repo.maven.apache.org/maven2) 2) no jar or shade configuration $ mvn -q clean package $ java -jar target/receipt-app-1.0-SNAPSHOT.jar no main manifest attribute, in target/receipt-app-1.0-SNAPSHOT.jar 3) discount computed on tuition + hostel $ mvn package ... [ERROR] FeeCalculatorTest.scholarshipReducesTuitionOnly:19 expected: <110000> but was: <101600> [ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0 [INFO] BUILD FAILURE
What this code does
- Each failure names its cause in one line: the exact coordinates searched, the missing manifest attribute, or expected 110000 but was 101600 at line 19 of the test.
- Fixes: correct the artifactId; add
mainClasswith the jar or shade plugin; correct the discount totuition * scholarshipPercent / 100.
"Could not find artifact g:a:v" means wrong coordinates; use -U only if they were right. NoClassDefFoundError from java -jar means the libraries are not inside the JAR, so shade them in. "There are test failures" means read expected and but was and the test line, and never hide it with -DskipTests.
More quick fixes:
- "records are not supported in -source 8" → add
maven.compiler.release17. - "No sources to compile" → the code is not under
src/main/java. Unknown lifecycle phase ".shoplite"in PowerShell → quote each-Doption.- Red imports only in the IDE → reload the Maven project.
Fix the error
Fix the failing test
A 20% scholarship should reduce only the tuition. Pick the corrected line.
The hostel fee must never be reduced.
Error
FeeCalculatorTest.scholarshipReducesTuitionOnly:19 expected: <110000> but was: <101600>
Next step
Check your understanding
12 quick questions