Maven

Goals

Concepts

Preview

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

  <groupId>com.example</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0</version>

  <name>Hello World Program</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
</project>
cd helloworld
mvn clean package
java -classpath target/helloworld-1.0.jar com.example.HelloWorld

Preparation

  1. Download and install Maven.
    1. Make sure your JAVA_HOME environment variable is set.
    2. Make sure your M2_HOME environment variable is set.
    3. Make sure that the Maven …/bin subdirectory is in your system PATH environment variable.

Lesson

Apache Maven is a popular tool used to compile, test, package, and deploy (collectively called “to build” or “the build”) Java projects. Rather than seek out javac to compile your programs, you'll ask Maven to do it. Once you start working with multiple classes (and using third-party libraries, which you'll learn about another day), you'll appreciate handing the build off to Maven rather than invoking javac on each file in your source code.

Project Object Model (POM)

The Maven build configuration for a project is stored in the Project Object Model (POM), which is described in a pom.xml file in the root directory of your project. the POM is stored in a simple XML format, the most basic of which looks like this:

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

  <groupId>com.example</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0</version>

  <name>Hello World Program</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
</project>

Project Coordinates

Your project is identified by coordinates. The coordinates are extremely important. They make up the basis for identifying components or artifacts in Maven, allowing you to easily use your project with other libraries. There are three minimum coordinates your project must have: groupId, artifactId, and version.

Coordinate Description
groupId Some identifier for the individual, company, or organization releasing the application or library. Usually this is the same as the Java package or relevant part of the package identifier.
artifactId Something to identify the program or library. Should be lowercase letters, with the hyphen character - allowed.
version Something to identify the version of your program. Usually a sequence of number separated by dots, such as 2.0.1.
Snapshot Versions

Maven provides an option for programs that are still in flux and would otherwise need to generate a new version number several times a day. If your program is under construction and not yet ready for public release, you can add a -SNAPSHOT to the end of your version coordinate, such as 2.0.1-SNAPSHOT. This let's Maven know not to expect the features of this version to be completely fixed, and not to use any older, cached version of the snapshot when performing builds. You can keep this snapshot version until you are ready for the final release—just don't forget to remove the -SNAPSHOT before you publish your final product!

Project Directory Structure

Maven expects your project to be stored in a certain standard directory layout, starting at the top-level project root directory you initially created. There are several directories Maven recognizes for different functions; for now, these are all you need to know. Note that although the source code hierarchy merely moves two levels down, the root directory of the project stays the same. See Introduction to the Standard Directory Layout for more details.

pom.xml Maven POM
src/main/java

Main source code root directory.

e.g. src/main/java/com/example/HelloWorld.java

When it compiles your project, Maven will produce .class files, which it places in the target/classes/ directory subtree:

target/

Output root directory, containing package archives. (See Packaging below.)

e.g. target/helloworld-1.0.jar

target/classes

Receives an accumulation of compiled classes and copied/filtered resources.

e.g. target/classes/com/example/HelloWorld.class

Maven Life Cycle

When Maven goes about its work, it goes through several stages called phases in the life cycle of the build. (Maven prefers the single word “lifecycle”.) There are many phases to the default life cycle; here are the only ones you need to know right now:

  1. validate
  2. initialize
  3. compile
  4. test
  5. package

Building with Maven

The following instructions work both on Linux and on Windows.

Compiling

When you invoke Maven indicating a particular phase, Maven will execute all phases up to and including the indicated phase. For example, the following command will cause Maven first to validate your project, initialize some settings, and only then compile your source files. You can later ask Maven to perform special actions during those or other phases.

cd helloworld
mvn compile

Adding clean to any Maven invocation will cause Maven first to clean the artifacts from any previous build. For example, mvn clean compile will cause Maven first to erase the .class files and other files in the target/ directory before compiling.

cd helloworld
mvn clean compile

Running Your Program from Maven

You can also run your program directly from Maven. You will need to specify the main class of your file, just as you would with java.

cd helloworld
mvn exec:java -Dexec.mainClass="com.example.HelloWorld"

Running Your Program from Class Files

Remember from an earlier lesson that you ran your program by invoking java from the root directory of your project. Because Maven places the .class files relative to the the target/classes/ directory, not relative your project root directory, you'll first need to change to that directory before you can run your program.

cd helloworld/target/classes
java com.example.HelloWorld

Another option is to use the java -classpath or -cp option to set the Java classpath, which is (naturally) the path to the location for finding class files.

cd helloworld
java -classpath target/classes com.example.HelloWorld

Testing

Maven knows how to run tests as well. Currently you probably don't have any tests to run; if you did you could enter the following command to have Maven test your program. Maven would also compile your program if needed, because the test phase comes after the compile phase in the Maven life cycle.

cd helloworld
mvn test

Packaging

Maven also knows how to package your .class files in an archive for distributing to others. Most projects consist of more than one class, and distributing multiple .class files separately would be unwieldy. The most common archive format for distributing Java programs is the JAR file, which is essentially a Zip file with a .jar extension. This is the type of archive Maven produces by default when it packages your project output.

To package your project, issue the following command to Maven.

cd helloworld
mvn package

This time, along with the .class file(s) placed in the target/classes/ directory, Maven creates a JAR file containing these files and places it in the target/ directory. Thus the project defined by the example POM above will result in the JAR file target/helloworld-1.0.jar when Maven packages the project.

Running Your Program from a JAR File

Besides running your program from one or more .class files as you've have been doing up till now, you can also run a program from a JAR file package. In fact this is the way Java applications are usually run, as Java programs are typically distributed via JAR files instead of individual .class files. You'll soon see that Maven coupled with JAR files allows easy integration of an application JAR, the library JARs it depends on, their JAR dependencies, and so on.

Running a Java program from a JAR file works similar to running a Java program from a set of .class files, except that you'll indicate the -classpath to the JAR instead of the .class file output root directory.

cd helloworld
java -classpath target/helloworld-1.0.jar com.example.HelloWorld

Plugins

All the actions Maven takes, including compiling your program and executing it from within Maven, are controlled by plugins. One action you might want to take is to generate Javadoc documentation as you have been doing manually using the javadoc tool. There exists an Apache Maven Javadoc Plugin that will generate Javadoc documentation for you. By specifying the plugin in the POM and invoking it though Maven, it will already know where your classes are located. Plugins are specified in the <build> section of your POM.

Specifying the Maven Javadoc Plugin.
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>3.0.1</version>
      </plugin>
    </plugins>
  </build>

After adding the plugin to the POM, you need to invoke a goal of the plugin. The Maven Javadoc Plugin has several goals. The first one you will want to try is the javadoc goal. You can invoke it through Maven itself on the command line, specifying the plugin name and then the goal, separated by a colon : character.

cd helloworld
mvn javadoc:javadoc

The Maven Javadoc Plugin will generate documentation as if you had used the javadoc tool manually; you do not need to specify any packages or output directory. By default the plugin will place the documentation in the target/site/apidocs directory.

Review

Summary

Maven standard project directory layout.
pom.xml Maven POM
src/main/java Main source code root directory.
target/ Output root directory, containing package archives.
target/classes Receives an accumulation of compiled classes and copied/filtered resources.
Maven life cycle.
  1. validate
  2. initialize
  3. compile
  4. test
  5. package

Gotchas

In the Real World

Think About It

Self Evaluation

Task

Convert the “Hello World” program you wrote in a previous homework to match the conventions expected by Maven. Be able to clean and package your program using Maven; as well as run your program from the resulting JAR file.

Create a compressed archive of your entire project and send it to your teacher.

See Also

References

Resources