Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 41 ยท Practice and Review

Practice Exercises with Solutions

Twelve graded Maven exercises on POMs, scopes, dependency mediation, profiles, versions and reactor order, with full solutions and runnable checkers.

Reading about Maven is not the same as predicting what it will do. These twelve exercises train that prediction: you look at a POM, a dependency tree or a command line and say what Maven resolves, runs or complains about. They go from easy to hard, and every one has a full solution further down the page.

Work on paper or in a text editor first, then read the solution. Maven cannot run on this site, so a few exercises are answered by reasoning only. Four of them also have a runnable Java checker, a stand-in that implements a simplified version of Maven's rule so that you can try other inputs. Use the exercises to check lessons you were unsure about: scopes, mediation, profiles and the reactor.

How to use this page

Try first Write your answer before looking.
Compare Read the solution and the why.
Experiment Change the checker's input.
1 / 2

Warm-up

Exercise 1: Fix the POM

This file fails to build. List every problem you can find.

XML
<project>
  <groupId>com.example</groupId>
  <artifactId>Demo App</artifactId>
  <packaging>jarr</packaging>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
  </properties>
</project>

Exercise 2: Predict the lifecycle

For a project with jar packaging, list the plugin goals that run, in order, for mvn clean package.

Exercise 3: Choose the scope

Pick compile, provided, runtime or test for each dependency of a web application that is deployed to Tomcat:

  1. The Servlet API (Tomcat supplies it).
  2. The PostgreSQL JDBC driver, which your code never references directly (it is loaded through DriverManager).
  3. JUnit.
  4. Guava, whose classes your code calls.
  5. Lombok, an annotation processor needed only while compiling.

Exercise 4: Interpolate

Given a project with artifactId shop-api and version 1.4.0-SNAPSHOT, what does each value resolve to?

XML
<properties>
  <jackson.version>2.18.2</jackson.version>
  <jar.name>${project.artifactId}-${project.version}</jar.name>
  <bundle.name>${jar.name}-bundle</bundle.name>
  <missing>${no.such.property}</missing>
</properties>

Core skills

Exercise 5: Nearest wins

The application declares A:1.0 and then B:1.0. These are the dependencies of each artifact:

  • A:1.0 needs C:2.0
  • C:2.0 needs D:1.0
  • B:1.0 needs E:1.0
  • E:1.0 needs C:3.0
  • C:3.0 needs D:2.0

Which versions of C and D end up on the classpath, and why?

Exercise 6: A tie, and how to break it

The application declares X:1.0 and then Y:1.0. X:1.0 needs L:1.1 and Y:1.0 needs L:1.2.

  1. Which version of L wins?
  2. What changes if the application declares Y:1.0 first?
  3. Name two ways to force L:1.2 regardless of order.

Exercise 7: Which profiles are active?

XML
<profiles>
  <profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation></profile>
  <profile><id>ci</id><activation><property><name>env</name><value>ci</value></property></activation></profile>
  <profile><id>jdk21plus</id><activation><jdk>[21,)</jdk></activation></profile>
  <profile><id>windows</id><activation><os><family>windows</family></os></activation></profile>
  <profile><id>release</id><activation><property><name>release</name></property></activation></profile>
</profiles>

Which profiles are active in each situation?

  1. mvn -Denv=ci verify on JDK 25, Linux.
  2. mvn verify on JDK 17, Linux.
  3. mvn -Drelease verify on JDK 21, Windows.
  4. mvn -Pdev -Denv=ci verify on JDK 17, Linux.

Exercise 8: Managed versions and scopes

The parent POM contains this block. The child module declares junit-jupiter with only a groupId and an artifactId, and also declares Guava with version 32.1.3-jre. What are the effective version and scope of each?

XML
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.11.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>33.4.0-jre</version>
    </dependency>
  </dependencies>
</dependencyManagement>

Challenges

Exercise 9: Reactor order

The parent lists modules in this order: docs, app, web, service, core, util. Dependencies: app needs web; web needs service; service needs core and util; core needs util; docs needs core.

  1. In what order does the reactor build them?
  2. Which modules does mvn -pl web -am package build?
  3. Which modules does mvn -pl core -amd package build?

Exercise 10: Three classpaths

Module app declares lib-a (compile), servlet-api (provided), jdbc-driver (runtime) and test-kit (test). The POM of lib-a declares lib-a1 (compile), lib-a2 (runtime), lib-a3 (test) and lib-a4 (provided). List the compile, runtime and test classpaths of app.

Exercise 11: Sort the versions

Sort these versions from oldest to newest by Maven's rules: 1.0.1, 1.0, 1.0-SNAPSHOT, 1.0-rc1, 1.0-alpha-2, 1.0-beta1, 0.9, 1.0-sp1. Then decide where 1.10 belongs relative to 1.9.

Exercise 12: Read the logs

Two failures from a CI run. For each, name the cause and the fix.

TEXT
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile
  (default-compile) on project shop-core: Fatal error compiling: error: release version 25
  not supported

(mvn -v prints Java version: 21.0.5 for that machine.)

TEXT
$ mvn -pl shop-app package
[ERROR] Failed to execute goal on project shop-app: Could not resolve dependencies:
  Could not find artifact com.example.shop:shop-service:jar:1.0.0-SNAPSHOT in central

Checkers you can run

These are stand-ins: simplified versions of Maven's rules in Java, so you can feed in your own trees, versions and profiles. They are not Maven itself. The real resolver handles more cases (exclusions, managed versions, optional dependencies), but for these exercises the simplified rule gives the same answers.

The first checker implements nearest-wins mediation with a breadth-first walk, for Exercises 5 and 6. Add your own entries to TREE and try them.

Java Mediation checker (exercises 5 and 6)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The second checker compares versions with a simplified form of Maven's ordering: numeric parts first, then a qualifier ranked alpha < beta < milestone < rc < snapshot < (release) < sp. It answers Exercise 11 and shows why sorting version strings alphabetically is wrong.

Java Version order checker (exercise 11)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The third checker implements profile activation for Exercise 7, including the rule that surprises most people: activeByDefault profiles switch off as soon as any other profile in the same POM becomes active.

Java Profile activation checker (exercise 7)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The fourth checker orders the reactor for Exercise 9 with a depth-first sort that visits modules in the order the parent lists them, and applies -am and -amd.

Java Reactor order checker (exercise 9)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Solutions

Solution 1

There are four problems. <modelVersion>4.0.0</modelVersion> is missing, so Maven rejects the file. <version> is missing, and a POM without a parent must declare one. The artifactId Demo App contains a space; artifact IDs may only use letters, digits, dots, hyphens and underscores, so use demo-app. Finally jarr is a typo for jar (or leave <packaging> out, since jar is the default), and Maven reports an unknown packaging type. Declaring the XML namespace on <project> is not required but is conventional, because it lets editors validate the file. See The POM File.

Solution 2

The clean lifecycle runs first, then the default lifecycle through package. The goals bound to those phases for jar packaging are:

  1. clean:clean
  2. resources:resources
  3. compiler:compile
  4. resources:testResources
  5. compiler:testCompile
  6. surefire:test
  7. jar:jar

Phases without a bound goal (validate, initialize, pre-clean, prepare-package and others) are still passed through, but do nothing. See Lifecycles and Phases and Plugins and Goals.

Solution 3

  1. Servlet API: provided. It is needed to compile but the container supplies it at run time; packaging it would clash with Tomcat's own copy.
  2. JDBC driver: runtime. The code does not compile against it, but it must be present when the application runs.
  3. JUnit: test.
  4. Guava: compile, the default.
  5. Lombok: provided (it is only needed during compilation, and consumers must not inherit it). Newer setups use annotationProcessorPaths for processors instead.

The matrix of scopes and classpaths is in Dependency Scopes.

Solution 4

  • jackson.version is 2.18.2.
  • jar.name is shop-api-1.4.0-SNAPSHOT.
  • bundle.name is shop-api-1.4.0-SNAPSHOT-bundle: interpolation is repeated until no known property is left.
  • missing stays ${no.such.property} literally. Maven does not fail for an unknown property; it leaves the text unchanged, which is a classic source of strange file names. See Properties and Build Settings.

Solution 5

C resolves to 2.0 and D to 1.0. C:2.0 is two steps from the application (application, A, C), while C:3.0 is three steps away (application, B, E, C). The nearest declaration wins, so C:3.0 is dropped. Because C:3.0 is omitted, its own dependency D:2.0 never enters the picture, and D:1.0 (from C:2.0) is selected. The final set is A:1.0, B:1.0, C:2.0, D:1.0, E:1.0. See Transitive Dependencies and Mediation.

Solution 6

  1. L:1.1. Both routes are two steps away, and with equal distance the first declaration wins (X is listed first).
  2. With Y declared first, L:1.2 wins.
  3. Declare L:1.2 directly in the application's <dependencies> (distance one always wins), or pin it in dependencyManagement, which overrides the version of transitive dependencies too. Relying on the declaration order is fragile, so prefer one of these. See Managing Versions.

Solution 7

  1. ci and jdk21plus. dev is off, because another profile is active.
  2. dev. Nothing else matches, so the default profile applies.
  3. jdk21plus, windows and release. Defining the property release with no value is enough, because the activation names the property without a value.
  4. dev and ci. Naming dev with -P activates it explicitly; ci activates through the property. Explicit activation always works, whatever else is active.

See Profiles and Environments.

Solution 8

junit-jupiter becomes version 5.11.4 with scope test: the child omitted both, and management supplies the missing parts. Guava uses 32.1.3-jre with scope compile: a version written in the child always overrides the managed one, so be careful when a child quietly shadows the parent's decision.

Solution 9

  1. util, core, docs, service, web, app. The sort visits modules in listed order. docs comes first, so before it can be built Maven builds its dependency core, and before that util. Then app pulls in web, which pulls in service. The listing order only decides ties.
  2. util, core, service, web: web and everything it needs.
  3. core, docs, service, web, app: core and everything that depends on it, directly or not.

Solution 10

  • Compile classpath: lib-a, servlet-api, lib-a1. Runtime dependencies are not needed to compile, and provided or test dependencies of lib-a are never passed on.
  • Runtime classpath: lib-a, jdbc-driver, lib-a1, lib-a2. Provided and test scopes are excluded; the runtime dependency of lib-a is kept.
  • Test classpath: lib-a, servlet-api, jdbc-driver, test-kit, lib-a1, lib-a2. Everything except what lib-a declared as test or provided.

The multi-module project lesson has a calculator for exactly this rule.

Solution 11

Oldest to newest: 0.9, 1.0-alpha-2, 1.0-beta1, 1.0-rc1, 1.0-SNAPSHOT, 1.0, 1.0-sp1, 1.0.1. A SNAPSHOT is older than the release it precedes, but newer than that release's candidates; a service-pack qualifier follows the plain release; and 1.0.1 is a higher number than anything in the 1.0 family. Maven compares numbers numerically, so 1.10 is newer than 1.9, even though it sorts earlier as plain text. See Coordinates.

Solution 12

First log: the release is 25 but Maven runs on Java 21. Run Maven with a JDK 25 (set JAVA_HOME, or choose the matching setup-java version in CI) or lower maven.compiler.release to 21.

Second log: shop-app needs shop-service, which has not been built or installed. Run mvn -pl shop-app -am package so the reactor builds the modules it needs, or run mvn install at the root first. See Troubleshooting Common Maven Errors.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A child module declares a dependency with a version that differs from the parent's dependencyManagement. Which version is used?

    A version written directly in the child's dependency always overrides the managed one.
  2. 2. Which classpath contains a dependency declared with runtime scope?

    Runtime dependencies are not needed to compile, but they are on the runtime classpath and the test classpath.
  3. 3. Two routes to library L have the same distance from your project. Which version is chosen?

    Nearest wins; on a tie, the first declaration wins.
  4. 4. In Maven's ordering, how does 1.0-SNAPSHOT relate to 1.0?

    A snapshot is a work in progress towards a release, so it is ordered before the release itself.

Summary

  • Predicting Maven's behavior comes down to a few rules: lifecycle order, scope visibility, nearest-wins mediation, profile activation and topological reactor order.
  • dependencyManagement supplies missing versions and scopes; an explicit version in the child overrides it.
  • activeByDefault profiles turn off as soon as any other profile in the POM is active.
  • Versions compare numerically with ranked qualifiers: 1.10 is newer than 1.9, and a SNAPSHOT is older than its release.
  • The Java checkers above are simplified stand-ins for Maven's rules, useful for experiments but not the real resolver.

Continue with the next lesson in the track: Maven Interview Questions.