James Strachan's Weblog
Ramblings on Open Source, Java, Groovy, XML and other geeky malarkey
        

Using Eclipse and Maven

Just a quick note on using eclipse with Maven enabled projects as some folks don't know this.

Setting up projects, source paths and classpaths in any IDE can be a pain - especially if you work with lots of different projects. But if your project uses Maven then this step can be totally automated. There are similar plugins to do the same thing with IDEA and JBuilder and too - I'll just describe the Eclipse plugin here.

Things have changed slightly on eclipse so I'll give two sets of instructions depending on if you are using 2.1 M4 or earlier releases.

Eclipse 2.1 M4 or later

You need to tell Eclipse where the Maven repository is on your machine. This enables the generated project files to reference the Maven repository jars while still being portable to other users machines.

So you need to define a build variable called MAVEN_REPO.

To do this open the preferences (Window menu -> Preferences). Then click on  Java -> Classpath Variables. Then click the 'New' button to add a new classpath variable. Enter the name MAVEN_REPO and the value is the 'repository' directory inside your Maven installation. e.g. if you installed Maven in your C:maven-1.0-b7 directory then the value of MAVEN_REPO should be c:maven-1.0-b7repository.

Note you only need to do the above once, then the MAVEN_REPO variable can be reused across all Maven-enabled projects.

 

Now once you've configured your Eclipse installation, you can just cd into any of your maven-enabled projects directories and type

    maven eclipse

and Maven will create a .project and .classpath for you!

Then import the project into Eclipse (or close and open the project if its already in your workspace) and all the classpaths & source paths should all be setup for you. You may wanna Rebuild Project and your errors should disappear.

The nice thing about this is that it works on any Maven-enabled project and should ultimately work with any IDE. Right now Eclipse, IDEA and JBuilder are supported; I'm sure others could be added to the mix pretty easily.

 

Eclipse 2.1 M3 or earlier

First off you need to configure your Eclipse installation so that it knows about your Maven repository. You need to define a property called maven.eclipse.workspace to point to where your eclipse workspace is. There's documentation here...

http://jakarta.apache.org/turbine/maven/reference/plugins/eclipse/properties.html

You can define this property a local build.properties, in the build.properties in your home directory or on the command line via -DpropertyName=value in the following command.

Then to update your eclipse installation so that its aware of the Maven repository, type the following.

    maven eclipse:add-maven-repo

Now once you've configured your Eclipse installation, you can just cd into any of your maven-enabled projects directories and type

    maven eclipse

and Maven will create a .project and .classpath for you!

Then import the project into Eclipse (or close and open the project if its already in your workspace) and all the classpaths & source paths should all be setup for you. You may wanna Rebuild Project and your errors should disappear.

The nice thing about this is that it works on any Maven-enabled project and should ultimately work with any IDE. Right now Eclipse, IDEA and JBuilder are supported; I'm sure others could be added to the mix pretty easily.



© Copyright 2007 James Strachan. Click here to send an email to the editor of this weblog.
Last update: 17/4/07; 11:11:34.