Monday, 21st June 2010
The new frontier for learning Java

Starting a Java project with Maven 2

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book


This tutorial explains how to set up a Java project with Maven 2 while using Eclipse as an IDE.

Contents

the article

Getting Started

In it's essence Maven is a command line build tool that can compile and aggregate all the bits and pieces of your project. It runs unit tests automatically straight out of the box and has a very convenient dependency management system.

After you have downloaded and followed the installation instructions in the download page open a command window (if you are using Windows) or bring up your Linux console and enter in this command:

mvn -v

If the instructions have been followed correctly, then the consoles output should be something like this:

Maven version: 2.0.7
Java version: 1.4.2_16
OS name: "windows xp" version: "5.1" arch: "x86"

Since Maven relies on the projects folder structure, w have to create the structure first before we start adding our source and resource files. To do so:

  • Navigate to or create a project folder where you want your project source to reside.
  • Open a command line interface in that folder (you can also navigate and create folders in the command interface)
  • Enter the following command in the console:
mvn archetype:create \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.yourcompany.app \
  -DartifactId=my-project

After the command has executed, you should see something like this in your command interface:

[INFO] Parameter: groupId, Value: com.yourcompany.app
[INFO] Parameter: packageName, Value: com.yourcompany.app
[INFO] Parameter: basedir, Value: C:\wikijava
[INFO] Parameter: package, Value: com.yourcompany.app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: my-project
[INFO] ********************* End of debug info from resources from generated POM
 ***********************
[INFO] Archetype created in dir: C:\wikijava\my-project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15 seconds
[INFO] Finished at: Mon May 26 14:26:59 BST 2008
[INFO] Final Memory: 4M/8M
[INFO] ------------------------------------------------------------------------

Note: If you are using Windows, then the line brakes in the command above have to be removed since the Windows console doesn't support commands with line brakes in them.

When the above command has executed successfully navigate to the folder that Maven has created. In this case it's my-project and execute the command:

mvn install
This command and
mvn clean
will be the most used commands while working with maven. The install command (or goal in build tool terms) compiles your source code, runs all the unit tests it can find and aggregates the class files into a jar, war or ear file (this can be specified by modifying the Maven pom.xml (Project Object Model) file).

When running the command for the first time don't be surprised if takes a few minutes to complete. This is because Maven does most of the dependency management itself. While the command is executing you will see many libraries being downloaded. These libraries and plugins are stored locally in your local Maven repository, so the next time you will run this command Maven won't need to download everything again.

If you can see a
[INFO] BUILD SUCCESSFUL
in the console after the install command has finished, then this means that the project was successfully built.


Importing a Maven project into Eclipse

If you are using Eclipse as your development IDE, you probably will want to get the project imported in it. To do so:

  • Execute the command
    mvn eclipse:eclipse
    in your project folder (the one that contains the pom.xml file)
  • After the command has finished with a
    [INFO] BUILD SUCCESSFUL
    message, open Eclipse
  • In Eclipse click on File→Import→General→Existing Projects into Workspace→Next
  • In the Import Projects window select the root directory of your project (in this case it's C:\wikijava)
  • Check the projects you want to import (in this case my-project)
  • Click Finish

That's it, your project with all the dependencies and references is imported in Eclipse and you are good to go on your next project.

Have fun!


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page

nice

very interesting article, thanks for sharing

--DonGiulio 15:33, 20 June 2008 (PDT)


Title (required):

Website:

Comment: