Copy a configuration and add additional dependencies before resolving the copy

Hello,

as part of a task I would like copy a resolved configuration (in the example below ‘compile’), add some additional dependencies and resove the copy. It is important for my use case to add additional dependencies in between of resolving the configuration. The code snippet below defines the configuration ‘compile’ with the dependency ‘spring-jdbc’ and the copied configuration adds the dependency ‘spring-ws-core’. But ‘spring-ws-core’ will be not added to the resolved artifacts. The number of the resolved artifacts is in both cases 7.

repositories {
  mavenCentral()
}
  configurations {
  compile {
        transitive = true
  }
}
  dependencies {
  compile (group: 'org.springframework', name: 'spring-jdbc', version: '2.5.6')
}
      task myTask << {
  def compileConf = project.configurations.getByName('compile')
  println "State of configuration 'compile': " + compileConf.state //UNRESOLVED is expected
  println "Resolve configuration and print count of resolved artifacts: " + compileConf.resolvedConfiguration.resolvedArtifacts.size()
  println "State of configuration 'compile': " + compileConf.state //RESOLVED is expected
    def compileCopyConf = compileConf.copy()
  println "State of configuration 'compileCopy': " + compileCopyConf.state //UNRESOLVED is expected
  println "compileCopyConf.size: " + compileCopyConf.dependencies.size()
  compileCopyConf.dependencies.add(['org.springframework.ws:spring-ws-core:2.2.0.RELEASE'])
  println "State of configuration 'compileCopy': " + compileCopyConf.state //UNRESOLVED is expected
  println "compileCopyConf.size: " + compileCopyConf.dependencies.size()
  println "Resolve configuration and print count of resolved artifacts: " + compileCopyConf.resolvedConfiguration.resolvedArtifacts.size()
  println "State of configuration 'compileCopy': " + compileCopyConf.state //RESOLVED is expected
}

Output:

Executing command: ":myTask"
:myTask
State of configuration 'compile': UNRESOLVED
Resolve configuration and print count of resolved artifacts: 7
State of configuration 'compile': RESOLVED
State of configuration 'compileCopy': UNRESOLVED
compileCopyConf.size: 1
State of configuration 'compileCopy': UNRESOLVED
compileCopyConf.size: 2
Resolve configuration and print count of resolved artifacts: 7
State of configuration 'compileCopy': RESOLVED
  BUILD SUCCESSFUL
  Total time: 12.167 secs
  Completed Successfully

Can somebody advice? Kinde Regards Markus

The ‘compileCopyConf.dependencies.add()’ method expects a ‘Dependency’ object. You are passing a dependency notation string. The ‘DependencySet’ does not know how to resolve that string. You can however create a ‘Dependency’ by calling ‘DependencyHandler.create()’. Update the line of code where you are adding the dependency to look like this.

compileCopyConf.dependencies.add(dependencies.create(‘org.springframework.ws:spring-ws-core:2.2.0.RELEASE’))

Hi Mark,

thanks a lot. It is working after I called the method ‘getDependencies()’ (Instead of calling the property ‘dependencies’):

compileCopyConf.dependencies.add(getDependencies().create('org.springframework.ws:spring-ws-core:2.2.0.RELEASE'))

If I am not wrong, then the API (gradle 2.1) supports the notation string. It seems for me that there is a bug.

Kind Regards Markus

The difference is that ‘compileCopyConf.dependencies’ returns a ‘DependencySet’, which is really just a ‘Collection’, whereas ‘project.dependencies’ returns a ‘DependencyHandler’. The ‘DependencySet.add()’ method is actually just ‘java.util.Set.add()’, which obviously has no way of interpreting a dependency notation string.