Maven

Steven J Zeil

Last modified: Nov 20, 2013

Contents:
1. Why Maven?
2. Maven as a Configuration Manager
2.1 Dependencies
2.2 Extending Maven
3. Maven Report Generation
4. Further Modifying Maven Projects
4.1 Maven and Ant
4.2 Maven and Eclipse

1. Why Maven?

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


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

2. Maven as a Configuration Manager

2.1 Dependencies

Picking up with the example we used earlier.

Updating the Dependency

Fetching Dependencies

Version Ranges

Maven Repositories

Transitive Dependencies

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

Choosing Repositories

Example: the Forge350 Repository

A basic shared repository can be found here.

To add access to it, we add this to our pom.xml:

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


Publishing to a Repository

Suppose that we want to publish our jar to a repository.

  <repositories>
       ⋮
    </repository>
  </repositories>
  <distributionManagement>
    <repository>
      <id>forge350-publish</id>
      <name>ODU CS Forge350 Repository</name>
      <url>http://forge350.cs.odu.edu/archiva/repository/internal/</url>
    </repository>
  </distributionManagement>


Connection Info

In addition to adding the output repository to the POM, you need to provide your authentication info for the servers that you use in \string/.m2/settings.xml:

settings.xml.listing

2.2 Extending Maven


Circling the Wagons

However, for whatever reason, communication with repositories has a separate extension mechanism, called wagons.

  </dependencies>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
         <artifactId>wagon-ssh</artifactId>
         <version>2.2</version>
      </extension>
    </extensions>
  </build>


If all is well…


Modification via Plugin


Adding the JFlex Plugin

pom.xml.jflex.listing

3. Maven Report Generation


Maven Report Generation


Generating a Website

mvn site will generate a project website.

pom.xml.website.listing


Adding Reports

pom.xml.reports.listing

4. Further Modifying Maven Projects

Maven is wonderful when your project matches the archetype.

When it doesn’t …


Sometimes Doing the Easiest Things

… will be the hardest thing to accomplish in Maven

Basically, Maven is wonderful when your projects matches an archetype, and tortuous when it does not.

4.1 Maven and Ant


Maven and Ant

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

4.2 Maven and Eclipse


Maven and Eclipse