An Introduction

To Maven

Deven Phillips

Senior Software Engineer

Sungard Availability Services

What Is Maven?

A build automation tool used primarily for Java projects. The word maven means 'accumulator of knowledge' in Yiddish. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies.

 

- Wikipedia

Compiling Java Projects

  • Classpath Hell
  • Applying Compiler Plugins
  • Managing Dependencies/JARs
  • Project Validation
  • Running Tests
  • Validation
  • Reporting
  • Continuous Integration

Makefiles

  • Not Designed For Java
  • Difficult To Maintain
  • Doesn't Do Dependency Management
  • No Artifact Publishing

Apache Ant

  • Doesn't Do Dependency Management
  • Requires Lots Of Manual Integration
  • Still Leads To Classpath Problems
  • No Artifact Publishing

Apache Ivy

  • Just Ant With Dependency Management
  • Has Artifact Publishing

Why Maven?

  • Simple To Use
  • Widespread IDE Support
  • Extensible With Hundreds Of Plugins
  • Manages Dependencies Simply
  • Handles Nested Child Projects

How Do I Use Maven?

  1. Install Maven from https://maven.apache.org
  2. Create a new Maven project
    • mvn archetype:create 
        -DgroupId=[your project's group id]
        -DartifactId=[your project's artifact id]
        -DarchetypeArtifactId=maven-archetype-quickstart

A Simple Maven Project

<project root>/pom.xml
              /src
                  /main
                       /java/<package>/*.java
                       /resources/
              /target/<package>/*.class

Basic Maven Commands

  • mvn compile
  • mvn site
  • mvn clean
  • mvn package
  • mvn publish
  • mvn test

The POM File

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zanclus</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Adding Dependencies

  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>9.3.2.v20150730</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

Controlling The Build

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

... 

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

...
  
</project>

Useful Plugins

  • SureFire - Run And Report On Unit Tests

  • PMD - Static Code Analysis

  • Eclipse - Generate Eclipse .project from POM

  • Jetty - Run a web application from an embedded Jetty server

  • Cobertura - Code Coverage Reports

  • findbugs - More Static Code Analysis

  • license - Inserts/Verifies License Headers & Files

  • exec - Execute Commands/Java Applications

Let's Use It!

Code will be available at:

 

https://github.com/JUGGL/intro-to-maven

An Introduction To Maven

By Deven Phillips

An Introduction To Maven

An introduction to using the Maven project management toolset

  • 1,801