Saturday, October 8, 2016

How to run maven

mvn command

The syntax for running Maven is as follows:

mvn [options] [<plugin:goal>] [<phase>]

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.

Introduction to maven

What is Maven ?

Maven is a project management tool, based on the concept of a project object model (POM).
Maven includes a project lifecycle, a dependency management system and execution of plugin goals at defined phases in a lifecycle.

Convention over configuration

Maven uses convention over configuration. Convention over configuration means that the system assume resonable defaults and the user is not required to do the configuration of the system.

Maven's conventions apply to build phases:

  • a maven project is assumed to produce a JAR file.
  • maven defines a default lifecycle and a set of common plugins that know how to build and integrate software.
  • maven core plugins apply conventions for many common processes.

Maven provides default location of directories:

  • the source code is assumed to be in basedir/src/main/java
  • the tests are assumed to be in basedir/src/test/java
  • the resources are assumed to be in basedir/src/main/resources
  • the compiled code is in basedir/target/classes
  • the JAR is in basedir/target

When you use maven, you write your source code and if you follow the conventions, you only have to put the code in the predefined directory, maven will take care of the rest.