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.
- 1 1. Installing Maven on Windows
- 2 2. Installing Maven on Linux
- 3 3. Installing Maven on Mac OS X
- 4 4. Change Default Configurations
- 5 Using Maven
- 6 Mirror
- 7 Build Profiles
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:
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:
with M2_Home path corresponding with the location of your extracted Maven files.
Now append the M2 variable to the system path:
Finally, verify if Maven has been added by running:
The output should be as follows:
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:
The Maven package always comes with latest Apache Maven.
Run command sudo apt-get install maven, to install the latest Apache 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:
Fix the permissions:
Switch the Maven contents:
Archive the Admin session:
Add Maven binaries to the path and append.
Ctrl+x to Save and Exit from “nano”.
To load the new set up run:
To test the new installation run:
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.
Ctrl+x to save and exit from “nano”.
To load the new set up run: bash
4. Change Default Configurations
- 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.
There is a settings.xml file containing:
Maven allows use of a central maven repository.
The pom.xml (Project Object Model) file describes project dependencies which Maven resolves by downloading them.
- 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] ------------------------------------------------------------------------
- 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.
- After the first run, builds can skip tests with this additional parameter:mvn clean install -Dmaven.test.skip=true
Maven lifecycle phases
Here are sub-commands for mvn:
|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.|
|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|
|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.|
Maven plug-in goals by lifecycle phase
Each unit of execution work in Maven is called a goal.
Maven C/C++ Plugin http://duns.github.io/maven-nar-plugin/
Maven plug-ins by lifecycle phase
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: