8

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

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • What exactly is your setup? If you're using Maven with Eclipse, you will have to install M2Eclipse otherwise you won't be able to work. – Tunaki Jan 11 '16 at 15:34
  • I am just trying to find out if M2Eclipse could be a useful tool for us. I have not set up anything yet. – J Fabian Meier Jan 11 '16 at 15:37
  • Actually, I was asking about what you meant by "project in workspace corresponds to two different jars" (sorry) – Tunaki Jan 11 '16 at 15:37
  • We have cases in which one java project is used to create two different jars (in the context of Java EE, where a client jar is separated from the rest). Therefore there is no 1:1 between projects and jars, so that I need to find out how clever or customizable M2Eclipse is. – J Fabian Meier Jan 11 '16 at 15:39
  • Are you currently building your project with Maven? – Tunaki Jan 11 '16 at 15:40
  • No. I am just trying to gather information about a suitable build tool. – J Fabian Meier Jan 11 '16 at 15:43
  • I fear this is a bit too broad then. Since I'm a Maven enthusiast, I'd obviously recommend it :). I'm sure what you want to do can be done with it. – Tunaki Jan 11 '16 at 15:46
  • I'm pretty sure that maven isn't really capable of producing two totally separate artifacts (ie, JARs) from a single module (project). It can produce related artifacts (eg, a JAR, a sources JAR, a tests JAR, and Javadoc, etc) but all inherently related. I've never seen a Maven pom that defines totally separate artifacts like you're describing. – E-Riz Jan 11 '16 at 16:20

2 Answers2

14

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.

Isabella Almeida
  • 789
  • 7
  • 12
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).

Quentin
  • 481
  • 6
  • 13