Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

for Java, Jenkins, etc.


This shows you how install Maven using Homebrew.

Maven is a task runnner, like Ant and Gradle, Grunt. Unlike Ant, Maven also provides dependency management, standard project layout, and project management.

Mated with Java

Maven is written in Java, and grew up supporting Java builds by creating from Java source and resources for deployment deliverables like JAR file or WAR files.

So Maven is the default for the Jenkins continuous integration tool which triggers a Maven build whenever developer commit code.

Maven adopts the principle of “Convention over configuration”, which means less coding work.

Read more: http://javarevisited.blogspot.com/2015/01/difference-between-maven-ant-jenkins-and-hudson.html#ixzz4CQKSY3oF

Install latest

  1. To install the latest version:

    brew update
    brew install maven

    the response:

    ==> Downloading https://www.apache.org/dyn/closer.cgi?path=maven/maven-3/3.3.9/b
    ==> Best Mirror http://apache.mirrors.pair.com/maven/maven-3/3.3.9/binaries/apac
    ######################################################################## 100.0%
    🍺  /usr/local/Cellar/maven/3.3.9: 94 files, 9.6M, built in 31 seconds
  2. Copy the version number (3.3.9) to your Clipboard.


    Install previous version

    Alternately, to install a previous version:

    brew install homebrew/versions/maven30

    Then, to switch among versions:

    brew unlink maven30 && brew link maven
    brew unlink maven && brew link maven30

    Configure Path

  3. Verify install:

    mvn -version

    I got this response:

    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T09:41:47-07:00)
    Maven home: /usr/local/Cellar/maven/3.3.9/libexec
    Java version: 1.8.0_74, vendor: Oracle Corporation
    Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home/jre
    Default locale: en_US, platform encoding: US-ASCII
    OS name: "mac os x", version: "10.11.5", arch: "x86_64", family: "mac"
  4. Confirm the path:

    which mvn



    NOTE: Before OSX Mavericks, Maven was installed by default in:

  5. Confirm the path. Substitute the version (3.3.9) with the version you just installed:

    cd /usr/local/Cellar/maven/3.3.9/libexec
    cd bin

    mvn files should be listed.

  6. Open a text editor:

    cd $HOME
    subl .bash_profile

    Substitute subl for Sublime with atom or vim.

  7. Add the following to under export PATH=:

    export M2_HOME=/usr/local/Cellar/maven/3.3.9/libexec
    export M2=$M2_HOME/bin
    export PATH=$PATH:$M2_HOME/bin

    Change the version (3.3.9) to whatever appeared above.

  8. In a Terminal, verify changes:

    echo $JAVA_HOME
    The reply I got:
    echo $M2_HOME
    echo $M2
    echo $PATH

Change Default Configurations

  1. Navigate to the hidden folder Maven installed to house packages installed (junit, commons-cli, commons-lang, etc).

    cd ~/.m2/repository </tt>

    Some prefer to change Maven’s local repository to another location.

    See https://maven.apache.org/guides/mini/guide-configuring-maven.html

    There is a settings.xml file containing:


Using Maven

Maven allows use of a central maven repository.

The pom.xml (Project Object Model) file describes project dependencies which Maven resolves by downloading them.

  1. The vast majority of Maven-built projects can be built with this command:

    mvn clean install

    The “clean” cleans out results from the prior build before starting with install.

    Maven enforces a standard naming convention for artifacts defined using groupId, artifactId, and version.

    Maven is said to also act as project management tool because it can generate reports etc.


Maven lifecycle phases

fig maven-lifecycle-phases-527x360-6.png

Here are sub-commands for mvn:

Default Lifecycle:

Sub-command Description
validate Validate the project is correct and all necessary information is available
initialize initialize build state, (for example : set properties, create directories,… etc)
generate-sources generate any source code for inclusion in compilation
process-sources process the source code (for example : filter any values)
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation (for example : to do bytecode enhancement on Java classes)
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code (for example : to filter any values)
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory.
process-test-classes post-process the generated files from test compilation (for example : to do bytecode enhancement on Java classes)
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
package Take the compiled code and package it in its distributable format (for example : jar, war, …etc)
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Clean Lifecycle:

Sub-command Description
pre-clean Executes processes needed prior to the actual project cleaning
clean Remove all files generated by the previous build
post-clean Execute processes needed to finalize the project cleaning

Site Lifecycle:

Sub-command Description
pre-site Executes processes needed prior to the actual project site generation.
site Generates the project’s site documentation.
post-site Executes processes needed to finalize the site generation, and to prepare for site deployment.
site-deploy Deploys the generated site documentation to the specified web server.


  • http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

  • http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-common-goals.html

Maven plug-in goals by lifecycle phase

Each unit of execution work in Maven is called a goal.

fig maven-phase-plugin-goals

Maven C/C++ Plugin http://duns.github.io/maven-nar-plugin/

Maven plug-ins by lifecycle phase

fig maven lifecycle plugin-374x479-60


  • http://www.sonatype.com/resources/books/maven-by-example

  • https://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html

  • http://www.sonatype.com/resources/books/maven-the-complete-reference

More on OSX

This is one of a series on Mac OSX: