Вы находитесь на странице: 1из 11

Maven tutorial

1 Setting up maven
To setup, the following environment variables need to be set. Since maven is a java based tool, make
sure that java_home environment variable is already set before configuring maven.
In order to set an environment variable in windows: Right click on 'My Computer' -> Properties ->
Advanced system settings -> Environment variables
1.1 Set environment variable M2_Home
Set environment variable M2_Home; It should point to the base directory of Apache Maven as shown in
below screen.

1.2 Edit environment variable Path


2 Generating a new maven project (archetypes)
An archetype is a model or a template for a maven project. There are many archetypes registered in
maven central repository, which can be used to generate various types of projects (i.e. Simple Java
Project, Java Web Application Project, JSF-JPA-Hibernate Project, etc). In this exercise, we will generate a
simple java project with J-Units, and use it as a base line for our demos.
2.1 Maven command to generate a new project
Open command line, and browse to the folder where you want your project to be created. Use the
maven command ("mvn archetype:generate") to generate a new project using archetypes. As a result of
this command, maven will look up in central repository and download a list of available archetypes. This
is an interactive command, and you will be prompted to choose some settings, which are explained later
in this section.





2.2 Select an archetype id
Maven downloads a list of archetypes from the repository and prompts user to select the desired
archetype id. For this exercise, we will let the default be selected, which will create a simple java hello-
world application with unit tests. For the sake of curiosity, just have a look at the list of archetypes, to
know what different types of projects you can create.


2.3 Select archetype version
Select the version of archetype to generate the project. For this exercise, we will let the default be
selected.


2.4 Provide project co-ordinates
The following coordinates uniquely identify a project in maven repository.
group-id
artificat-id
version



2.5 Project created successfully


2.6 Browse the project created by above steps
Browse the project created by maven to get a feel of the folder structure. You will notice that all the
main java files go under folder src/main/java, and tests go under src/test/java. If we had any resource
in the project they would go under src/main/resources or src/test/resources. Furthermore, pom.xml
resides at the root folder of the project.

2.7 Analyze generated pom.xml

pom.xml

3 Maven Build Lifecycle
Maven has a defined build lifecycle. Some of the common build phases are compile, test, install, deploy
and clean.
3.1 compile
Compile build phase compiles the main java classes and places them in target directory in the project.
Execute command "mvn compile" at command line as shown in the below screen.

After executing the compile command successfully, browse to the target directory of your project and
see that main classes have been compiled. You will observe that java classes under /src/test/java have
not yet been compiled.
3.2 test
Test build phase is used to run test cases. It first executes the compile phase, then compiles the test files
(under folder /src/test/java) and executes all the test cases. Execute command "mvn test" on command
line as shown in below screen:

The above command will run all the unit tests, build will only succeed if all of them passed. If you
browse to target directory of your project, you will notice that two new folders have been created as
shown below:

test-classes folder contains the compiled classes for j-units, and surefire-reports contains reports for the
executed unit-tests.
Sometimes, you may want to execute a single test file, and not the whole suite of test-cases; you can
use command "mvn -Dtest=AppTest test". Here AppTest is the name of the test file (note that it is not
fully qualified name) that we want to run.
3.3 package
Package phase packages the project into jar, war or ear file (depending on what we have specified in
pom.xml). It executes all the phases (i.e. compile and test) before packaging the project.
Execute command "mvn package" to package your project. After successful execution, browse to target
directory and verify that the project has been packaged into jar file.
If you may want to package your project, you can use command "mvn -DskipTests=true package".
3.4 install
Install phase installs the project jar file into local repository. It executes all the build phases (compile,
test, package) before installing the project.
Execute command "mvn install" on command line.
4 Adding Dependencies
Joda Time is a library that contains utility methods for dealing with times. In this exercise we will use this
library to demonstrate how to add new dependencies in pom.xml.
1. Open the main java file, and add code to include time with hello-world string. The code to be added is
given below:
package uk.co.uworx;
import org.joda.time.DateTime;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
DateTime time = new DateTime();
System.out.println( "Hello World!" + time.toString());
}
}

2. Go to command line and compile the project using "mvn compile" command. Compile command
should raise an exception, as we have not yet included the dependency in our pom.xml
3. Open pom.xml and add the below dependency in the dependencies element. Please note that we
have not provided scope element with this dependency; when we don't supply scope, it defaults to
compile, which means that this dependency should be available at compile time.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>

4. Again run command "mvn compile". Since we have added the dependency in pom.xml, so maven will
download the jar file for joda-time and make it available at compile time. Browse to the maven
repository in file system (~/.m2/repository), and you should be able to see that the jar file for joda-time
has been downloaded in local repository.

Вам также может понравиться