Maven
  • 16 Jan 2023
  • 2 Minutes to read
  • Dark
    Light
  • PDF

Maven

  • Dark
    Light
  • PDF

Building Java Applications with Maven

Apache Maven (or Maven) is the most common build, packaging, and deployment tool for Java. Maven began as an attempt to standardize the build processes in the Jakarta Turbine project, which consisted of multiple projects, build scripts, and JAR output files—but Maven made it easy to publish project information and share JAR files.

The original goals of Maven were:

  • Simplify the build process
  • Provide a unified build system
  • Provide high quality project information
  • Provide guidelines for development best practices
  • Enable transparent migration to new features
  • All major IDEs (Eclipse, Netbeans, IntelliJ) use Maven under the hood

Maven Overview

Maven is a command line tool that works at the project level. In Maven, a project is conceived as an application or library and is defined by a POM.xml file. Maven packages projects into an artifact (JAR, WAR, EAR, etc.), and artifacts can be published to a local repository on a developer's machine or a build server, or to an enterprise repository such as ProGet to share with other developers.

Archetypes

A Maven archetype is a project template used to create Java projects for web applications, Jakarta EE projects, plugins for Maven, sites (i.e., a documentation site included in the Maven artifact), or custom archetypes. Maven ships with a default set of archetypes, and additional archetypes can be downloaded from a Maven repository.

Build Lifecycle

A build lifecycle in Maven consists of a series of phases, with each phase tied to zero or more goals. A goal in Maven is essentially an individual build task. The execution of a phase implicitly executes all phases before it in the lifecycle (e.g., compile must be executed before test). For more information on the different lifecycles, see here.

The most common way to associate goals with phases is to configure packaging element in your POM file under the <packaging> element. Some valid packaging values are jar (the default), war, ear, and pom.

Building with Maven

Once you have configured your Maven project, the general pattern for building any type of Maven application involves:

  • Run mvn clean package
    • The clean phase will remove all files generated by the previous build, and the package phase will execute all necessary phases to generate an artifact
    • For sites, it would be mvn clean site, since the package phase does not exist for sites

The build process can be customized by referencing a specific lifecycle in the POM file or by executing specific targets directly instead of phases. To execute targets directly, you would execute:

mvn clean:clean resources:resources compiler:compile jar:jar

Executing Maven with BuildMaster

To execute Maven builds in BuildMaster:

  • Maven must be installed on the build server prior to executing any of these commands
  • The Java extension must be installed in BuildMaster
  • Do not execute the install phase
    • When deploying with BuildMaster, we recommend only packaging artifacts because the package output will be captured and deployed as a BuildMaster artifact

Use the following OtterScript in a deployment plan to execute Maven in BuildMaster:

Java::Execute-Maven
(
    GoalsAndPhases: "clean package"
);
  • Running Maven directly:
Exec "mvn clean package";

Example plan that gets the latest source code from Git and captures a Maven artifact as a BuildMaster artifact:

Git::Get-Source
(
    RepositoryUrl: https://github.com/Inedo/ProfitCalcJava.git,
    Branch: master
);

Java::Execute-Maven
(
    GoalsAndPhases: "clean package",
    In: ProfitCalcJava
);

Create-Artifact ProfitCalcJava
(
    From: ProfitCalcJava\target
);

Was this article helpful?