Maven

Steven J Zeil

Last modified: Dec 27, 2023
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 - 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)

3.3 Overall

Goals scripting make ant maven
easy to use? Y Y Y Y
easy to set up for a given project? N N N Y 1
efficient: avoid redundant/unnecessary actions N Y ? Y
efficient: detect and abort bad builds in progress ? Y ? Y
incremental: allow focused/partial builds ? Y Y ?
flexible: allow for a variety of build actions N Y Y N
flexible: builds on/for a variety of platforms N N Y Y
configurable: permit the management of multiple artifact configurations ? ? Y Y

1: But can be very hard to modify.