# Adding dependencies to a configuration created from a custom source set

**URL:** <https://discuss.gradle.org/t/adding-dependencies-to-a-configuration-created-from-a-custom-source-set/8801>\
**Category:** Help/Discuss\
**Created:** [April 10, 2015, 7:34pm UTC](https://discuss.gradle.org/t/adding-dependencies-to-a-configuration-created-from-a-custom-source-set/8801 "2015-04-10T19:34:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![primIl](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@primIl](https://discuss.gradle.org/u/primIl)\
**Post date:** [April 10, 2015, 7:34pm UTC](https://discuss.gradle.org/t/adding-dependencies-to-a-configuration-created-from-a-custom-source-set/8801/1 "2015-04-10T19:34:32Z")

</div>

How can one programmatically add dependency to a configuration created from a custom source set? The configuration object gets created, but then when trying to add dependency it complains:

```
task t3 << {
  project.apply(plugin:'java-base')

  def sourceSet1 = project.sourceSets.create('sourceSet1')
  def compile1 = configurations.getByName(sourceSet1.compileConfigurationName)

  println "My configuration: " + compile1

  dependencies {
     compile1 files('xxx', 'yyy')
  }
}

```

This is producing the following error:

```
$ gradle t3 
:t3
My configuration: configuration ':sourceSet1Compile'
:t3 FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/user/code/intellij/Proj1/build.gradle' line: 256

* What went wrong:
Execution failed for task ':t3'.
> Could not find method call() for arguments [file collection] on configuration ':sourceSet1Compile'.

```

On the other hand, for explicitly created configurations, it works normally:

```
configurations { compile2 }
dependencies {
    compile2 files('xxx', 'yyy')
}
```

---

<div class="post-metadata">

**Author:** ![primIl](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@primIl](https://discuss.gradle.org/u/primIl)\
**Post date:** [April 10, 2015, 10:58pm UTC](https://discuss.gradle.org/t/adding-dependencies-to-a-configuration-created-from-a-custom-source-set/8801/2 "2015-04-10T22:58:48Z")

</div>

The following works:

```
dependencies.add(compile1.name, files('xxx', 'yyy'))
```

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [April 11, 2015, 12:34pm UTC](https://discuss.gradle.org/t/adding-dependencies-to-a-configuration-created-from-a-custom-source-set/8801/3 "2015-04-11T12:34:32Z")

</div>

Just a few notes…

```gradle
task t3 << {
  project.apply(plugin:'java-base')

```

This is applying a plug-in during the [“execution” lifecycle](https://gradle.org/docs/current/userguide/build_lifecycle.html). I’m surprised this works, but it’s going to cause problems if you leave it like this. All of this configuration should be done outside a task block.

```gradle
  def compile1 = configurations.getByName(sourceSet1.compileConfigurationName)
  dependencies {
     compile1 files('xxx', 'yyy')
  }

```

The error you’re getting takes a little explaining of what Groovy’s doing here in the “normal” case. `dependencies` is really a [`DependencyHandler`](https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html). When you say `compile1 files(...)`, Groovy looks for a method called “compile1” on DependencyHandler. When it doesn’t find one, it calls `methodMissing()` passing “compile1” and the arguments. We’ve implemented `methodMissing()` to look for configurations with the missing method name and add dependencies to it (basically doing exactly what you found that works). So you can’t assign a configuration name to a variable and get the same behavior. We’ll look for a configuration with the variable’s name and not the value of the variable.

We’re doing something similar to what you’re trying in one of the samples:

> <https://github.com/gradle/gradle/blob/master/subprojects/docs/src/samples/java/withIntegrationTests/build.gradle#L13>

We’re just relying on the convention that new source sets create configurations with the names "sourceSet"Compile/Runtime.
