Getting Started with Maven

November 28, 2017

Now that I have Maven installed (see previous post) I wanted to create a simple project and compile it.

Generate New Project

We can use Maven to generate a new project, taking a template project structure to work from. Here we're using the maven-archetype-quickstart to give us a simple Hello World project.

$ cd projects
$ mvn archetype:generate -DgroupId=uk.co.drumcoder -DartifactId=sample 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false

The groupId defines the package that the generated code resides within. The artifactId is the project name, and the last entry switches off interactive mode.

The result of this is a simple generated project, with the following main:

public static void main( String[] args )
{
    System.out.println( "Hello World!" );
}

and the following pom.xml:

<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>uk.co.drumcoder</groupId>
  <artifactId>sample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sample</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>

Compiling

We can now compile this generated code using Maven:

 $ mvn compile

This gave me a Build Failure:

Target option 1.5 is no longer supported. Use 1.6 or later.

I'm sure this is due to my use of the very new Java 9. To get around this problem, I added

<properties>
  <maven.compiler.source>1.6</maven.compiler.source>
  <maven.compiler.target>1.6</maven.compiler.target>
</properties>

inside the project xml tag, giving me a total pom.xml of:

<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>uk.co.drumcoder</groupId>
  <artifactId>sample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sample</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

The mvn compile command now completes successfully.

Running The Program

In order that we can run the program, we first need to run mvn install:

$ mvn install

This will build the code into the target folder.

Once this is done, we can run the program with:

$ java -cp target/sample-1.0-SNAPSHOT.jar uk.co.drumcoder.App
Hello World!

Tags: maven, java, mac, build