Maven Notes
Maven Notes
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
===================================================================================
================================
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
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).
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
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}
===================================================================================
=========================================
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.
===================================================================================
========================================
Directory structure of Maven Project:
===================================================================================
========================================
1. pom.xml: Main instruction file which downloads and resolve dependencies
automatically.
11 directories, 4 files
[root@ip-172-31-37-150 server]#
===================================================================================
========================================
Explaination about pom.xml?
===================================================================================
========================================
XML : Extended markup language
POM : Project object model
======================================
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