Maven RC Notes
Maven RC Notes
What is Maven ?
Common usage of Maven
1) as a Build Tool (Similar to Ant)
2) as a Project Management Tool
Common problems and activities
1) Multiple Jars : For example if we are using Hibernate and Spring , we will need
to include all the Jars.
Sometimes we don't know the name of the Jars or if all the Jars
are included etc.
Maven helps us in this.
2) Dependencies and Versions : Suppose a jar has a dependency on another jar.
we need to make sure to close all the dependencies (i.e supply all dependant jars ).
Due to different versions , we need to match the right version of dependent jars.
3) Project Structure : We create a directory Structure for our project . For example
, for a web project we need WEB-INF, libraries
4) Build Publish and Deploy.
Maven Setup :
1. Go to maven.aapache.org and download the binary zip file and extract it.
2. Now we need to setup two environment variables:
1) M2_HOME = Path where you have extracted Maven
C:\Users\sravi\Documents\Java\apache-maven-3.0.4-bin\apachemaven-3.0.4
2) Add the path where Maven's bin is located to the path variable
set path=C:\Users\sravi\Documents\Java\apache-maven-3.0.4bin\apache-maven-3.0.4\bin;%path%
3. To test if maven is working file , execute a simple maven command
for example mvn --version
You should see the below output
C:\Users\sravi>mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 03:44:56-0500)
Maven home: C:\Users\sravi\Documents\Java\apache-maven-3.0.4-bin\apachemaven-3.
0.4\bin\..
Java version: 1.7.0_07, vendor: Oracle Corporation
1 | Page
2 | Page
4) You will see a folder called src and a file pom.xml (pom stands for project object
model)
5) cd src ; you will see two folders main and test
src/main/java/org/rajesh/javabrains
org/rajesh/javabrains is the package name we specified in step 9 .
Lets look at pom.xml
<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>org.rajesh.javabrains</groupId>
<artifactId>MavenTestApp</artifactId> -- Maven uses it to refer to this application
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> --- means that application will be a jar file
(it is default)
<name>MavenTestApp</name> ---Name we use to refer to application , Can be
different than Artifact
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId> --if junit is dependent on other jars we dont have to
mention Maven will
<artifactId>junit</artifactId> -- take care of it
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Compiling using Maven
1. Inorder to compile using Maven, we need to be in the directory where
pom.xml is there
mvn compile
2. Maven takes care of downloading all the related dependencies that we mentioned
in the pom.xml
3 | Page
3. Once the compilation is done we need to package it by using the command mvn
package
It will create a jar file (in this case ) under the target directory
(MavenTestApp-1.0-SNAPSHOT.jar)
It will create a target directory .
Maven would also run the junit Testcases .
Execute the class
java -cp target/ MavenTestApp-1.0-SNAPSHOT.jar org.rajesh.javabrains.App
Maven Basics
A repository in Maven is used to hold build artifacts and dependencies of varying types.
Maven Repository(on
Internet)
Arche Type
Info
Dependen
cyInfo
Maven
Document Structure
jar
s
pom.xml
When we compile , using mvn compile , maven reads the pom.xml to get the
dependency information . It then connects to the "Dependency Info" in the Maven
Repository and downloads all the Jar's required and then compiles all Java Classes .
When we request something from Maven , it first checks the local repository to see ,
if it has what it needed , if it does not find then it connects to online repository and
download the needed artifacts to the local repository and then uses them .
4 | Page
6 | Page