0% found this document useful (0 votes)
374 views6 pages

Maven RC Notes

Maven is a build automation tool used to manage Java projects and their dependencies. It handles tasks like compiling code, running tests, packaging artifacts, and managing dependencies. When using Maven, it will generate a project structure with directories for source code and tests. It also creates a pom.xml file which contains project metadata and dependencies. When compiling or packaging code, Maven reads pom.xml to download any required dependencies from online repositories. It handles matching versions and ensuring all dependencies are included.

Uploaded by

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

Maven RC Notes

Maven is a build automation tool used to manage Java projects and their dependencies. It handles tasks like compiling code, running tests, packaging artifacts, and managing dependencies. When using Maven, it will generate a project structure with directories for source code and tests. It also creates a pom.xml file which contains project metadata and dependencies. When compiling or packaging code, Maven reads pom.xml to download any required dependencies from online repositories. It handles matching versions and ensuring all dependencies are included.

Uploaded by

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

Maven

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

Java home: C:\Program Files\Java\jdk1.7.0_07\jre


Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
Maven uses repositories(for build tasks) . There are repositories available
online(internet) and maven talk to these repositories , so we need to be connected
to Internet while working with Maven .
Using Maven
First advantage Maven provides regarding Project Structure
1) create a directory
mkdir maventest
2) cd maventest
3) mvn archetype:generate
Maven will download all the required plugin's and a prompt is displayed to
select the arche type
106 is default .
4) select the default and enter
5) select the default version and enter
6) Define value for property 'groupId': :
This would be similar to package name that you would create.
give org.rajesh.javabrains
7) Define value for property 'artifactId': :
this is analogous to a class name while group id is analogous to a
package name
give MavenTestApp. A directory with the ArtifactId name specified will
be created.
ArtifactId will be the name of the Jar or War file .
The group Id and artifact Id are defined for the complete application .
8) Define value for property 'version': 1.0-SNAPSHOT: :
select default
9) Define value for property 'package': org.rajesh.javabrains: :
Enter
You can specify your package name here , the default is the group id
10) Confirm
So what has Maven Done
1) It has created a directory with the ArtifactId you specified : MavenTestApp
2) cd MavenTestApp
3) dir

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 installed on our machine talks to


the repository.

Maven Repository(on
Internet)

Arche Type
Info

Dependen
cyInfo

when we excuted mvn archetype:genrate


maven connects to the Maven repository and get s
the arche type info and generates the dir structure.

Development Environment(our Machine)

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

mvn archetype:generate creates the following


1) Folder Structure
2) pom.xml
When you execute mvn archetype:generate you will be asked to select the following
options
1.archetype : This represents the type of application you want to create . (for
examle spring-jsfhibernate) Every archetype has an
associated number . you need to enter the number .
2. GroupId :
3.ArtifactId : this is the application name ; for webapplication -- you will get a
artifactId.war
for enterprise application -- .ear
for simple application --.jar
4.Version :
5.Package : Name of the package to place your source code .What packages that
class should belong to .
One group id can contain more than one Artifact Id .
A combination of groupid ,artifactid and version can be used to identify an artifact .
--------------------------------------------------------------------------------------------------------------------pom.xml has the details about the artifact that is being produced and it has list of
dependencies .
since an artifact (like a jar or war file) is uniquely identified by a combination of
groupId
Maven Co-ordinates
AtrifactId
VersionId
In the dependency section for each dependency we have to provide the above 3
information.
Once we create our project directory structure , we write out code and also update
the pom.xml with the dependency information . In order to compile the code or to
package the code (i.e to a .jar or a .war file ) you go to the directory where pom.xml
is stored and execute the mvn compile or mvn package commands .
Maven Build Phases
1. validate
2. compile
3. test -- runs the junit test cases.
4. package --creates the .jar or .war files .
5 | Page

5. install (installs the artifact in local repository)


6. deploy (installs/publishes the artifact in online repository).
when you execute a phase such as mvn package , all the preceding phases are
executed automatically .
So in this case validate compile and test phases are run automatically.
when you package you application , maven creates a directory named target and
places the jar or war file in it .
How to add a dependency ?
mvn clean will delete the target folder that mvn created.
Once you

6 | Page

You might also like