Start with Maven
Installation and Setup
1. Download Maven . Click here
2. Set environment variables as shown in the screenshot
1. Add maven path in the Path Variable
2. Create new system variable MAVEN_OPTS
-Xms256m -Xmx512m


3. Validate if maven is installed successfully. Run mvn — version

Create Maven Project
- Create a directory on your system
- Run mvn archetype:generate ( This is used to setup a package structure for particular project type)
- There are different number for different type of project structure. 1092 — is the sample Maven project
- groupId — package structure
- artifactID — Project Name
- This command has created a java project directory structure with pom.xml file


6. pom.xml file
<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.test</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Build LifeCycle
- validate
- compile
- test
- package
- install
- deploy
To compile the project, run
mvn compile
To generate a jar, run
mvn package ( This will always do the validate ,compile,test and then package) This applied to all build life cycle, it always execute in the order only whichever command you run. for example mvn compile, only phases up to and including that phase will execute.
Setup of MultiModule Project
We have 2 modules (simple-weather and simple-webapp). There is one parent pom.xml and two which are module specific inside both the folders. Here the packaging tag doesnt have a jar/war, instead of it is pom.

Parent POM.xml
It is referencing one ore more submodules as highlighted in the below pom.xml
<?xml version=”1.0" encoding=”UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>parent</artifactId>
<version>0.8-SNAPSHOT</version>
</parent>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<name>Multi Chapter Simple Parent Project</name>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
Child pom.xml will have a reference to the parent
<parent>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>0.8-SNAPSHOT</version>
</parent>
Dependency Scopes
compile
compile
is the default scope; all dependencies are compile
-scoped if a scope is not supplied. compile
dependencies are available in all classpaths, and they are packaged.
provided
provided
dependencies are used when you expect the JDK or a container to provide them. For example, if you were developing a web application, you would need the Servlet API available on the compile classpath to compile a servlet, but you wouldn’t want to include the Servlet API in the packaged WAR; the Servlet API JAR is supplied by your application server or servlet container. provided
dependencies are available on the compilation classpath (not runtime). They are not transitive, nor are they packaged.
runtime
runtime
dependencies are required to execute and test the system, but they are not required for compilation. For example, you may need a JDBC API JAR at compile time and the JDBC driver implementation only at runtime.
test
test
-scoped dependencies are not required during the normal operation of an application, and they are available only during test compilation and execution phases.
system
The system
scope is similar to provided
except that you have to provide an explicit path to the JAR on the local file system. This is intended to allow compilation against native objects that may be part of the system libraries. The artifact is assumed to always be available and is not looked up in a repository. If you declare the scope to be system
, you must also provide the systemPath
element. Note that this scope is not recommended (you should always try to reference dependencies in a public or custom Maven repository).