35

How can I reference another workspace project using Eclipse m2e?

Do I have to add a project dependency in the project setting? But in that case the dependency is not shown in the pom.

If I set them in the pom, it will not reference the project in workspace but reference the jar in the local repository. Quite annoying, anyone can help?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Wudong
  • 2,320
  • 2
  • 32
  • 46
  • 2
    Did you enable "Resolve dependencies from Workspace projects" in the Maven configuration? – Thomas May 21 '12 at 15:44

5 Answers5

62

The correct way to do this is the following:

  • Use the dependencies section in the POM file exclusively, don't fiddle with the Eclipse project references. Right-click the project, then select Maven > Update Project Configuration to reset the project to the Maven default settings. This way, m2e has ownership of the dependencies.
  • Make sure all referenced projects are open in Eclipse and have the Maven nature enabled.
  • Check the Maven settings for each project, make sure that groupId, artifactId and version match with the projects you have open in Eclipse. So if the project you depend on has version 1.0.0-SNAPSHOT in Eclipse, make sure that the depending project's POM file references version 1.0.0-SNAPSHOT in the dependencies section.
  • Enable Workspace Resolution for each of the projects. Right-click the project, then Maven > Enable Workspace Resolution.
  • Finally, if the projects are still not resolved, right-click the project again, then Maven > Update Project

This should solve your problem. If after this, your dependencies are still referenced from the file system, check the groupId, artifactId and especially version of each dependency again.

Also check if you don't have any errors in your project - try to run Maven install.

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 2
    this helped me, but the referenced projects are deployed as normal folder instead of an jar file, is there a way to change this ?many thanks in advance – Khinsu Jul 10 '14 at 06:49
  • 1
    I was in need of the opposite solution (wanted to have the jar within the local repo take precedence over the project reference) and by disabling the "workspace resolution" feature I was able to get it done! Thanks for the pointer in the right direction... – Matthias Steiner Nov 27 '14 at 15:12
  • Sounds silly but in addition to point 2 _"Make sure all referenced projects are open in Eclipse and have the Maven nature enabled"_: **Make sure all referenced projects are up to date**. If workspace resolution works correctly the dependencies are shown with an eclipse project folder icon instead of the jar icon in the Maven POM editor. – andy Sep 04 '15 at 13:25
  • Any other ideas? - All projects are open and have Maven nature enabled. - groupId, artifactId, and version all match. - Workspace resolution is enabled for all projects. - Maven > Update Project doesn't help. Maven install works, and that's the only way I can get changes in the dependent project to show up. What's strange is I have another set of projects with *exactly* the same setup, and those resolve correctly. The Maven dependency points to the project in Eclipse, not the JAR in the repository. But not for my current project. – lukpac Apr 01 '16 at 19:18
  • 2
    Following up to my own comment, the issue was a case mismatch. The names for these projects are mixed case. The artifact IDs were supposed to be all lowercase, but I had made them mixed case as well. That broke workspace resolution in Eclipse, but not from my local repository for some reason. I corrected the case mismatch, and now workspace resolution works correctly. – lukpac Apr 04 '16 at 19:21
  • I have tried all the above points. but still I am getting the error: Failed to execute goal on project abc: Could not resolve dependencies for project abc:abc:ear:0.0.1-SNAPSHOT: Failure to find xyz:xyz:war:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1] [ERROR] – Surodip May 02 '16 at 08:17
  • I had done everything mentioned here. But still no luck. Finally, I realized that my project structure is not according to the Maven Directory Layout: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html. Fixed the structure and it started working. – Chirag Agrawal Oct 26 '17 at 05:18
  • Misleading is that although Maven uses the local workspace projects and all is working fine, 'they will not appear under projects in the build path view'. Instead they will still be listed in the "Maven dependencies" library, just with a project, not a jar icon. – JRA_TLL Feb 21 '19 at 10:33
8

I'd go even further than this. If you've ever run mvn eclipse:eclipse on your project then you're probably in trouble. I had a situation where I had both a "Referenced Libraries" section and a "Maven Dependencies" section in my eclipse project, with conflicting library versions, causing eclipse and myself inevitable confusion.

The safest thing I found was to run mvn eclipse:clean from the command line then go back in to eclipse, refresh the project, "OK" the resulting problem dialog, and then go Maven > Update Project. This sorted it all out for me.

Huw Roberts
  • 146
  • 1
  • 5
6

When eclipse is messed up with importing and deleting several projects, you may need to rebuild index of maven repositories. Here is a way that I have done.

  1. Check if an referenced project is recognized as a maven project by eclipse properly.

    In menu bar, click Window -> Show View -> Other...
    When 'Show View' window pops up, select Maven -> Maven Repositories
    In Maven Repositories window, You should see your project as jar file in Local Repositories -> Workspace Projects
    If you can not find your project in Workspace Projects, right click on Workspace Projects and select Rebuild Index.

  2. Update maven of an referencing project

    Right click on the referencing project, Maven -> Update Project... -> OK

Community
  • 1
  • 1
heemin
  • 321
  • 3
  • 12
1

You also need to make sure that you are running the correct goals.

If you don't run the install goal then it won't be copied to your repository and won't compile.

To learn more about goals have a look at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

  • The OP is about getting the dependencies resolved from the Eclipse workspace, not the local repository - or more precisely: how to resolve dependencies in the local workspace without installing them. – Guss Jul 07 '19 at 09:06
1

With kudos to @nwinkler's response, the main problem is matching the version number.

A common scenario is that if you are developing a set of projects that are version lock-steped with each other - for example, a project and a set of library projects that are not very loosely coupled, such that a library API might change in a version to be consumed by the relevant app project version, but may change in a way that is incompatible with a past or future version of the app project.

The correct way to set Maven dependencies in such a configuration (and it is also the recommended practice) is to have the app consume specific versions of the libraries - so, for example, if you rebuild an old version of the app, it will use the library version that it previously compiled with.

With the app project's POM library dependency set to a release version (lets say 1.0.0), and while working on the next release with both the app and library projects set to a SNAPSHOT release (lets say 2.0.0-SNAPSHOT), the m2e will not resolve the library version correctly, and will likely download an old version, so that trying to use Eclipse features like "Open Decleration" will target the download jar (sometimes without even a source attachment) which can be pretty annoying.

One way to work around that is to set the app POM dependency version to a range, so instead of depending on 1.0.0, you'd depend on [1.0.0-). With an open range like that, m2e will happily find your workspace library project. But you'd want to set it back to the "correct" version before committing, building and publishing - and this can be very error prone.

My solution is to use build profiles and set a custom profile for m2e, like this:

  • Set your dependency version with a property, to the version you want to publish against:
...

<properties>
<my.library.version>1.0.0</my.library.version>
</properties>

<dependencies>
  <dependency>
    <groupId>my.group</groupId>
    <artifactId>my.library</artifactId>
    <version>${my.library.version}</version>
  </dependency>
</dependencies>

...
  • Then add a profile section with an active by default profile that does nothing, and an Eclipse-specific profile that overrides the library version property with a range:
...

<profiles>
    <profile>
        <id>default</id>
        <activation><activeByDefault></activeByDefault></activation>
    </profile>
    
    <profile>
        <id>eclipse</id>
        <properties>
            <my.library.version>[1,)</my.library.version>
        </properties>
    </profile>
</profiles>

...
  • Finally go to your project properties, and under "Maven" type "eclipse" into "Active Maven Profiles":

enter image description here

  • Then "Apply and close".

Eclipse m2e will then always see the version range and will resolve dependencies from the eclipse project (even if you have the library installed in the local Maven repo, as the Eclipse project will have a higher version number), but other builders will see the original, strict, version number.

Community
  • 1
  • 1
Guss
  • 30,470
  • 17
  • 104
  • 128
  • 1
    Nice catch @Guss! Just would like to add you don't really need a default empty active profile and there's a way to create a eclipse aware profile using m2e properties to activated it: m2e.version. This way is a little bit cleaner IMHO. Tks. – Reginaldo Santos Oct 03 '19 at 16:29