Dave Mulligan
Dave Mulligan

Reputation: 1673

What determines the behavior of a Maven dependency when loading resources?

I have an Eclipse Maven project that has dependencies on other projects in my workspace. I have defined them all as imports from my local repository, and the dependency statements in pom.xml all appear consistent, for example:

<dependency>
  <groupId>com.mycompany.mygroup</groupId>
  <artifactId>Project1</artifactId>
  <version>1.0.2</version>
</dependency>

<dependency>
  <groupId>com.mycompany.mygroup</groupId>
  <artifactId>Project2</artifactId>
  <version>2.0.1</version>
</dependency>

However, some of the projects appear in my build path to be binary jar files, with this icon:binary icon, and others appear with what I think is the "workspace project" icon: project icon.

This becomes an issue when I try to load resources contained within the JAR file: the projects with the "binary jar" icon load resources successfully from within the JAR; the projects with the "workspace project" icon try to go to the file system for the resource instead.

What is it about the projects that determines how they appear in the list of Maven dependencies? I've checked the "natures" tag in the .project file, and they are identical.

Upvotes: 1

Views: 192

Answers (1)

Ken Chan
Ken Chan

Reputation: 90467

This behaviour is controlled by the option Maven --> Disable/Enable Workspace Resolution which has the following behaviour (source)

This has the effect of altering the way that Maven locates dependency artifacts. If a project is configured to resolve dependencies from the workspace, these artifacts do not need to be present in your local repository. Assume that project-a and project-b are both in the same Eclipse workspace, and that project-a depends on project-b. If workspace resolution is disabled, the m2eclipse Maven build for project-a will only succeed if project-b's artifact is present in the local repository. If workspace resolution is enabled, m2eclipse will resolve the dependency via the Eclipse workspace. In other words, when workspace resolution is enabled, project's don't have to be installed in the local repository to relate to one another.

That means if Workspace Resolution is disable , all dependencies must be read from the maven repository and will be displayed as enter image description here.

Other the other hand , if Workspace Resolution is enable and your workspace contains a open project which all of its artifacts information (groupId , artifactId , version) are the same as what defined in the <dependency> , that dependencies will directly refer to the source codes of this open project. The icon will be displayed as enter image description here

I don’t think that can be the sole factor, because some of the dependencies with the “binary jar” icon are also open in my workspace.

I think most probably it is because the <version> that you use in the pom.xml are different from the declared <version> of that project in the workspace.

Upvotes: 1

Related Questions