You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Prerequisites

Basic Concepts

For the below examples, a Maven <command> will typically be of the form mvn clean package install where mvn is the command being run, and clean/package/install/etc are phases of the Maven project you're building.

Maven includes several predefined phases to cover the default lifecycle:

  • clean - clears out old build artifacts
  • test - run the Maven surefire plugin to execute your tests and display the result (i.e. JUnit, TestNG, etc)
  • package - download dependencies (if necessary) and build up new artifacts into the target/ folder
  • install - installs built artifacts (assumes package) from your target/ folder into your local .m2 Maven repository, allowing local Maven projects to consume the built artifacts
  • install-file - install a JAR file manually to your .m2 repo, allowing local Maven project to consume the JAR
  • deploy - publish built artifacts (assumes package) to a remote Maven repository, allowing local Maven projects to consume the built artifacts
  • deploy-file - publish a standalone JAR file manually to a remote Maven repository, allowing other remote Maven projects to consume the JAR

For a full list of these lifecycle goals: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

How it Works

Every Maven project contains a file called pom.xml at the project root. This file is the POM or Project Object Model, which tells Maven everything it needs to know about the project's dependencies and build steps.

Running a Maven command will parse the current project's POM to determine available phases/goals, and execute them according to the details defined within.

The pom.xml can also link off to various other XML documents describing various pluggable aspects of the build process:

  • build.xml: if you're using the Maven Antrun Plugin, this file tells Ant how to execute
  • assembly.xml: If you're using the Maven Assembly Plugin, this file tells Maven how to package your application (JAR, WAR, ZIP, etc)

Nested Projects

POMs can also exist in subfolders of the root. Every subproject's POM inherits the values defined by the parent, creating a nested hierarchy. 

Values in subprojects will override values in the parent project, so we must first compute an "effective POM" by walking down the POM tree from root project to our target subproject and collect all values encountered.

Usage

Now that you've heard the basics, let's try it out! 

Via Maven

The following command will run Maven's clean and install phases natively:

mvn clean install

This is a very common command to build a Maven project from source, and install its built artifacts into your .m2 cache for local consumption.

Via Docker

The following command will run the same phases within a Docker container, without needing to install Maven on your machine:

docker run -it --rm -v $(pwd):/build -w /build maven:3-jdk-8 mvn clean install

Publishing Artifacts

To easily run your own test Nexus repository via Docker:

docker run -d -p 8081:8081 --name nexus sonatype/nexus:oss

OSSRH

For a more long-term solution for hosting your (open-source) Maven artifacts, you can follow these steps to deploy them to OSSRH. OSSRH will even sync your released artifacts with Maven Central on your behalf.

Third-Party Dependencies

mvn deploy:deploy-file -Dfile=./indri-5.11.jar -DgroupId=edu.illinois.lis -DartifactId=indri -Dversion=5.11-SNAPSHOT -Dpackaging=jar -Durl=https://oss.sonatype.org/content/repositories/snapshots/ -DrepositoryId=ossrh


mvn deploy:deploy-file -Dfile=./indri-5.11.jar -DgroupId=edu.illinois.lis -DartifactId=indri -Dversion=5.11-SNAPSHOT -Dpackaging=jar -Durl=https://oss.sonatype.org/content/repositories/snapshots/ -DrepositoryId=ossrh

NOTE: If you attempt to deploy a SNAPSHOT to staging, or a non-SNAPSHOT to snapshots, the command will fail.

Syncing to Maven Central

Even OSSRH has a strict list of requirements for syncing artifacts to Maven Central.


  • No labels