Friday, 14th October 2011
Follow WikiJava on twitter now. @Wikijava

Using Maven and Eclipse, the basics

From WikiJava

Jump to: navigation, search


Maven is a very powerful tool, because it allows you to start many types of project from a template, rather than from a scratch. And because it frees you from having to download and possibly store in your repository the Jars with the libraries you need.

In this article I'm going to show you how to create a simple java project, and how to include it in Eclipse, keeping also all the dependencies on external libraries.

I'll show you in the article: Using Maven with any jar dependency, how to deal with the specific situation where the dependency we want to use is not available in the main repository. (which is not so rare apparently)

Contents

a Little bit about Maven

Maven, according to the official site, was made for:

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

To achieve these goals the way they took was to make maven handle very specific (state of the art/best practice ... ) organizations of the projects. So that maven would be able to build, deploy and perform many actions on any project, as long as it follows the specifications.

Maven has a great system to help us following the project specifications. That is: It can create most of all of them automatically.

This is achieved via a superb template system.

We will see below that with just two command line commands we can create a full-fledged java project, and even the Eclipse project files so that we don't need to bother any more with importing libraries.

Since all the projects created with maven have more or less the same file structure, Maven can build, deploy, package and test them with always the same few commands. offering the uniform build system mentioned above.

Starting a maven project

As said I won't go through this too deeply, this is covered very well else where and it makes no sense to repeat it here.

We will just generate a simple (empty) java project with the command:

mvn archetype:create -DgroupId=com.test -DartifactId=mytest

This will create a subdirectory called mytest containing a pom file and a src directory which contains java files for unittest and for your code.

In this new directory there's also the important pom.xml file, containing all the important data for building and using your new project.

This includes also the dependencies that tell maven which Libraries to download and put in the classpath of your project so that it can work.

Next step we want to achieve is to use the information contained in the pom.xml file to generate the project files for Eclipse, so that we can use our favourite IDE to write our code.

Using your project in Eclipse

Since all the information required for the eclipse project is available in the pom.xml file, it is really simple to use maven to do the magic.

The command is the following:

cd mytest
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true

At this point you can:

  1. move the mytest directory to your eclipse workspace directory,
  2. start eclipse,
  3. Hit on : File -> new -> Java Project
  4. in the wizard window type "mytest" (the name of the directory that you just copied in the workspace directory)
  5. and when Eclipse tells you that a project with that name already exists, hit on "finish" to import the project.

The Dependencies of the eclipse project generated by Maven are done (in the way they are supposed to be) so that all the paths are relative to a variable called M2_REPO which you should point to the directory where Maven holds the local repository.

The local repository is at:

  • ~/.m2/repository for unix and mac users
  • C:\Documents and Settings\username\.m2\repository for windows users

Here's how to set a variable in Eclipse for it:

open the eclipse preferences panel and type in the search box the word variables. This should let you see in the list below the menu Classpath variables looking like in the figure.

File:Eclipse_set_a_variable.png

Click on New and in the window type M2_REPO for the variable name, and the path to your Maven local repository, for the value.

At this point you can rebuild the project and it's all there.

You can add any other dependency to your project by editing your pom.xml file and adding chunks like:

 	<dependency>
 		<groupId>net.sourceforge.javacsv</groupId>
 		<artifactId>javacsv</artifactId>
 		<version>2.0</version>
 		<optional>false</optional>
 	</dependency>

Then you can repeat the steps explained in this section. to refresh the eclipse project files. and have everything ready again.

conclusions

In this article we have seen how to generate a new Java project, having few file structures ready for use, and we imported the project in Eclipse, maintaining the Library dependencies. We did all this using Maven, in two simple commands.

In the article Using Maven with any jar dependency We will get to the funny part, where we will use as dependencies some jars that are not in the maven main repository, and for this reason need special treatment.


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


Comments on wikijava are disabled now, cause excessive spam.