Maven
-
Maven provides java and java based languages developers a way to managed
- Builds
- Documentations
- Dependencies
- Releases
- Distributions
-
Convention over Configuration:
- Maven uses Convention over configuration & developers are not required to creat the build process themselves
Item Default Source Code ${basedir}/src/main/java Resources ${basedir}/src/main/resources Tests ${basedir}/src/test Compiled byte code ${basedir}/target/classes Distributable (jar/war/ear) ${basedir}/target POM ${basedir}/pom.xml -
Installing Maven:
- Pre-reqs:
- Java with 1.7 or above (Verify if the JAVA is installed
java -version)
- Java with 1.7 or above (Verify if the JAVA is installed
- To install Maven
- Package managers => apt, yum, brew (mac), choco(windows)
- Manual process => download tar/zip file depending on your OS Refer Here and configure everything on your own
- Manual installation steps
- Install suitable version of java
sudo apt update && sudo apt install openjdk-11-jdk -y - Set Java Environment: Set JAVA_HOME environment variable
- on ubuntu instances => /usr/lib/jvm/java-11-openjdk-amd64
- on redhat/centos instances => /usr/lib/jvm/jre
- We need to create enviornment varaible
export JAVA_HOME=<Java Path> - Open the file
~/.bashrcand add export statement at the last
- Try to logout and login or
source ~/.bashrc
- Download the maven and extract it to /usr/local
cd /tmp wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz tar -xvzf apache-maven-3.8.4-bin.tar.gz sudo cp -r apache-maven-3.8.4/ /usr/local- Now we need to add two environment variables
- M2_HOME => This represent the maven directory
- PATH => Add the path to maven binary

- Note: The configuration which we have done is for the particular user, if you want the same configuration for all users try adding export statements to /etc/enviornment
- Install suitable version of java
- Pre-reqs:
Maven- POM (Project Object Model)
- This is fundamental unit of work in Maven which is an xml file that resides in projects base directory
- There should be a single pom.xml file for every project.
- All pom files require the project elements and three mandatory fields
- groupId
- artifactId
- version
- Projects notation is
groupId:artifiactId:version
- All pom files require the project elements and three mandatory fields
- Minimal Elements of POM
- Project root: This is schema setting for the maven project
- Model version: model version of maven and it is always 4.0.0
- groupId: This is an id of project’s group. Generally organziation name
- artifactId: This is Id of the project. Generally name of the project
- version: version of the project. If the Version Contains SNAPSHOT understanding is project version is still under development. If it doesn’t contain it is expected to be Ready for Release, some organization use RELEASE in version to represent the release.
- Super POM:
- This is Maven’s default POM. All the POMs inherit from a parent or default
- Goals: A Build lifecycle is a well defined sequence of phases, in which the goals are execute
| Phase | Comments |
|---|---|
| prepare-resources | Resource copying can be customized in this phase |
| validate | Validates if the project & other info is available or not |
| compile | Source code is compile and if there are errors they would be shown |
| test | executes all the tests written in test folder |
| package | This generates jar/war package |
| install | This phase installs the pom and package in local repository |
| deploy | Copies the final package to remote repository |
| clean | clean will remove the target folder |
| site | which creates the documentation |
- All goals of maven
validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy
Activities
- Sample1 : Refer Here for the git repository
- Refer Here for the initial set of changes
- Lets generate Super POM
mvn help:effective-pom
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!-- -->
<!-- Generated by Maven Help Plugin on 2021-11-30T03:11:26Z -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -->
<!-- -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!-- -->
<!-- Effective POM for project 'com.qt.devops:sample1-app:jar:1.0-SNAPSHOT' -->
<!-- -->
<!-- ====================================================================== -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qt.devops</groupId>
<artifactId>sample1-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>/home/qtdevops/mavenexamples/sample1/src/main/java</sourceDirectory>
<scriptSourceDirectory>/home/qtdevops/mavenexamples/sample1/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>/home/qtdevops/mavenexamples/sample1/src/test/java</testSourceDirectory>
<outputDirectory>/home/qtdevops/mavenexamples/sample1/target/classes</outputDirectory>
<testOutputDirectory>/home/qtdevops/mavenexamples/sample1/target/test-classes</testOutputDirectory>
<resources>
<resource>
<directory>/home/qtdevops/mavenexamples/sample1/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>/home/qtdevops/mavenexamples/sample1/src/test/resources</directory>
</testResource>
</testResources>
<directory>/home/qtdevops/mavenexamples/sample1/target</directory>
<finalName>sample1-app-1.0-SNAPSHOT</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>/home/qtdevops/mavenexamples/sample1/target/site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>/home/qtdevops/mavenexamples/sample1/target/site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>/home/qtdevops/mavenexamples/sample1/target/site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>/home/qtdevops/mavenexamples/sample1/target/site</outputDirectory>
</reporting>
</project>
- Now execute
mvn compile
- Now execute
mvn package
- Now execute
mvn clean
- We can combine goals and also run
mvn clean compile
- Now lets build some opensource project using maven Refer Here
