130

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">
  <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>

To manage dependencies, I usually add the project or JAR to the build path and then i'll be able to build my project..

But when using M2Eclipse the dependencies are not added automatically to the build path. Is there any configuration to let Eclipse know that the Maven is managing the dependencies?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
James Goodwin
  • 7,360
  • 5
  • 29
  • 41

18 Answers18

119

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

Buhb
  • 7,088
  • 3
  • 24
  • 38
  • 3
    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 Jan 10 '10 at 14:20
  • 29
    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 Goodwin Jan 10 '10 at 16:15
  • 13
    What if there is no "Maven" option in the right-click menu, even though I seem to have m2eclipse installed? – dfrankow Apr 20 '12 at 22:33
  • 86
    Ah, "Configure" => "Convert to Maven Project" is your friend! – dfrankow Apr 20 '12 at 22:35
  • James way is the right thing to do if you have added a POM to a project that was not build as a Maven project. – Vivek Sep 26 '12 at 04:46
  • 2
    @dfrankow what if "Configure" => "Convert to Maven Project" option does not exist ?? – vasilakisfil Jan 20 '13 at 21:09
  • @Filippos No idea. Different Eclipse version? Different or missing Maven plugin? – dfrankow Jan 21 '13 at 23:55
  • In case this fails with an error message in eclipse take a look at http://www.scandio.de/2012/10/eclipse-enabling-maven-dependency-management-has-encountered-a-problem/ where a workaround in described. – s.Daniel Apr 04 '14 at 14:19
  • For Eclipse Mars (v4.5), the fix is to right-click then select "Maven" => "Enable Workspace Resolution" – Saïd Oct 08 '15 at 11:50
  • @Saheed For Eclipse Mars, enabling workspace resolution did not work for me. All of my maven dependencies are still not on the build path. I've tried all other recommended solutions... still no luck. – user1491636 Jan 28 '16 at 14:46
  • @user1491636 - Does the maven project build normally from the command line or there are errors when you execute `mvn compile install` for instance? – Saïd Jan 28 '16 at 14:49
  • @Saheed It builds fine with maven. Eclipse still flags most of the classes with errors though because none of the dependencies show up on the build path. – user1491636 Jan 28 '16 at 14:51
  • @Saheed So I figured out that if I manually add the following classpathentry "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" to the .classpath file, then some of the maven dependencies get added. However its only those dependecies from my parent pom, not those from my module (child) pom. – user1491636 Jan 28 '16 at 15:27
  • I am using STS, the classpath is there, and there is no such option as "enable dependency management" :( – Shilan Nov 09 '16 at 10:21
70

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
  • 22,499
  • 9
  • 75
  • 95
  • 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 Jan 10 '10 at 14:21
  • @matt b: You need to redo mvn eclipse:eclipse each time your dependencies change. – Buhb Jan 10 '10 at 16:05
  • 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 Thomas Jan 10 '10 at 17:02
  • 6
    There is no need for this when using the m2eclipse plugin. – Pascal Thivent Jan 10 '10 at 18:19
  • 3
    I'm using the m2eclipse plugin and was experiencing the problem after importing an existing maven project into eclipse. Doing `mvn eclipse:clean` and `mvn eclipse:eclipse` actually fixed the problem for me. – aioobe Jul 10 '12 at 13:52
  • Great solution if one is not using the m2eclipse plugin. – Nile Aug 02 '14 at 17:26
  • Those commands didn't work for me. They ran and succeeded but no new jars in my lib folder. Do I have to use Eclipse EE? I don't see m2eclipse in the marketplace but I do have "Maven Integration for Eclipse (Luna and newer) 1.5". I have Luna 64bit but not EE. I probably should start over with EE. – Skystrider Jan 15 '16 at 06:13
29

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 2
    Just found out that the maven-eclipse-plugin was the cause of my strange unresolved dependency errors. Thanks! – vaughan Jun 27 '11 at 15:22
  • 1
    I just use EclipseNeon downloaded from official site.. There is no Maven option in menu after I created standard java project.Do we need to install something separately or everything is already downloaded with Neon? – Ewoks Feb 22 '16 at 14:41
  • @Pascal Thivent, I've imported the Maven project. I'm not able to find the solution for this one – Syed Jun 15 '16 at 04:55
21

Add this to .classpath file ..

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

Thx

Apurva Singh
  • 4,534
  • 4
  • 33
  • 42
  • 1
    yeah, i need to add that manually everywhere for a last few years.... why don't they impove eclipse plugins to recongnize maven dependencies automatically ?? – razor May 04 '17 at 10:26
  • 1
    Thanks for this solution. This worked for me. I accidently deleted .project and .classpath files. This solution worked for me to add maven dependencies in class path, – Tahir Mehmood Apr 23 '18 at 11:18
  • The shortcut to generate above .classpath file automatically is Project ->Maven -> Update Project Otherwise simply Alt+F5 – Venkateswara Rao Jan 09 '19 at 05:31
  • Worked for me too. Solved most of my missing dependencies but still have a couple of problems, so I wonder if the more user-friendly solutions aren't working because there is a small error somewhere (but this behavior doesn't help debug the problem...) – xenoid Jan 31 '19 at 17:26
14

Right click on the project Configure > convert to Maven project

Then you can see all the Maven related Menu for you project.

DevJav
  • 149
  • 1
  • 2
11

I did like this..

Right click on the project--> configure-->convert to maven project. Right click on the project-->maven-->add dependencies.

madhu_karnati
  • 785
  • 1
  • 6
  • 22
5

In Eclipse (Mars) go to:

Project Properties --> Maven. Ensure that "Resolve dependencies from workspace projects" is disabled.

Thomas Beer
  • 230
  • 3
  • 9
  • This did not solve the original problem, however it DID solve another nagging problem I've been having for weeks - After disabling that option, my project, which was only running from a maven command line, is now running again inside eclipse! Thank you! – Bill Hileman Apr 20 '18 at 18:42
4

I did not found the maven or configure menus but found the following button that solved my problem:

enter image description here

borjab
  • 11,149
  • 6
  • 71
  • 98
2

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
  • 2,383
  • 14
  • 15
  • 3
    Attention: since version 2.8 maven-eclipse-plugin doesn't support "eclipse:m2eclipse" anymore (couldn't find reason/informations why) – Wolkenarchitekt Aug 13 '10 at 12:17
1

For newer Eclipse versions (>=Mars) right click on project > configure > convert to Maven project

JBoy
  • 5,398
  • 13
  • 61
  • 101
1

Adding my answers for a couple of reasons:

  • Somehow none of the answers listed directly resolved my problem.
  • I couldn't find "Enable dependency management" under Maven. I'm using Eclipse 4.4.2 build on Wed, 4 Feb 2015.

What helped me was another option under Maven called as "Update Project" and then when I click it this window opens which has a checkbox that says "Force update of Snapshot/Releases". The real purpose of this checkbox is different I know but somehow it resolved the dependencies issue. enter image description here

Saurabh Patil
  • 4,170
  • 4
  • 32
  • 33
1

None of that solved my problem. but what I did was if click on the pom.xml, there is a tab at the bottom named dependencies. in this tab it is split into 2 section, one called dependencies and one called dependency management. select every thing in the dependency section and click add to be under the dependency management control. close and reopen your project.

amralieg
  • 113
  • 8
1

I had a slight variation that caused some issues - multiple sub projects within one project. In this case I needed to go into each individual folder that contained a POM, execute the mvn eclipse:eclipse command and then manually copy/merge the classpath entries into my project classpath file.

Mearde
  • 21
  • 1
0

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ä
  • 33,425
  • 31
  • 131
  • 183
  • eclipse:eclipse is bad, adds all libraries, one by one, to your dependencies instead of relying on Maven – razor May 04 '17 at 12:25
0

Make sure your POM follows the naming convention, and is named in lowercase lettering as pom.xml and NOT POM.xml.

In my case all was right, but Eclipse still complained when trying to Right-click and Update project configuration - told me that the POM could not be read. Changed the name to lowercase - pom.xml from POM.xml - and it worked.

kenny_k
  • 3,831
  • 5
  • 30
  • 41
Karun
  • 1
0

I met this issue too. When I add dependencies in the pom.xml, I checked in the local folder /Users/xyz/.m2/ and the jars are already downloaded there, but cann't added the the buildpath of the eclipse.

My eclipse Version: Mars.2 Release (4.5.2)

right click project > Maven > Enable Workspace Resolution

And this solved my issue.

Eddy
  • 3,623
  • 37
  • 44
0

Typically, you can copy the .classpath file from a working project since there isn’t anything project specific here assuming you’re structured as a standard Maven project.

Puneet Lamba
  • 755
  • 8
  • 15
0

This worked for me in Eclipse Oxygen (4.7.0):

Right click Project -> Maven -> Select Maven Profiles... then check the Repository Proxy box, press OK.

r590
  • 689
  • 1
  • 10
  • 15