I’m trying to build a new web app using Eclipse, I want to use Spring web services and Hibernate, so I thought I’d try using Maven instead of Ant so I don’t have to track down all the JAR files manually. This is what I did …
I have a new eclipse workspace, I used Eclipse Marketplace to install the Maven for WTP features.
I tried two things, I really don’t know the “correct” way of doing this. Any help is greatly appreciated!
ATTEMPT ONE:
Created a new Maven project, not really sure what I’m doing but I chose the “maven-archetype-webapp”. It created an eclipse project with a different (maven-esq) file structure. I added Spring Web as a dependency to the pom.xml file. I can run the web app from within eclipse and see the hello world page; and a ‘maven install’ downloads the dependencies and produces a war file. However, then I tried adding a Java class “PersonController.java” and annotate it as a @Controller (and so on) but eclipse doesn’t seem to recognize that PersonController.java is a java class. For example, I can’t generate getters/setters or use the refactoring tool to rename, eclipse gives me an error like “The resource is not in the build path of a Java project”.
In the end, I feel like the maven file structure is too different to what eclipse is expecting, and basic eclipse features seems somewhat messed up. Of course, it’s entirely possible that I’m doing something very wrong.
ATTEMPT TWO:
Created a new ‘Dynamic Web Project’ in Eclipse, everything is as expected, I can run my web app fine. Now I want to add the Spring libraries as a Maven dependency and be able to build the project with maven, so I figure I need a pom.xml file. File > New … doesn’t not seem to help me create a Maven pom.xml file, and I figure if I just create the file manually that probably won’t work. So I’m not sure how to proceed adding maven support to my ‘Dynamic Web Project’.
So here I am … can anyone help … what’s the “correct” way of doing what I’m trying to do? And is Maven worth the trouble?
Thanks!
I wouldn’t say there is a single correct way since not everyone would want to use maven archetypes, that’s from my experience of rehabilitating legacy web apps. For the second approach you need the m2eclipse plugin to add maven support (and auto creation of pom file if there isn’t any).
As for whether it is worth the trouble? You will have to evaluate yourself depending on the kind of project you are dealing and to what extent you are willing to use maven. It is definitely a good way to manage shared dependencies, once you get past the initial learning curve. If you intend to follow maven conventions as much as possible, this is also the best time to do it (or figure out how to do it), since it gets much more expensive later on.