Managing Builds with Maven

Steven J Zeil

Last modified: Sep 14, 2017
Contents:

Abstract

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.

pom.xml.listing
<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.odu.cs</groupId>
  <artifactId>codeAnnotation</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>codeAnnotation</name>
  <url>https://secweb.cs.odu.edu/~zeil/cs795SD/s13/Directory/topics.html</url>
  <description>
    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.
  </description>

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


  <repositories>
  </repositories>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>de.jflex</groupId>
    	<artifactId>jflex</artifactId>
    	<version>1.4.3</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>de.jflex</groupId>
        <artifactId>maven-jflex-plugin</artifactId> ➀
        <version>1.4.3</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>  ➁
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins> 
  </pluginManagement>
  </build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
</project>

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++.