0% found this document useful (0 votes)
64 views8 pages

Maven Notes

The document provides an overview of Maven including its architecture, goals, commands, installation process, and directory structure. Maven is a build tool that resolves dependencies, compiles code, runs tests, packages artifacts, and integrates with tools like SonarQube and JFrog. The document explains how Maven goals like validate, clean, compile, test, package, install, and deploy can be used to build a project from the command line.

Uploaded by

akhil reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views8 pages

Maven Notes

The document provides an overview of Maven including its architecture, goals, commands, installation process, and directory structure. Maven is a build tool that resolves dependencies, compiles code, runs tests, packages artifacts, and integrates with tools like SonarQube and JFrog. The document explains how Maven goals like validate, clean, compile, test, package, install, and deploy can be used to build a project from the command line.

Uploaded by

akhil reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Overview of Maven

Maven Architecture
Maven Goals
Maven commands
Installation of Maven
Clone github project and execute maven commands
Directory structure of Maven installation
Directory structure of Maven Project
Explaination about pom.xml?
Creating maven project from scratch from built-in command?
===================================================================================
=========================================
Maven is a build tool which resolves/downloads all the dependencies and provides
easy way to compile, test, package the code and build artifacts.
Maven also helps in verifying the java code using the test cases via build-in maven
command.
Maven also supports inetgration with various tools like Sonarqube, tomcat, Jfrog
etc
Maven allows us to push the code to sonarqube for checking the code quality, code
errors(bugs), code virus(vulnerabilities), duplication of code etc
Maven supports in upload/download of artifacts to store in Jfrog artifactory in
proper format.
Maven also provides deploying the artifacts(war) from maven command line to
Tomcat(web/application) for launching the applications.

==========================================================================
Why Maven?
==========================================================================
Developers -> write/develop the code
-> Push code to github

Ecommerce website:
* login feature
* logout feature
* add to cart feature
* product catalog feature
* Payment methods
* Acknowledgment feature

1. login feature: login.java


login-security.java
login-password.java

a. Install java in local system:


java> javac login.java -> compilation -> Generates login.class(1's & O's) ->
Hardware process
java> java login -> Intepretation -> output.

b. Dependencies: Dependencies like .jar/.war./zip etc. Before hand, developers


needs to downlaod manually all the dependencies in one directory.
Exact Compartible jar files are required for resolving
dependencies.

eg: httpd jar


-> httpd jar has some sub-dependencies httpd-tools,httpd-security etc

c. Test cases: loginTest.java


loginTest-security.java
loginTest-password.java
Framework like junit,munit for writing the test cases for verifying
the .java files line by line.

d. Packaging(bundling) the files/directories/images/jars and generate artifacts.


eg:login.jar, logout.jar, ecommerce-website.war/jar

===================================================================================
================================
Maven Features :
===================================================================================
================================
1. Maven have proper directory structure which is automatically.
eg : code kept in directory -> src directory
files/images kept in directory -> resources directory
output kept in directory -> target directory
2. Maven has built-in commands for creating projects.
3. Maven has instruction file which is called pom.xml( project object model) -
which download/resolves all the jars from Maven central repository automatically.
4. Maven provides set of goals which are called plugin for performing all build
operations like compilation, running test cases, packaging, pushing code to
sonarqube, uploading to nexus, deploying to tomcat etc.

===================================================================================
==========================================Maven Architecture:
===================================================================================
==========================================
1. Local Repo : /root/.m2/repository

2. Central Repo : Public Repository - https://mvnrepository.com/repos/central

3. Remote Repo : Private Repository - Nexus/onedrive/cloud/dropzone etc

4. Site : Documentation
===================================================================================
=========================================
Maven Goals: Goals are plugins(.jar) which are used for performing all the maven
operations.
===================================================================================
=========================================
1. validate: This goal performs some checks to verify if all the required things
are avilable before any execution takes place.
eg: pom.xml
src directory - .java files

2. clean: Clean goal make sures it deletes the output directory before re-running
the execution again.
The output directory is called as target directory which contains all the
outputs like classes directory, final artifact, generate reports etc

3. compile: Compile goal generates an .class files out of the .java files for
hardware to process and with compile goal, we can also check the syntax errors.
eg: login.java -> target/classes/login.class(1's & 0's)

4. test: This goal is used for running the test cases for line by line verification
on .java files which are performed by using testing framerworks like Junit, Munit
etc
eg: login.java -> Test case file loginTest.java

5. package: Package goal is used for generating the final artifacts out the
compiled java files.
eg: login.jar, logout.jar, ecommcerce-website.war etc

6. install: This goal will copy the final artifacts(.jar/.war) from target
directory(target) into local repository(/root/.m2/repository).

7. deploy: This goal is used for pushing/uploading/downloading the artifacts


into/from Remote repository(Jfrog artifactory) in proper format.
===================================================================================
======================================
Maven commands:
===================================================================================
========================================
cd project => pom.xml
1. mvn valdiate => validate
2. mvn clean => validate, clean
3. mvn compile => validate, clean, compile
4. mvn test => validate, clean, compile, test
5. mvn package => => validate, clean, compile, test, package
6. mvn install => validate, clean, compile, test, package, install
7. mvn deploy => validate, clean, compile, test, package, install, deploy

mvn clean(plugin):clean install


mvn clean deploy
mvn package deploy

Maven integration with SonarQube, Tomcat and Jfrog


* mvn sonar:sonar
* mvn tomcat:deploy
* mvn deploy
===================================================================================
========================================
Installation of Maven :
===================================================================================
========================================
Infrastructure requirement :
Instance OS : Redhat OS - 1 Machine
Instance type: t2.micro ( 1 cpu & 1 GB RAM)
Harddisk : 10 GB

1. Pre-requsite: Install java ( java -> JVM(java virtual machine) -> create
processid(7373)-> share process id to Kernel -> kernel will share the required cpu
& ram to Maven for processing.

sudo su
yum install java-11* -y
yum install wget -y
java -version

2.Download the maven from offical


website:(https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/)
cd /opt
wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-
3.6.3-bin.tar.gz
tar -xvf apache-maven-3.6.3-bin.tar.gz
mv apache-maven-3.6.3 maven
rm -rf apache-maven-3.6.3-bin.tar.gz

Note: verify mvn --version (bash: mvn not found)


-> cd /opt/maven/bin
./mvn --version ( this gives output)
echo ${PATH}

3.Set the Path for Maven to run from any location:

vi /etc/profile.d/maven.sh
export MAVEN_HOME=/opt/maven
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export PATH=${JAVA_HOME}/bin:${MAVEN_HOME}/bin:${PATH}

source /etc/profile.d/maven.sh // load files and make change reflect in OS.

4. Verify maven version

mvn --version //run from any location


echo $PATH
===================================================================================
========================================
Clone github project and execute maven commands:
===================================================================================
========================================
Github link : https://github.com/techiescorner/..........

yum install git -y


git clone https://github.com/techiescorner/...........git

===================================================================================
=========================================
Directory structure of maven installation:
===================================================================================
=========================================
1. /opt/maven/conf - settings.xml -> Profile details like user, password, URL,
plugin, reponames etc
logging -> Developer will the log levels - Info, debug, warn,
error,system outputs etc
2. /opt/maven/bin - binaries(executable) - mvn commmand line utility.

3. /opt/maven/lib - libraries - Jar file for maven to function properly

4. /opt/maven/boot - Boot files for maven perform the operations

===================================================================================
========================================
Directory structure of Maven Project:
===================================================================================
========================================
1. pom.xml: Main instruction file which downloads and resolve dependencies
automatically.

2. src directory: src/main/java -> .java files


src/test/java -> Test.java files

3. target directory: This contains all outputs of execution


* .jar/.war files
* classes directories -> .class file
* generated reports -> test cases results -> .txt/.xml

4. resource directory: This contains generic files like


.txt/images/logos/.xml/files etc

[root@ip-172-31-37-150 server]# ls -lrt


total 4
-rw-r--r--. 1 root root 1255 Feb 27 01:52 pom.xml
drwxr-xr-x. 5 root root 42 Feb 27 01:52 src
[root@ip-172-31-37-150 server]# tree
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── Greeter.java
├── site
│ └── apt
│ └── index.apt
└── test
└── java
└── com
└── example
└── TestGreeter.java

11 directories, 4 files
[root@ip-172-31-37-150 server]#

===================================================================================
========================================
Explaination about pom.xml?
===================================================================================
========================================
XML : Extended markup language
POM : Project object model

Main instruction file which downloads and resolve dependencies automatically.


mvn command first searches for pom.xml for before any execution takes place.

GAV parameters: G - groupId - community or group name to which the artifact


belongs
A - artifactId - Acutally name of the artifact
V - version - version of the artifact
- SNAPSHOT: Developement & Testing phase
eg: 1.0-SNAPSHOT, 2.0-SNAPSHOT etc
- RELEASE: Stable code
ef: 1.0, 2.0, 3.0 etc

======================================
vi pom.xml
======================================
<project xmlns= URL for maven central repository >

<groupId>com.example.maven-project</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<scope>test</scope>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
===================================================================================
========================================
Creating maven project from scratch from built-in command?
===================================================================================
========================================
command: mvn archetype:generate

Developers will raise ticket for creating new maven project:

1. Project number: 2104


2. groupId: com.org.sprintbootapplication
3. artifactId: microservice-backend
4. version: 1.0-SNAPSHOT
5. package: jar

[root@ip-172-31-37-150 project]# mvn archetype:generate


Choose a number or apply filter (format: [groupId:]artifactId, case sensitive
contains): 2104:
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
7: 1.3
8: 1.4
Choose a number: 8:
Downloading from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
quickstart/1.4/maven-archetype-quickstart-1.4.pom
Downloaded from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
quickstart/1.4/maven-archetype-quickstart-1.4.pom (1.6 kB at 37 kB/s)
Downloading from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
bundles/1.4/maven-archetype-bundles-1.4.pom
Downloaded from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
bundles/1.4/maven-archetype-bundles-1.4.pom (4.5 kB at 144 kB/s)
Downloading from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
quickstart/1.4/maven-archetype-quickstart-1.4.jar
Downloaded from central:
https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-
quickstart/1.4/maven-archetype-quickstart-1.4.jar (7.1 kB at 137 kB/s)
Define value for property 'groupId': com.org.sprintbootapplication
Define value for property 'artifactId': microservice-backend
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.org.sprintbootapplication: : jar
Confirm properties configuration:
groupId: com.org.sprintbootapplication
artifactId: microservice-backend
version: 1.0-SNAPSHOT
package: jar
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-
archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.org.sprintbootapplication
[INFO] Parameter: artifactId, Value: microservice-backend
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: jar
[INFO] Parameter: packageInPathFormat, Value: jar
[INFO] Parameter: package, Value: jar
[INFO] Parameter: groupId, Value: com.org.sprintbootapplication
[INFO] Parameter: artifactId, Value: microservice-backend
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Project created from Archetype in dir: /opt/project/microservice-backend
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:06 min
[INFO] Finished at: 2024-02-27T02:24:01Z
[INFO] ------------------------------------------------------------------------
[root@ip-172-31-37-150 project]#
===================================================================================
========================================
Once project is created, upload project contents to new github repository:
===================================================================================
========================================
1. Create an repository in github

2. clone the repository to machine.

3. Copy the contents of the maven project(microservice-backend) to new github


repository

4. Add changes to source control and commit changes


5. Push the changes to github remote repository
===================================================================================
=========================================

You might also like