Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Transitive Dependency Resolution when Declared in Profiles #1800

Open
hankolerd opened this issue Aug 9, 2024 · 1 comment
Open

Comments

@hankolerd
Copy link

hankolerd commented Aug 9, 2024

There are some issues with transitive dependency resolution when a dependency uses profiles to select from one or more artifacts:

  1. Dependencies declared in any profile marked as <activeByDefault>true</activeByDefault> are already resolved even if that profile is de-selected in Maven Profiles options
  2. Dependencies from any non-default profile are not transitively resolved even if requesting that profile to be enabled in the Maven Profiles options

My expectation was that enabling/disabling profiles in Maven Profiles configuration of a project would result in identical resolved class path as Maven would see if building the project from command line.

The project structure is as follows:

|----old-component
|  |----pom.xml
|----new-component
|  |----pom.xml
|----consumers
|  |----module1
|  |  |----pom.xml
|  |----module2
|  |  |----pom.xml
|  |----pom.xml
|----pom.xml

If a project, 'module1', swaps a compile dependency based on enablement of one of two profiles, 'old' or 'new', like so:

<profiles>
  <profile>
    <id>old</id>
    <dependencies>
    <dependency>
      <groupId>test</groupId>
      <artifactId>old-component</artifactId>
    </dependency>
    </dependencies>
  </profile>
  <profile>
    <id>new</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <dependencies>
      <dependency>
	<groupId>test</groupId>
	<artifactId>new-component</artifactId>
      </dependency>
    </dependencies>
  </profile>
</profiles>

When another project, 'module2', depends on this project, M2Eclipse only ever resolves the transitive dependencies in the "activeByDefault" profile ( 'new'), even when manually selecting the 'old' profile for enablement:

Screenshot 2024-08-09 at 3 07 39 PM

Additionally, if you remove the default profile activation:

-  <activation>
-    <activeByDefault>true</activeByDefault>
-  </activation>

Then neither dependency is transitively resolved for 'module2':
Screenshot 2024-08-09 at 3 15 17 PM

In both of these cases, I would have expected 'old-component', which has dependency declared through 'old' profile, should have been on the project class path, since that profile is selected for enablement.

Attached is a sample project to reproduce the issue.
M2E-wrong-classpath-profile-override.zip

Environment
Operating System: MacOS 15.0
M2E Version: 2.6.1.20240602-2342
Eclipse Version: 4.32.0.v20240601-0610
Java Version: OpenJDK 64-Bit Server VM Temurin-21.0.4+7

@hankolerd hankolerd changed the title Wrong Dependency Tree resolved when an activeByDefault Profile is overridden with another profile Incorrect Transitive Dependency Resolution when Declared in Profiles Aug 9, 2024
@hankolerd
Copy link
Author

Actually I need to add some clarification to this issue. By default, Maven does not take into account transitive dependencies coming through profiles in dependencies. i.e. building 'module2' with -Pnew or -Pold does not bring the transitive deps of 'module1'

In the project POM's at my company where I observed this problem, they have declared an additional profile activation through property:

<profiles>
  <profile>
    <id>old</id>
    <activation>
      <property>
        <name>old</name>
      </property>
    </activation>
  </profile>
  <profile>
    <id>new</id>
    <activation>
      <activeByDefault>true</activeByDefault>
      <property>
        <name>new</name>
      </property>
    </activation>
  </profile>
</profiles>

In this case, Maven will activate profiles and honor transitive dependencies in the profiles of dependency POM's if the property is set in the build, i.e. mvn clean install -Pold -Dold or mvn clean install -Pnew -Dnew.

As such, I suspect this is a pretty extreme edge case to swap compile-time dependencies based on build profiles, considering getting even Maven to honor the transitives is kind of relying on a hack and doesn't appear that they really intended to support it by design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant