M2Eclipse: Resolving Maven dependencies from the Eclipse workspace

本秂侑毒 提交于 2019-12-08 19:18:27

问题


The M2Eclipse Homepage states that the plugin is capable of the following:

Resolving Maven dependencies from the Eclipse workspace without installing to local Maven repository

As I did not find any documentation, I could not figure out what this exactly means and how it is done. I am especially interested in cases where a project in workspace corresponds to two different jars (which both contain parts of the classes).


回答1:


The Eclipse workspace (when using M2E) acts as a local maven repository. Every Maven project you have checked out is available to be used as a dependency (just as if you had installed it on your local repository).

For example: If your project A depends on lib B version 1.0.0 and you check out the source for lib B on version 1.0.0, Eclipse will be able to compile A using the workspace version of B. You will not need to install lib B on your local repository.

This is specially useful when you need to make changes to a lib and test it in an application you also have on your workspace.

Notice, though, that the version of the dependency for lib B on the pom A and the declared version of B on pom B must match EXACTLY for this to work. For example, if on pom.xml for A you have:

 <dependency>
        <groupId>a.b.c</groupId>
        <artifactId>B</artifactId>
        <version>1.0.0</version>
 </dependency>

You need the checkout B on version 1.0.0.

If you need to make changes on B, you will probably have to change your dependency version to something-SNAPSHOT (1.0.1-SNAPSHOT, for example) and check out that version of B.

You also need to check the option "Resolve workspace artifacts" on your Eclipse project for this to work. (Right click the project -> Properties -> Maven -> Resolve dependencies from Workspace Projects)

If you want to make sure that Eclipse is using the version on your workspace and not an installed version (or even a version from a remote repository), check the "Dependencies" tab on the pom.xml Editor. The "regular" dependencies are shown with jar icons, the dependencies resolved on the workspace (like lib B) are shown with Eclipse project icons.




回答2:


M2Eclipse reads Maven descriptor (pom.xml) of your opened projects in your workspace to resolve artifacts, even if the project was not yet installed in your local maven repository (command mvn install).

For example, you have 2 projects A and B. A depends of B. If you are working on both projects, and both are opened in your workspace. Eclipse will automatically build the project B when it resolves the dependencies of the project A. Thus, when you will run the project A, Eclipse will ensure you are using the lastest version of the project B.

If you use Maven directly to run the project A, Maven will get the project B version from your local repository. And thus, you have to install the project B before running A to have the latest version (mvn install B).



来源:https://stackoverflow.com/questions/34725294/m2eclipse-resolving-maven-dependencies-from-the-eclipse-workspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!