Maven 829
Maven 829
Maven 829
txt
============
Build Tools
============
JAR ---> Java Archieve ---> It is package format for java standalone application
WAR ---> Web Archieve ---> It is package format for java web applications
-> Instead of doing the above build steps manually, we can take the help of Build Tools to
automate that process.
1) Ant (Outdated)
2) Maven
3) Gradle
===========
Maven
===========
-> Maven is a free and open source software given by Apache Organization
========================
What we can do using maven
========================
-> To develop one java project we will use several frameworks like spring, hibernate etc along
with Java
-> We need to download those frameworks and we should add to our java project
-> These frameworks we are using in our project are called as our project dependencies
-> Instead of we are downloading dependencies, we can tell to maven s/w to download dependencies
Note: Required dependencies we will add in "pom.xml" file then maven s/w will download them
-> When we create maven project then pom.xml file will be created automatically
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 1/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
4) We can package java project as jar or war file using maven
======================
Maven Installation
======================
3) Set Path for JAVA (Go to System Env Variables -> Env Variables -> System Variables -> Select
Path and Click on Edit then add JDK path like below)
6) Extract Maven Zip file -> Copy extracted maven folder and paste it in "C" drive
MAVEN_HOME = C:\apache-maven-3.8.5
Path : C:\apache-maven-3.8.5\bin
9) Open Command Prompt and verify Maven Installaton using below command
======================
Maven Terminology
======================
archetype
groupId
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 2/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
artifactId
packaging
version
-> packaging represents how we want to package our java application (jar or war)
============================================
Creating stand-alone application using maven
============================================
01-Maven-App
- src
- main
-java
- test
-java
- pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 3/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
============================================
How maven will downoad dependencies
============================================
-> Maven will download dependencies using repository
1) Central Repository
2) Remote Repository
3) Local Repository
-> every company will maintain their own remote repository (Ex: Nexus, JFrog)
-> When we add dependency in pom.xml, maven will search for that dependency in local repository.
If it is available it will add to project build path.
-> If dependency not available in local repository then maven will connect to Central Repository
or Remote Repository based on our configuration.
Note: By default maven will connect with central repository. If we want to use remote reposiotry
then we need to configure remote repository details.
Note: Every software company will maintain their own remote repositories (Ex: Nexus / JFrog)
=================================
Configuring Remote Repository
=================================
<repositories>
<repository>
<id>id</id>
<url>jfrong-repo-url/</url>
</repository>
</repositories>
===========
Maven Goals
===========
-> To perform project build activities maven prorvided several goals for us
clean
compile
test
package
install
-> compile goal is used to compile project source code. Compiled code will be stored in target
folder
compile
.java ------------> .class
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 4/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
-> test goal is used to execute unit test code of our application (junit code)
-> package goal is used to generate jar or war file for our application based on packaging type
available in pom.xml file.
-> install goal is used to install our project as a dependency in maven local repository.
Note: Every maven goal is associated with maven plugin. When we execute maven goal then respective
maven plugin will execute to perform the operation.
=================================
Creating web application using maven
=================================
>> mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=in.ashokit -
DartifactId=flipkart_app -DinteractiveMode=false
===================================================
Working with Maven Using IDE (STS / Eclipse
================================================
-> Every IDE will have Maven plugin by default (no need to install)
-> Right Click on Project -> Build Path -> Configure Build Path -> Libraries -> Select JRE -> Edit
-> Configure JDK
Note: If JDK not displaying, then add JDK using 'Installed-JRES' button (we have to select jdk
folder from our Program Files where we have installed java)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
-> Right click on Project -> Run As -> Maven Build -> Enter Maven Goals (Ex: clean package)
=========================
What is Exclusion In Maven
=========================
-> Exclusion is used to remove un-wanted child dependencies from build path
Ex: When we add 'spring-context' we are getting other dependencies also like 'spring-core',
'spring-beans', 'spring-aop' etc..
-> I want to use 'spring-context' but i don't want 'spring-aop' in build path then we can exclude
'spring-aop' from 'spring-context' like below
<dependency>
<groupId>org.springframework</groupId>
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 5/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
===================================
Maven Multi - Module Project
==================================
1) User Module
2) Admin Module
3) Reports Module
5) Payment Module
6) Products Module etc....
-> It is not recommended to develop all the modules code in single project. Maintenence will
become very difficult
-> In Maven Multi Module, We will create project with Parent - Child relation
-> When we execute Maven Goal in Parent Project then it will execute that goal in all the child
projects also.
============================================
Creating Maven Multi Module Project in IDE
==============================================
2) Right Click on the Project -> New -> Select Others -> Search For Maven Module -> Create the
Module
3) After Module got created, check parent project pom.xml and child project pom.xml
===========================================
Parent Project pom.xml looks like below
==========================================
<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 http://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.flipkart</groupId>
<artifactId>Flipkart_App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ADMIN</module>
<module>REPORTS</module>
</modules>
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 6/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
</project>
=========================================
Child Project pom.xml looks like below
==========================================
<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 http://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.flipkart</groupId>
<artifactId>Flipkart_App</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>REPORTS</artifactId>
</project>
-> Right click on Parent Project -> Run as -> Maven Build -> Enter Goals and Execute.
==========================================
What is Dependency Scope in the Maven
==========================================
-> 'Scope' represents when that dependency should be used by Maven in Build Process
compile : This is the default scope, used if none is specified. Compile dependencies are available
in all classpaths of a project. Furthermore, those dependencies are propagated to dependent
projects.
provided : This is much like compile, but indicates you expect the JDK or a container to provide
the dependency at runtime. For example, when building a web application for the Java Enterprise
Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope
provided because the web container provides those classes. A dependency with this scope is added
to the classpath used for compilation and test, but not the runtime classpath. It is not
transitive.
runtime : This scope indicates that the dependency is not required for compilation, but is for
execution. Maven includes a dependency with this scope in the runtime and test classpaths, but not
the compile classpath.
test : This scope indicates that the dependency is not required for normal use of the application,
and is only available for the test compilation and execution phases. This scope is not transitive.
Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for
non-test libraries such as Apache Commons IO if those libraries are used in unit tests
(src/test/java) but not in the model code (src/main/java).
system: This scope is similar to provided except that you have to provide the JAR which contains
it explicitly. The artifact is always available and is not looked up in a repository.
import : This scope is only supported on a dependency of type pom in the <dependencyManagement>
section. It indicates the dependency is to be replaced with the effective list of dependencies in
the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a
scope of import do not actually participate in limiting the transitivity of a dependency.
==============================
What we have learnt in Maven
==============================
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 7/8
4/9/24, 12:32 AM ashokitech.com/uploads/notes/1744759701_1703303520.txt
6) Standalone Application Creation using Command Prompt & IDE
7) Web Application Creation using Command Prompt & IDE
8) Maven pom.xml file
9) How to add dependencies in 'pom.xml' file (www.mvnrepository.com)
10) Maven Goals Execution in command prompt & in IDE
11) Maven Repositories (local, remote, central)
12) Working with Remote Repository (Ex: Nexus)
13) Maven Dependency Exclusion
14) Maven Multi Module Project
15) Maven Dependency Scopes
https://ashokitech.com/uploads/notes/1744759701_1703303520.txt 8/8