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
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"
To compile the project, run
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.
It is referencing one ore more submodules as highlighted in the below pom.xml
<?xml version=”1.0" encoding=”UTF-8"?>
<name>Multi Chapter Simple Parent Project</name>
Child pom.xml will have a reference to the parent
compile is the default scope; all dependencies are
compile-scoped if a scope is not supplied.
compiledependencies are available in all classpaths, and they are packaged.
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 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-scoped dependencies are not required during the normal operation of an application, and they are available only during test compilation and execution phases.
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).