Building a Project with Maven

The vast majority of Maven-built projects can be built with the following command:

mvn verify

This command tells Maven to build all the modules, and to check if all integration tests succeeded (when any was defined)

That's it! If you look in the target subdirectory, you should find the build output and the final library or application that was being built.

Note: Some projects have multiple modules, so the library or application you are looking for may be in a module subdirectory.

While this will build most projects and Maven encourages this standard convention, builds can be customisable. If this does not suffice, please consult the project's documentation.

More than just the Build

Maven can do more than just build software - it can assist with testing, run web applications and produce reports on projects, as well as any number of other tasks provided by plug-ins.

When Things go Wrong

The following are some common problems when building with Maven, and how to resolve them.

Missing Dependencies

A missing dependency presents with an error like the following:

[INFO] Failed to resolve artifact.

Missing:
 ----------
1) jnuit:junit:jar:3.8.1

  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -DgroupId=jnuit -DartifactId=junit \
          -Dversion=3.8.1 -Dpackaging=jar -Dfile=/path/to/file

  Path to dependency:
        1) org.apache.maven:maven:pom:2.1-SNAPSHOT
        2) jnuit:junit:jar:3.8.1

 ----------
1 required artifact is missing.

for artifact:
  org.apache.maven:maven:pom:2.1-SNAPSHOT

from the specified remote repositories:
  central (https://repo.maven.apache.org/maven2)

To resolve this issue, it depends on what the dependency is and why it is missing. The most common cause is because it can not be redistributed from the repository and must be manually installed using the instructions given in the message. This is most common with some older JARs from Sun (usually javax.* group IDs), and is further documented in the Guide to Coping with Sun JARs.

You can check the list of repositories at the end of the error to ensure that the expected ones are listed - it may be that the project requires an alternative repository that has not been declared properly or is not accessible with your Maven configuration.

In other cases, it may be an incorrectly declared dependency (like the typo in the example above) which the project would need to fix, like a compilation error.

Inconsistent output

Most plugins are optimized to know if they have to execute their task. In some cases, the output can be polluted from a previous build and the end result is not what you expected. In such rare situations, you can call the clean phase which means: remove the output directory. You can also call it as mvn clean verify which means: first clean up the output directory, next build the project and verify the outcome.