Part 3: Building Projects with Maven

Once you've created your project (or checked one out from version control) you're likely going to want to build your project. You want to turn your source code and resources into a JAR, WAR or other type of package.

The type of artifact your project generates is defined by the <packaging> element in you pom.xml. If the element isn't supplied Maven defaults to jar for the value. Other possible values are war, ear, rar, par, pom, maven-plugin, and ejb.

To orchestrate the building of a project, Maven uses a concept know as a Build Lifecycle. Maven has three built-in Build Lifecycles:

  • clean - performs project cleanup
  • default - performs compilation, testing, packaging, installation and deployment of your project
  • site - performs site generation and deployment

Each Build Lifecycle consists of a list of ordered Phases. The phases for the lifecycles listed above are shown in Figure 1. Each Phase is associated with a set of Goals and, as described previously, Goals are provided by Maven Plugins. The Goals associated with each Phase are determined by the type of Packaging your project uses.

When you pass a phase to the mvn command, such as mvn compile from the default lifecycle, Maven will work from the starting phase of that lifecycle up to the phase you requested. So running the compile phase will run validate, initialize, generate-sources, process-sources, generate-resources, process-resources and then finally compile.

You can request multiple phases or goals be preformed by separating them on the command-line with a space. Maven will process them in the order provided.

By using the help plugin to describe a Phase, you can see which Plugin Goals are associated with each Phase. They will be in the form groupId:artifactId:goal

Figure 2: Lifecycle Phases

You can provide multiple goals/phases on the command line: mvn clean test