69

I have created a Maven project and I want to add all Maven dependencies in build path of the project in Eclipse. When I go to Java Build Path > Add Library > Maven Managed Dependency > Next in property dialog of the project, it asks me to Use "Maven Project Setting" to configure Maven dependency resolution.

My question is how to add maven dependencies in current build path?

Richard
  • 6,812
  • 5
  • 45
  • 60
Amit Patel
  • 15,609
  • 18
  • 68
  • 106
  • If this has not been created as a Maven project but you need to add maven dependencies then Right-click on Project -> Configure -> Convert to Maven Project which will add pom.xml to your project where you can configure maven dependencies. – Srini Karthikeyan Jun 06 '21 at 14:48

16 Answers16

52

from the command line type:

mvn eclipse:eclipse

this will add all the dependencies you have in your pom.xml into eclipse...

however, if you haven't done any of this before you may need to do one other, one time only step.

Close eclipse, then run the following command from the shell:

mvn -Declipse.workspace=<eclipse workspace> eclipse:add-maven-repo

sample:

mvn -Declipse.workspace=/home/ft/workspaces/wksp1/ eclipse:add-maven-repo
ftravers
  • 3,809
  • 3
  • 37
  • 38
  • 9
    [eclipse:add-maven-repo](http://maven.apache.org/plugins/maven-eclipse-plugin/add-maven-repo-mojo.html) has been deprecated in favor of [eclipse:configure-workspace](http://maven.apache.org/plugins/maven-eclipse-plugin/configure-workspace-mojo.html) – geaw35 Aug 03 '12 at 14:56
  • 27
    You may need to do: "click right on project > Maven > Update project" to get the maven dependencies enabled... it worked for me, see http://stackoverflow.com/questions/2037188/how-to-configure-eclipse-build-path-to-use-maven-dependencies – Adriano Aug 22 '12 at 13:04
  • 4
    If you're using M2Eclipse then eclipse:eclipse doesn't play nicely and you may have to rerun when you change your dependencies. Typically there is some issue in your pom that M2Eclipse doesn't like that you need to find. I posted a workaround in my answer that may get you going temporarily but the solution is to find the root cause. – Marquee Sep 04 '13 at 23:18
38

If you have m2e installed and the project already is a maven project but the maven dependencies are still missing, the easiest way that worked for me was

  • right click the project,
  • Maven,
  • Update Project...

Eclipse screenshot

schnatterer
  • 7,525
  • 7
  • 61
  • 80
  • Accidentally removed Maven Dependencies from my build path instead of my deployment assembly and couldn't get them back.This saved me an hour and a headache! – Paul Mar 31 '15 at 09:47
  • Worked for me like a charm – minhas23 Jun 02 '16 at 08:58
  • 4
    doesn't work, and if i add Maven Managed Dependencies (add Library under Java Build Path->Libraries), nothing happens..... – razor Aug 19 '16 at 08:59
12

Make sure your packaging strategy defined in your pom.xml is not "pom". It should be "jar" or anything else. Once you do that, update your project right clicking on it and go to Maven -> Update Project...

Pedro Madrid
  • 1,887
  • 1
  • 20
  • 32
11

I have the same issue using SpringSource Tool Suite. I was importing an existing Maven project which ran fine from the command line. However, when I imported the project using import -> Maven -> existing Maven project, the project did not import correctly import and I could not add the maven managed dependencies library to the build path.

If I ran mvn install from the command line the project built successfully. If I ran mvn eclipse:eclipse and then imported into STS, then everything worked as expected, except of course I'd have to re-run this every time I updated the pom, which was undesirable.

I worked around it by running mvn eclipse:eclipse and then manually updating .classpath to eliminate the M2_REPO dependencies added by eclipse:eclipse and adding the m2eclipse dependency entry:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
  <attributes>
    <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
  </attributes>
</classpathentry>

Then I imported existing Maven project and it worked as expected.

This is a hack and I'm not sure what other consequences running eclipse:ecplise has when working with m2eclipse. But it did at least allow me to get my project compiling so I could get to coding.


EDIT 1/4/2013

The workaround I posted above will work temporarily, and I never discovered any negative side effects. However, I've had this issue a few times now and each time the problem is that there is some section of the pom that maven accepts but m2eclipse barfs on.

So I recommend removing parts of the pom 1 by 1 until you can mavenize the project successfully. Just keep running maven -> update configuration after each pom edit until it works as it should. I usually start by removing the plugin configuration tag blocks one at a time, starting with the most suspicious (i.e. most complicated).

Once it mavenizes properly, you can revert the pom and it should still work as expected.

After I get running, I'd research the offending configuration(s) to try to figure out what the 'proper' fix is (according to m2eclipse, anyway).

Hopefully this approach leads to the permanent solution instead of settling for a hacky workaround!

Marquee
  • 1,776
  • 20
  • 21
  • The root cause of my issue ended up being a problem in the pom related to the Java compiler. So I just figured I'd reiterate that the solution above is just a work around until the root cause is discovered and fixed. Once I fixed the pom, the m2e import into STS worked as expected. – Marquee Aug 08 '12 at 19:33
  • and in a middle of 2016 i still need to add that entry to .classpath manually (STS 3.8.1) ..... – razor Aug 19 '16 at 08:57
8
  • Install M2E plugin.
  • Right click your project and select Configure -> Convert to Maven project.

enter image description here

  • Then a pom.xml file will show up in your project. Double click the pom.xml, select Dependency tab to add the jars your project depends on.
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
6

You can install M2Eclipse and open the project as maven project in Eclipse. It will create the necessary configuration and entries.

This is also useful for subsequent updates to the pom. With maven eclipse plugin, you will need to manually regenerate the eclipse configuration for each changes.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
2

If Maven->Update Project doesn't work for you? These are the steps I religiously follow. Remove the project from eclipse (do not delete it from workspace) Close Eclipse go to command line and run these commands.

mvn eclipse:clean
mvn eclipse:eclipse -Dwtpversion=2.0

Open Eclipse import existing Maven project. You will see the maven dependency in our project.

Hope this works.

Sabarish
  • 862
  • 1
  • 14
  • 23
2

I could figure out the problem. I was getting following warning on startup of eclipse.

The Maven Integration requires that Eclipse be running in a JDK, because a number of Maven core plugins are using jars from the JDk.

Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that
Installed JRE's are also using JDK installs

I changed eclipse.ini file and added following and restarted eclipse

-vm
C:/Program Files/java/jdk1.6.0_21/bin/javaw.exe

Now I can see "Maven Dependency" library included automatically in java build path.

Amit Patel
  • 15,609
  • 18
  • 68
  • 106
2

If you imported an existing maven project and Maven dependencies are not showing in the build path in eclipse then right click on project--> Maven--> 'update Project' will resolve the issue.

Vikky
  • 1,123
  • 14
  • 16
1

If you have removed Maven dependency from Library accidentally. Add below in pom.xml

<build>     
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
</build>
Guest
  • 11
  • 1
0

Try:Right Click your project->Maven->Disable Dependency Management. And re-enable dependency management.

ChangePicture
  • 31
  • 1
  • 3
0

You could also consider to maven-dependency-plugin to your pom:

<plugins>
... 
    <plugin> 
        <artifactId>maven-dependency-plugin</artifactId> 
            <executions> 
                <execution> 
                    <phase>process-resources</phase> 
                    <goals> 
                        <goal>copy-dependencies</goal> 
                    </goals> 
                    <configuration>   
                        <outputDirectory>${project.build.directory}/lib</outputDirectory> 
                    </configuration> 
                </execution> 
            </executions> 
    </plugin> 
...
</plugins>

Than you can run "mvn package" and maven will copy all needed dependencies to your_project_path/target/your_project_name/WEB-INF/lib/ directory. From there you can copy them to your project/lib dir and add as external jars (configuring your project settings buildpath)

Picrochole
  • 167
  • 5
0

If you use an eclipse plugin to do your maven tasks (there are 2 of them : M2ecipse from sonatype the 'official' one and q4e on Google Code) then, there are options in the contextual menus (right click on the project) to do that painlessly.

You can have both plugins running at the same time in your eclipse workbench and use them indifferently on a per project basis.

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
0

Follow these steps

1) Go in projects class path

2) Go in library tab

3) click on Add Library

4) In opened dialogue select Maven Managed Dependencies

5) Click on Next

6) In the new dialogue click on Manage Project Settings

7) In opened dialogue select the check box Resolve dependencies from workspace

8) Click on Restore defaults

9) It will do some process and you will have all your dependencies in your library now.

raj
  • 19
  • 3
0

Likely quite simple but best way is to edit manually the file .classpath at the root of your project folder with something like

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>

when you want to have jar in your WEB-IN/lib folder (case for a web app)

hinanooos
  • 1
  • 1
0

If this has not been created as a Maven project but you need to add maven dependencies then Right-click on Project -> Configure -> Convert to Maven Project.

This will add a pom.xml file to your project.

If the pom.xml file already exists, it is already a Maven project so that you can add your dependencies directly in pom.xml

Now to do any other actions related to Maven, you can Right Click on project and click Maven