Cannot add dependency to configuration in a custom task


(tulsoba) #1

In a custom gradle task that I run in a gradle war project I am trying to add some existing dependencies to the providedCompile configuration

Configuration providedCompileConf = project.getConfigurations().findByName("providedCompile");
    if(providedCompileConf==null) {
      throw new RuntimeException("This task is not supported");
    }
    List<ResolvedDependency> transitiveDependencies = computeTransitiveDeps(project);
    for (ResolvedDependency dep : transitiveDependencies) {
      providedCompileConf.getAllDependencies().add(buildDependency(dep));
    }
  }
    private Dependency buildDependency(final ResolvedDependency dep) {
    return new Dependency() {
              @Override
      public String getVersion() {
        return dep.getModuleVersion();
      }
              @Override
      public String getName() {
        return dep.getModuleName();
      }
              @Override
      public String getGroup() {
        return dep.getModuleGroup();
      }
              @Override
      public Dependency copy() {
        // Hm will this work?
        return this;
      }
              @Override
      public boolean contentEquals(Dependency dependency) {
        return false;
      }
    };
  }

but I get this error:

add() is not supported on CompositeCollection without a CollectionMutator strategy

Is there a simpler way to add dependencies to a configuration or am I missing some basic knowledge here?


(Luke Daley) #2

Use ‘providedCompileConf.getDependencies()’ instead of ‘getAllDependencies()’.


(tulsoba) #3

Thanks! But how do I add a classifier like: configurationName “group:name:version:classifier” taken from: http://gradle.org/docs/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html

Have tried to replace with a ExternalModuleDependency but it does not seem to have a classifier either


(Luke Daley) #4

You should use…

providedCompileConf.getDependencies().add(project.dependencies.create(«dependency notation»))


(tulsoba) #5

The task is written in java - is there an equivalent for the above for java or is it only possible to do this kind of operation from groovy?


(Luke Daley) #6

‘providedCompileConf.getDependencies().add(project.getDependencies().create(«dependency notation»))’


(tulsoba) #7

Doh :slight_smile: After creating it is there a method to see if it resolves from code? It could in theory be non existent.


(Luke Daley) #8

You can only resolve a configuration once. So you’ll have to add them all, then trigger a resolve. Easiest way to do that is ‘providedCompileConf.files’.