开发者

How to add "Maven Managed Dependencies" library in build path eclipse?

开发者 https://www.devze.com 2023-02-07 16:26 出处:网络
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 propert

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 "Mave开发者_运维问答n Project Setting" to configure Maven dependency resolution.

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


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


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

How to add "Maven Managed Dependencies" library in build path eclipse?


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


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!


  • Install M2E plugin.
  • Right click your project and select Configure -> Convert to Maven project.

How to add "Maven Managed Dependencies" library in build path eclipse?

  • 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.


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.


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.


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.


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.


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>


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.


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


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)


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.


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消