Uploaded image for project: 'ShrinkWrap'
  1. ShrinkWrap
  2. SHRINKWRAP-345

MavenDependencyResolver resolves wrong version or scope for transitive dependencies when using <dependencyManagement>

    Details

    • Workaround Description:
      Hide

      Include each transitive dependency in the <dependencies> section

      Show
      Include each transitive dependency in the <dependencies> section

      Description

      Using includesDependenciesFromPom() followed by resolveAsFiles() can produce a transitive dependency with the wrong scope or version if it is defined in the <dependencyManagment> section of the POM.

      e.g.:
      <dependencyManagement>
      <dependencies>
      <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
      </dependency>
      </dependencies>
      </dependencyManagement>
      <dependencies>
      <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.7.0</version>
      <exclusions>
      <exclusion>
      <artifactId>servlet-api</artifactId>
      <groupId>javax.servlet</groupId>
      </exclusion>
      </exclusions>
      </dependency>
      <dependencies>

      common-beanutils depends on common-logging:1.0.3 but we've specified a higher version in the <dependencyManagement> section. A 'mvn package' will put common-logging:1.1 in the WEB-INF/lib directory but the following code will spit out a version of 1.0.3:

      File[] files =
      DependencyResolvers.use( MavenDependencyResolver.class )
      .useCentralRepo( false )
      .configureFrom( System.getProperty( "user.home" ) + "/.m2/settings.xml" )
      .includeDependenciesFromPom( "pom.xml" )
      .resolveAsFiles( new ScopeFilter( "compile", "runtime" ) );

      for ( File f : files )

      { System.out.println( f.getName() ); }

        Gliffy Diagrams

          Activity

          Hide
          mtpettyp Mike Pettypiece added a comment - - edited

          Example project attached

          Show
          mtpettyp Mike Pettypiece added a comment - - edited Example project attached
          Hide
          mtpettyp Mike Pettypiece added a comment -

          This appears to occur because the transitive dependencies are not loaded in includedDependenciesFromPom(). By the time the transitive dependencies are loaded (in resolveAsFiles()) none of the POM dependencyManagement information is available.

          Show
          mtpettyp Mike Pettypiece added a comment - This appears to occur because the transitive dependencies are not loaded in includedDependenciesFromPom(). By the time the transitive dependencies are loaded (in resolveAsFiles()) none of the POM dependencyManagement information is available.
          Hide
          kpiwko Karel Piwko added a comment -

          Thanks for the test. I'll try it to on newest development branch, where we reworked the internals and dependencyManagement was added to Aether resolution, which should fix your issue.

          Show
          kpiwko Karel Piwko added a comment - Thanks for the test. I'll try it to on newest development branch, where we reworked the internals and dependencyManagement was added to Aether resolution, which should fix your issue.
          Hide
          kpiwko Karel Piwko added a comment -

          Fixed in https://github.com/kpiwko/resolver/commit/a00fa3ca5c1fad9455c55600650b8e2ad169bd18

          Usage:

              @Test
              public void testIncludeFromPomWithDependencyManagement() {
                  File[] jars = DependencyResolvers.use(MavenDependencyResolver.class).useCentralRepo(false)
                          .loadEffectiveFromPom("target/poms/test-depmngmt-transitive.xml").importAllDependencies().resolveAsFiles();
           
                  Assert.assertEquals("Exactly 2 files were resolved", 2, jars.length);
                  new FileValidationUtil("test-deps-b-2.0.0", "test-deps-c-1.0.0").validate(jars);
           
              }
          

          Note: API is suspect to change.

          Show
          kpiwko Karel Piwko added a comment - Fixed in https://github.com/kpiwko/resolver/commit/a00fa3ca5c1fad9455c55600650b8e2ad169bd18 Usage: @Test public void testIncludeFromPomWithDependencyManagement() { File[] jars = DependencyResolvers.use(MavenDependencyResolver.class).useCentralRepo(false) .loadEffectiveFromPom("target/poms/test-depmngmt-transitive.xml").importAllDependencies().resolveAsFiles();   Assert.assertEquals("Exactly 2 files were resolved", 2, jars.length); new FileValidationUtil("test-deps-b-2.0.0", "test-deps-c-1.0.0").validate(jars);   } Note: API is suspect to change.
          Hide
          kpiwko Karel Piwko added a comment -

          Pushed upstream.

          Show
          kpiwko Karel Piwko added a comment - Pushed upstream.

            People

            • Assignee:
              kpiwko Karel Piwko
              Reporter:
              mtpettyp Mike Pettypiece
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development