Monday, March 03, 2008

Using maven with eclipse

As i first started using maven for my projects, I used to add the maven plugin to eclipse. The plugin makes you able to mark a project as "maven enabled". This adds the library maven dependencies to the classpath of the project so the dependency libraries can be visible to your classes.

As i moved further, I found a better way dealing with maven projects using eclipse; maven eclipse:clean and eclipse:eclipse goals.

the goals enables you to make all the configuration and settings through command line which is more powerful and stable. Actually i now never created a new project inside eclipse. It is better to created it outside using maven. Then through eclipse:eclipse u create eclipse project settings files that is ready to be imported to any running eclipse instance.

M2_REPO variable that points to the local maven repository. The pretty thing is that you can add that variable to an eclipse workspace through mvn -Declipse.workspace={workspace-path} eclipse:add-maven-repo

To put things into order. for better use of maven and eclipse:

Setup maven repo variable for the eclipse workspace

mvn -Declipse.workspace=D:\java\eclipse-workspace eclipse:add-maven-repo

Create your new maven project through command line

mvn archetype:create -DgroupId=com.mycompany -DartifactId=MyProject -DarchetypeArtifactId=maven-archetype-webapp -Dpackaging=war -Dgeneratepom=true

Change the working directory to that of the new project and setup eclipse project files

mvn eclipse:eclipse

When you edit your pom file to add/remove dependencies you must resetup the eclipse project files through

mvn eclipse:clean eclipse:eclipse
and then do not forget to refresh ur project in eclipse :)

find more about maven with eclipse


mshalaby said...

Great Info. Thanks a lot. That was very helpful.

mahmoud said...

glad it helped

Phil Crow said...

Hi Mahmoud. I agree about the command line being more stable. We've got Maven integration in eclipse but things quickly get hard to manage. I believe it's partly because we don't have a strong grasp of how to use Maven. The commands you've listed here are very useful foundation commands. Thanks

mahmoud said...

I'm glad it helped.

GUI just wraps command line tools.. but using command line directly gives you a better grasp of what is really going on

Good Luck