To check Maven version:
mvn -- version
To create a project:
Maven will download all required maven programs and prompt for the project type
mvn archetype:generate
Maven will prompt for:
archetype - sample archetype number: (webpapp, restful, ejb, etc)
version - choose the archetrype version:
groupId - similar to package name - ex: com.volatileMemoryFix
artifactID - applicationName - ex: MyMavenTest
versionNumber - leave as default
Maven will create a project / folder called MyMavenTest.
Below is the folder structure:
MyMavenTest
|_pom.xml
|_src
|_main
|_java
|_com
|_volatileMemoryFix
|_App.java
|_test
|_java
|_com
|_volatileMemoryFix
|_AppTest.java
Checking the pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"This project depends on jUnit (default dependency)and Maven will take care of all the dependencies (download all the necessary jars)
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.defaultMaven</groupId>
<artifactId>DefaultMavenProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DefaultMavenProject</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>
To compile this project (java classes)
cd MyMavenTest
mvn compile
(Maven will download all the dependencies mentioned in pom.xml and compile all the java class. It will take longer in the first time, given Maven will download all the necessary libraries)
To package the application in a jar file:
mvn package
mvn package will also run all the test cases.
Maven Build
Maven has a build life cycle: (not comprehensive list). The subsequent phase will invoque the previous phases- validade (check if everything is in order (configuration, there is a pom.xml, etc)
- compile (download all the jars, it creates a target folder)
- test (all the tests will be executed)
- package (create a .jar for the application
- install (package artifacts in a local Maven repository / publish your jars in local repository)
- deploy (publish artifacts to remote repository)
Maven Dependencies
mvn clean delete all the dependencies downloaded previouslyAdding a dependency:
Search for "maven repository search" or go to http://mvnrepository.com
Searching for Slf4j (Logger library) . Click on the desired version and copy the following Maven tags into the <dependencies> session of the pom.xml file:
<dependency>
<groupId>org.grlea.log.adapters</groupId>
<artifactId>simple-log-sl4j</artifactId>
<version>1.7</version>
</dependency>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.grlea.log.adapters</groupId>
<artifactId>simple-log-sl4j</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
Running mvn compile will download all the jars that belong to the Logger library:
Web Application using Maven
- Run mvn archetype:generate
- Choose: 448: remote -> org.codehaus.mojo.archetypes:webapp-j2ee14 (-)
- Choose the version: default
- Choose the 'groupId': com.volatileMemoryFix
- Chose the 'artifactId': myWebj2ee14
- 'version' and 'package' can get their default values
- Confirm all the properties configuration.
This is the pom.xml file generated for this project:
<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.volatileMemoryFix</groupId>
<artifactId>myWebj2ee14</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <!-- -->
<name>myWebj2ee14</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- It has 2 dependencies: Servlets and jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- The compiler in Maven is a plugin, and the behavior of the compile plugin is configured in this session.This is the default compiler plugin. It has been added with custom configuration
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Running mvn compile will download all the dependencies mentioned in pom.xml file.
Running mvn package will create a .war file that can be deployed in a container (tomcat)
This is the package structure:
Maven Plugins
Plugins - extended functionality for Maven. - Maven has a modular architecture.<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
In order to use Generics, it will be necessary to change the source and target versions to 1.5.
Jetty Plugin
Add the following plugin to pom.xml<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</plugin>
.....
.....
</plugins>
</build>
Run the following command:
mvn jetty:run (plugin that will handle the command:command)
Eclipse Plugin for Maven
mvn eclipse:eclipse
.classpath and .project files will be created
In eclipse, just open myWebj2ee14 as an existing project and point to the root folder.
Right-click in the project myWebj2ee14, choose "Properties" and verify the M2_REPO Library is configured properly
Install "M2Eclipse" plugin
Run Maven comands :
Right click on pom.xml and run the required commands.
Search for dependencies / artifacts from the IDE:

