Maven

maven2-100156414-orig

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

The installation of Apache Maven is a simple process of extracting the archive and adding the bin folder with the mvn command to the PATH.

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

1. Installing Maven on Windows

To install Maven on windows, head over to Apache Maven site to download the latest version, select the Maven zip file, for example apache-maven-3.3.9-bin.zip.

Unzip it to the folder you want Maven to live.

1.1. Adding Maven to the Environment Path

Add both M2_HOME and MAVEN_HOME variables to the Windows environment using system properties, and point it to your Maven folder.

Update the PATH variable by appending the Maven bin folder – %M2_HOME%\bin, so that you can run the Maven’s command everywhere.

To verify it run:

mvn -version

in the command prompt. It should display the Maven version, the java version, and the operating system information. That’s it, you have set up Maven on your Windows system.

2. Installing Maven on Linux

To install Maven on the Linux operating system, download the latest version from the  Apache Maven site, select the Maven binary tar.gz file, for example: apache-maven-3.3.9-bin.tar.gz.

Extract the archive to your desired location.

2.1. Adding Maven to the Environment Path

Open the command terminal and run the following commands to set the environment variables:

$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9
$ export M2=$M2_HOME/bin
$ export MAVEN_OPTS=-Xms256m -Xmx512m

with M2_Home path corresponding with the location of your extracted Maven files.

Now append the M2 variable to the system path:

$ export PATH=$M2:$PATH

Finally, verify if Maven has been added by running:

$ mvn -version

The output should be as follows:

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2016-12-03T17:27:37+05:30)
Maven home: /usr/local/apache-maven/apache-maven-3.3.9
Java version: 1.8.0_75, vendor: Oracle Corporation
Java home: /usr/local/java-current/jdk1.8.0_75/jre

You have successfully installed Maven on your Linux system.

2.2. Installing Maven on Ubuntu

In a terminal, run apt-cache search maven, to get all the available Maven packages:

$ apt-cache search maven
....
libxmlbeans-maven-plugin-java-doc - Documentation for Maven XMLBeans Plugin
maven - Java software project management and comprehension tool
maven-debian-helper - Helper tools for building Debian packages with Maven
maven2 - Java software project management and comprehension tool

The Maven package always comes with latest Apache Maven.

Run command sudo apt-get install maven, to install the latest Apache Maven.

$ sudo apt-get install maven

This will take a few minutes to download. Once downloaded you can verify by running the mvn -versionto verify your installation.

3. Installing Maven on Mac OS X

To install Maven on Mac OS X operating system, download the latest version from the  Apache Maven site, select the Maven binary tar.gz file, for example: apache-maven-3.3.9-bin.tar.gz.

Extract the archive to your desired location.

3.1. Adding Maven to the Environment Path

Open the terminal and switch to the directory where the files were extracted to and then login as Super – User.

Remove the tar.gz archive:

rm Downloads/apache-maven*bin.tar.gz

Fix the permissions:

chown -R root:wheel Downloads/apache-maven*

Switch the Maven contents:

mv Downloads/apache-maven* /opt/apache-maven

Archive the Admin session:

exit

Add Maven binaries to the path and append.

nano $HOME/.profile
export PATH=$PATH:/opt/apache-maven/bin

Ctrl+x to Save and Exit from “nano”.

To load the new set up run:

bash

To test the new installation run:

mvn -version

You are now ready to use Maven on your Mac OS X.

3.2. HighSierra Compatibility

For HighSierra users, we’ll need to additionally add Maven binaries to the path and append.

1
2
nano $HOME/.bashrc
export }}{{PATH=$PATH:/opt/apache-maven/bin

Ctrl+x to save and exit from “nano”.

To load the new set up run: bash

 

4. 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:

    <localRepository>~//maven/repo/</localRepository>
    

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.

    During the first full run, testing jars are installed in your local maven repository.

    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 26.549 s
    [INFO] Finished at: 2018-02-15T09:55:56-05:00
    [INFO] Final Memory: 86M/639M
    [INFO] ------------------------------------------------------------------------
    
  2. Maven is said to also act as project management tool because it can generate reports etc.build.xml

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

  3. After the first run, builds can skip tests with this additional parameter:mvn clean install -Dmaven.test.skip=true

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.

See

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

maven2-100156414-orig

Dependencies

Mirror

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

The Maven Central Repository provides a UI to
the mirror website where Maven pulls files from. In the US it is:

The UK mirror supports Europe:

Others:
http://repo.maven.apache.org/maven2/.meta/repository-metadata.xml

Build Profiles

http://maven.apache.org/guides/introduction/introduction-to-profiles.html