Maven

Steven J Zeil

Last modified: Oct 20, 2020
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.

A major innovation offered by maven is its support for automatically importing third-party libraries for use by the project code. We’ll take a first look at this ability, though we will follow up in more detail later.

1 Why Maven?

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

1.1 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

1.2 Getting Started with Maven

Perhaps the easiest way to illustrate maven is to follow the steps in Maven in 5 Minutes


Building with the Sample Source


1.3 pom.xml

The Maven build file is pom.xml. The POM for our simple project is

<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-SNAPSHOT</version>
  <name>codeAnnotation</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

2 Maven Innovations: archetypes

What you didn’t see in the pom.xml were the targets/tasks that would be used.

2.1 Archetypes Enforce Someone’s Idea of Best Practices

Archetypes can be supplied by


Creating Archetypes

Creating new archetypes is

2.2 Extending Maven for an Individual Project

Maven is usually extended by adding plugins

2.2.1 Plugin Example - JFlex

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

pom.xml.jflex.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>

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


  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>forge350</id>
      <name>ODU CS Forge350 Repository</name>
      <url>http://forge350.cs.odu.edu/archiva/repositories/internal</url>
    </repository>
  </repositories>
  <distributionManagement>
    <repository>
      <id>forge350Scp</id>
      <name>ODU CS Forge350 Repository (scp)</name>
      <url>scp://forge350.cs.odu.edu/var/lib/gforge/chroot/home/groups/mavenrepo1/htdocs/repo</url>
    </repository>
  </distributionManagement>



  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
         <artifactId>wagon-ssh</artifactId>
         <version>2.2</version>
      </extension>
    </extensions>
    <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>
  
  </build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
</project>

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


Using plugins is not always simple:

2.2.2 Plugin Example - Ant

One of the more popular ways to “escape” when Maven is getting in the way of a simple step is the antrun plugin

3 Maven Innovations: 3rd Party Libraries

3.1 Dependencies

Picking up with the example we used earlier.

3.1.1 Updating the Dependency

3.1.2 Fetching Dependencies

3.1.3 Version Ranges

3.1.4 Maven Repositories

3.1.5 Transitive Dependencies

How does Maven know whether junit itself depends on other libraries?

3.2 This was a Game Changer!

The combination of

was a tremendous advance.

Probably the major factor in attracting Java developers to start working with Maven.

3.2.1 “Imitation is the sincerest form of flattery”

(Oscar Wilde)