views:

2636

answers:

6

I would like to take advantage of the features that Maven provides for managing dependencies in a project. My brief understanding of how Maven works is that it will aquire the JARs needed and then build the project with these libraries.

Currently I have a simple POM file set up as a test:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jamesgoodwin.test</groupId>
  <artifactId>com.jamesgoodwin.test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.0.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

Usually when managing dependencies for a project I would simply add the project or JAR to the project build path and then I would be able to build my project. However when using M2Eclipse the dependencies do not seem to be added to the build path automatically, thus I can't build the project.

Is there somewhere I need to configure this to allow Eclipse to know that Maven is managing my dependencies?

+1  A: 

if you execute

mvn eclipse:clean

followed by

mvn eclipse:eclipse

if will prepare the eclipse .classpath file for you. That is, these commands are run against maven from the command line i.e. outside of eclipse.

davek
Note that this works by using a classpath variable (M2_REPO) to refer to your dependencies, it doesn't use m2eclipse and I don't believe it picks up and dependencies you add as you work on the project - but it's great for getting the initial setup going
matt b
@matt b: You need to redo mvn eclipse:eclipse each time your dependencies change.
Buhb
To add to what Buhb said, do mnv eclipse:eclipse on the command line and refresh the Eclipse project every time you add more deps to pom.xml.
binil
There is no need for this when using the m2eclipse plugin.
Pascal Thivent
A: 

Maybe you could look into maven-eclipse-plugin instead of M2Eclipse.

There you basically add maven-eclipse-plugin configuration to your pom.xml and then execute mvn eclipse:eclipse which will generate the required .project and .classpath files for Eclipse. Then you'll have the correct build path in Eclipse.

Juha Syrjälä
+1  A: 

When m2eclipse is installed properly, it should add dependencies automatically. However, you should generate the eclipse project files by entering:

mvn eclipse:m2eclipse

or, alternatively if you don't use m2eclipse:

mvn eclipse:eclipse
EJB
Attention: since version 2.8 maven-eclipse-plugin doesn't support "eclipse:m2eclipse" anymore (couldn't find reason/informations why)
ifischer
+3  A: 

If you right-click on your project, there should be an option under "maven" to "enable dependency management". That's it.

Buhb
m2eclipse doesn't do this by default if you import a project created with mvn eclipse:eclipse, so this always seems to trip people up.
matt b
It was actually enabled already for my project. Maybe because I chose to add a POM file to an existing project? However doing Maven > Update project configuration seems to have fixed it for me. Also it was your answer that eventually led me to discovering that toolbar where I could fix the project. Many thanks
James
+2  A: 

I'm assuming you are using m2eclipse as you mentioned it. However it is not clear whether you created your project under Eclipse or not so I'll try to cover all cases.

  1. If you created a "Java" project under Eclipse (Ctrl+N > Java Project), then right-click the project in the Package Explorer view and go to Maven > Enable Dependency Management (depending on the initial project structure, you may have modify it to match the maven's one, for example by adding src/java to the source folders on the build path).

  2. If you created a "Maven Project" under Eclipse (Ctrl+N > Maven Project), then it should be already "Maven ready".

  3. If you created a Maven project outside Eclipse (manually or with an archetype), then simply import it in Eclipse (right-click the Package Explorer view and select Import... > Maven Projects) and it will be "Maven ready".

Now, to add a dependency, either right-click the project and select Maven > Add Dependency) or edit the pom manually.

PS: avoid using the maven-eclipse-plugin if you are using m2eclipse. There is absolutely no need for it, it will be confusing, it will generate some mess. No, really, don't use it unless you really know what you are doing.

Pascal Thivent
A: 

please check the link to know more information link text

kidcoder