Managing Builds with Maven

Steven J Zeil

Last modified: Dec 21, 2019


Maven combines build management with configuration management (a future topic in this course). It attempts to standardize the build process in a way that improves consistency from project to project and enforces local best practices.

In this lesson, we will look at the capabilities and limitations of Maven and how projects can be structured to use it. We will look at how it works with some of our sample projects.

1 Why Maven?

Another Apache project, Maven came well after Ant had come to dominate the Java open source landscape.

Maven as a Build Manager

Maven uses an underlying task dependency model, but the tasks and their dependencies are pre-defined for a collection of archetype projects.

As a consequence, maven offers far less flexibility than ant, but a basic, unaltered maven build may include tasks that less experienced developers would never think to include or might have difficulty adding to a more conventional build manager.

Motivations for Maven

Grew out of an observation that many supposedly cooperative, related Apache projects had inconsistent and incompatible ant build structures.

Stated goals are

Uniform Build System

Providing quality project information

Providing guidelines for best practices development

2 Maven as a Build Manager

Perhaps the best way to illustrate this is to follow the steps in Maven in 5 Minutes

Building with the Sample Source

OK, that was fun…

Let’s try this with some real source code.

What Went Wrong?

That was a little better

3 Case Studies

3.1 Simple Java Build

Well, we pretty much just did that.

  1. Use mvn archetype:generate to set up the directories and build file.

  2. Replace maven’s “Hello World” code by the real source code.

3.2 Java Build with Code Generation

Not All Sourcecode is Hand-Written

This project adds a stage before compilation to generate some of the source code that then needs to be compiled.

<project xmlns="" xmlns:xsi=""
    This is a tool used to parse code listings and to 
    generate syntax-highlighted C++/Java listings in both
    HTML and LaTeX.

    Markup can be added in the form of special comments that will
    be recognized as instructions to highlight blocks of code, to
    add callout symbols, or to insert vertical ellipses.

  <!-- site generation:
       mvn test
       mvn surefire-report:report
       mvn site


        <artifactId>maven-jflex-plugin</artifactId> ➀
              <goal>generate</goal>  ➁

TYhe highlighted portions represent (most) of the changes required.

How do we know where to put our jflex input files?

How do we know that the proper time to use it is during the generate task?

3.3 C++ Multi-project Build

Not even going to try and go there.

I’ve never had much luck getting maven to work with C++.