Saturday, October 8, 2016

How to run maven

mvn command

The syntax for running Maven is as follows:

mvn [options] [<goal(s)>] [<phase(s)>]

The order of execution depends on the order in which the goals and the build phases are invoked.

Running maven phases

Example: build a Maven project with the package phase of the default life cycle

mvn package

Example: install in local repository

mvn clean install

Above you see the most common build invocation for a Maven project. The command mvn clean install creates the package and install it in the local repository for re-use from other projects. This command executes each default life cycle phase prior to install, in order, before executing install.

Example: deploy to shared repository

mvn clean deploy

In a build environment, the call mvn clean deploy clean, build and deploy artifacts into the shared repository. When the command is used in a project with one or more sub-projects, Maven traverses every subproject to clean, build and then executes deploy.

Running maven goals

A build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute. If it has one or more goals bound to it, it will execute all those goals

Example: call generate goal with archetype plugin

mvn archetype:generate

Example: call check goal with checkstyle plugin

mvn checkstyle:check

Example: call copy-dependencies goal before the package phase

mvn clean dependency:copy-dependencies package

The clean and package arguments are build phases, while the dependency:copy-dependencies is a goal of a plugin.
The clean phase is executed first (comprising of all preceding phases of the clean lifecycle), then the dependency:copy-dependencies goal, finally is executed the package phase (and all its preceding build phases).

Introduction to maven

What is Maven ?

Maven is a project management tool, based on the concept of a project object model (POM).

Maven was created to solve the lack of standards in Ant.

Maven’s Objectives

Maven’s primary areas of concern are:

  • Providing a uniform build system through a project object model and plugins
  • Providing quality project information from the POM and from the project’s sources
  • Providing guidelines for best practices in software development
    • The maven normal build cycle includes the execution of unit tests.
    • Maven suggests how to layout the structure of the directories of a project.
    • Maven assists in project workflow such as release management and issue tracking.

Introduction to the Build Lifecycle

Maven is based on the concept of a build lifecycle. A lifecycle is a well defined process for building and distributing a project.

There are three built-in build lifecycles:

  • the default lifecycle handles the project build and distribution
  • the clean lifecycle handles project cleaning
  • the site lifecycle handles the creation of documentation of the project
A Build Lifecycle is Made Up of Phases

Each build lifecycles is defined as a list of build phases, where a build phase represents a step in the lifecycle. For example, the main phases of the default lifecycle are:

validate - validate the project is correct and all necessary information is available
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing framework.
package - package the compiled code in distributable format, such as a .jar file.
verify - run any checks on results of integration tests to ensure quality criteria are met
install - install the package into the local repository, for use as a dependency in other projects locally
deploy - copy the final package to the remote repository for sharing with other developers and projects.

The lifecycle phases are executed sequentially to complete the default lifecycle.

A Build Phase is Made Up of Plugin Goals

The manner in which a build phase carries out its responsibilities may vary and depends on the plugin goals bound to that build phase. Each phase has zero or more plugin goals bound to it.

  • A goal represents a specific task which contributes to the building and managing of a project.
    • examples of goals include the compile goal in the Compiler plugin or the test goal of the Surefire plugin
  • A plugin is a collection of one or more goals.
    • example of core plugins are: the Jar plugin, the Compiler plugin, the Surefire plugin
    • example of specialized plugins are: the Hibernate3 plugin