4 Replies Latest reply on Mar 4, 2014 4:36 AM by kpiwko

    What is the replacement of MavenDependencyResolver in ShrinkWrap Resolver 2.0.2

    oranheim

      I'm trying to refactor a method resolveDependency(..) using org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven:jar:2.0.2. I'm not able to figure out what is the correct way. Earlier I used org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven:jar:1.0.0-beta-7.

       

      /**
       * Resolve maven dependency graph with transitive resolution
       * 
       * @param qualifiedNamed
       * @param exclusionPattern
       * @return Collection of JavaArchive
       */
      public static final Collection<JavaArchive> resolveDependency(final String qualifiedNamed, final String... exclusionPattern) {
          MavenDependencyResolver dependencies = DependencyResolvers.use(MavenDependencyResolver.class);
          dependencies = dependencies.loadMetadataFromPom("pom.xml");
          if (!NULL_STRING.equals(customPOM.get())) {
              dependencies = dependencies.loadMetadataFromPom(getCustomPOM());
          }
          dependencies = dependencies.useCentralRepo(false);
          dependencies = dependencies.artifact(qualifiedNamed);
          if (exclusionPattern != null) {
              for (String exclude : exclusionPattern) {
                  dependencies = dependencies.exclusion(exclude);
              }
          }
          return dependencies.resolveAs(JavaArchive.class);
      }
      

       

      Here's my findings so far:

       

      1. MavenDependencyResolver is obsolete and is replaced by the Maven singleton
      2. Calling Maven.resolver().loadPomFromFile("pom.xml") replaces line 10
      3. How can I call another custom pom and have the dependency graph merged, line 12?
      4. PomEquippedResolveStage-instance.resolve(qualifiedNamed).withTransitivity() replaces line 15
      5. How to solve artifact.exclusion on line 18?
      6. How to return a Collection<JavaArchive> from PomEquippedResolveStage?

       

      Please advise.

        • 1. Re: What is the replacement of MavenDependencyResolver in ShrinkWrap Resolver 2.0.2
          kpiwko

          Hi Ove,

           

          would documentation help?

           

          resolver/README.asciidoc at master · shrinkwrap/resolver · GitHub

           

          Refactoring your snippet, it would look similar to code below. I believe that you want to use option2. Also, strategies are quite powerful, so you can implement your own or check documentation

          to find out more.

           

          public static final Collection<JavaArchive> resolveDependency(final String qualifiedNamed, final String... exclusionPattern) {
          
          
                  MavenResolverSystem resolver = Maven.resolver();
                  resolver.loadPomFromFile("pom.xml");
                  if (!NULL_STRING.equals(customPOM.get())) {
                      resolver.loadPomFromFile((getCustomPOM());
                  }
          
          
                  // following constructions will just add exclusions to dependency defined by qualified name only
                  List<MavenDependencyExclusion> exclusions = new ArrayList<MavenDependencyExclusion>();
                  for (String exclusion : exclusionPattern) {
                      exclusions.add(MavenDependencies.createExclusion(exclusion));
                  }
                  
                  
                  JavaArchive[] option1 = resolver.addDependencies(MavenDependencies.createDependency(qualifiedNamed,
                      ScopeType.COMPILE,
                      false,
                      exclusions.toArray(new MavenDependencyExclusion[0])))
                      .resolve()
                      .withTransitivity()
                      .as(JavaArchive.class);
                  
                  // original snippet rather seem to define global exclusions, that would be mean following code
                  JavaArchive[] option2 = resolver.resolve(qualifiedNamed).using(new RejectDependenciesStrategy(exclusionPattern)).as(JavaArchive.class);
                  
                  return option1 or option2 casted as collection;
              }
          

           

          Cheers,

           

          Karel

          1 of 1 people found this helpful
          • 2. Re: What is the replacement of MavenDependencyResolver in ShrinkWrap Resolver 2.0.2
            oranheim

            Thanks a lot Karel and for providing the refactored snippet!

             

            This was very useful. I wasn't aware of the documentation.

            • 3. Re: Re: What is the replacement of MavenDependencyResolver in ShrinkWrap Resolver 2.0.2
              oranheim

              Hi Karel,

               

              I'm able to resolve dependencies using ("G:A") with shrinkwrap-resolver-maven-plugin.

               

              However, is it still possible to obtain version resolution without the maven plugin?

               

              Maven.resolver().loadPomFromFile("pom.xml").importCompileAndRuntimeDependencies().resolve("org.hibernate:hibernate-core").withTransitivity().as(JavaArchive.class)
              

               

              Because the above snippet fails:

               

              org.jboss.shrinkwrap.resolver.api.ResolutionException: Unable to get version for dependency specified by org.hibernate:hibernate-core:compile:, it was either null or empty.
                at org.jboss.shrinkwrap.resolver.impl.maven.ResolveStageBaseImpl.resolveVersion(ResolveStageBaseImpl.java:205)
              

               

              Cheers,

              Ove

              • 4. Re: What is the replacement of MavenDependencyResolver in ShrinkWrap Resolver 2.0.2
                kpiwko

                Hi Ove,

                 

                what do you mean exactly by it works with maven plugin? Can you provide a code snippet?

                 

                As for missing org.hibernate:hibernate-core version, there are two things to verify:

                1. Is this defined as <dependency> in <dependencyManagement> or <dependencies> section either in pom you load or or predecessors? If not, version cannot be determited
                2. Is hibernate-core defined in BOM and import'ed in dependencyManagement - that would be a bug in SWR
                3. Does scope have any effect? E.g. would it find version if you change scope to compile? - thaw would be a bug in SWR

                 

                As for importCompileAndRuntimeDependencies(), you most likely don't need that method at all. It adds all compile and runtime scoped dependencies to resolution. Which means it actually adds hibernate-core as well if it is defined in pom.xml as compile or runtime scoped dependency. If you are interested just in version resolution, just loading POM is enough.

                 

                Cheers,

                 

                Karel