How to configure Eclipse build path to use Maven dependencies?
Solution 1
If you right-click on your project, there should be an option under "maven" to "enable dependency management". That's it.
Solution 2
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.
Solution 3
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.
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).If you created a "Maven Project" under Eclipse (Ctrl+N > Maven Project), then it should be already "Maven ready".
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.
Solution 4
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
Solution 5
Right click on the project Configure > convert to Maven project
Then you can see all the Maven related Menu for you project.
James Goodwin
Updated on March 06, 2020Comments
-
James Goodwin over 4 years
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?
-
matt b over 14 yearsm2eclipse 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 over 14 yearsNote 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
-
Buhb over 14 years@matt b: You need to redo mvn eclipse:eclipse each time your dependencies change.
-
James Goodwin over 14 yearsIt 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
-
Binil Thomas over 14 yearsTo 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.
-
Devanshu Mevada over 14 yearsThere is no need for this when using the m2eclipse plugin.
-
Wolkenarchitekt almost 14 yearsAttention: since version 2.8 maven-eclipse-plugin doesn't support "eclipse:m2eclipse" anymore (couldn't find reason/informations why)
-
vaughan about 13 yearsJust found out that the maven-eclipse-plugin was the cause of my strange unresolved dependency errors. Thanks!
-
dfrankow about 12 yearsWhat if there is no "Maven" option in the right-click menu, even though I seem to have m2eclipse installed?
-
dfrankow about 12 yearsAh, "Configure" => "Convert to Maven Project" is your friend!
-
aioobe almost 12 yearsI'm using the m2eclipse plugin and was experiencing the problem after importing an existing maven project into eclipse. Doing
mvn eclipse:clean
andmvn eclipse:eclipse
actually fixed the problem for me. -
Vivek almost 12 yearsJames way is the right thing to do if you have added a POM to a project that was not build as a Maven project.
-
vasilakisfil over 11 years@dfrankow what if "Configure" => "Convert to Maven Project" option does not exist ??
-
dfrankow over 11 years@Filippos No idea. Different Eclipse version? Different or missing Maven plugin?
-
s.Daniel about 10 yearsIn case this fails with an error message in eclipse take a look at scandio.de/2012/10/… where a workaround in described.
-
Nile almost 10 yearsGreat solution if one is not using the m2eclipse plugin.
-
Saheed over 8 yearsFor Eclipse Mars (v4.5), the fix is to right-click then select "Maven" => "Enable Workspace Resolution"
-
Skystrider over 8 yearsThose 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.
-
user1491636 over 8 years@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.
-
Saheed over 8 years@user1491636 - Does the maven project build normally from the command line or there are errors when you execute
mvn compile install
for instance? -
user1491636 over 8 years@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 over 8 years@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.
-
Ewoks over 8 yearsI 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?
-
Syed about 8 years@Pascal Thivent, I've imported the Maven project. I'm not able to find the solution for this one
-
Shilan over 7 yearsI am using STS, the classpath is there, and there is no such option as "enable dependency management" :(
-
razor about 7 yearsyeah, 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 about 7 yearseclipse:eclipse is bad, adds all libraries, one by one, to your dependencies instead of relying on Maven
-
Bill Hileman about 6 yearsThis 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!
-
Tahir Mehmood about 6 yearsThanks 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,
-
Hannes Schneidermayer about 6 yearsthe right way if you didnt create a mvn project in the first place
-
Venkateswara Rao over 5 yearsThe shortcut to generate above .classpath file automatically is Project ->Maven -> Update Project Otherwise simply Alt+F5
-
xenoid over 5 yearsWorked 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...)