Ensode.net
Google
 

Home
Blog
Guides
Tips
Articles
Utilities
Reviews
About Us
Contact us


Facebook profile

XML

Introduction to Maven 2
Bookmark and Share

Introduction

Maven is a tool used to build Java code, similar to ANT. However Maven has a number of advantages over ANT, the main one being it being able to automatically download a project's dependencies from a central repository. Maven's goals (similar to ANT tasks) are standard, for example, to compile the code is always mvn compile, where in ANT the command depends on the name of the target in the project's build.xml file.

This article is intended as a quick getting started with Maven 2 guide. It will cover quickly creating a project skeleton by using Maven's archetype:create goal, as well as adding dependencies to a project. See resources at the end of the article for links to more in-depth guides to Maven 2.

Quickly Creating a Java Project

To quickly create a Java project, type the following command in the command line (all in one line):
mvn archetype:create -DgroupId=maven-test -DartifactId=maven-test -DpackageName=net.ensode.maventest
The above command tells Maven to create a new Java project, belonging to a group called "maven-test" (each Maven artifact belongs to a group), having an artifactId of "maven-test", and a java package of "net.ensode.maventest." After running this command, maven will create the directory structure for the project, along with a simple Java class with a main method, and a simple unit test. The following screenshot illustrates the generated directory structure and files.

maven generated directory structure

As can be seen in the screenshot, Maven creates a file called pom.xml, this file is analogous to ANT's build.xml file. It also creates a directory structure to place Java classes and unit tests. Although Maven doesn't force us to use its standard directory structure, it strongly encourages its use. Following Maven's standard directory structure allows developers to easily locate files through different projects using Maven as their build tool.

The generated App.java class is the typicall "Hello world!" sample application. It is meant to be modified in order to add real functionality to the application.

Compiling The Code

To compile the code, from the command line, change directory to the project's root directory (maven-test in this example), and execute the following command:
mvn compile
The output should look something like this:


[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------------
[INFO] Building maven-test
[INFO]    task-segment: [compile]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
Compiling 1 source file to /home/heffel/maven-test/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Thu Dec 14 20:01:51 EST 2006
[INFO] Final Memory: 5M/21M
[INFO] ------------------------------------------------------------------------


Maven target directory

If compilation is successful, Maven will create a directory called "target" under the project's root directory, this directory will contain the compiled class files corresponding to the Java source files in the src/main/java directory.

Executing the code

Maven provides a goal to execute stand alone Java applications. In order to execute our application, the following command must be executed from the project's root directory:
mvn exec:java -Dexec.mainClass=net.ensode.maventest.App
The -Dexec.mainClass argument tells Maven what class contains the main method we want to execute.

Output of the above command should look something like this:


[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ----------------------------------------------------------------------------
[INFO] Building maven-test
[INFO]    task-segment: [exec:java]
[INFO] ----------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java]
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Dec 14 20:14:08 EST 2006
[INFO] Final Memory: 3M/8M
[INFO] ------------------------------------------------------------------------


The "Hello World!" line is the actual output of our code.

Next - Adding Dependencies To The Project


Java EE 6 Development With NetBeans 7
Java EE 6 Development With NetBeans 7


Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server


JasperReports 3.5 For Java Developers
JasperReports 3.5 For Java Developers