title: Gradle cannot change dependencies of configuration
body: <p>"refresh all gradle projects" in Intellij Idea gives me following error:</p>
  
  <pre><code>Error:(15, 0) Cannot change dependencies of configuration ':util:compile' after it has been included in dependency resolution.
  </code></pre>
  
  <p>The problem doesn't occur when I move second if statement to build.gradle of submodule project. While I found workaround I would like to understand why original solution is not correct. The line making trouble is:</p>
  
  <pre><code>"Class-Path": (configurations.compile.collect {"$dependencyDir/$it.name"}.join(" "))
  </code></pre>
  
  <p>Project structure:</p>
  
  <pre><code>mcv-gradle-example
  |__ submodule (gradle project)
  |__ util (gradle project)
  |__ build.gradle
  </code></pre>
  
  <p>build.gradle:</p>
  
  <pre><code>allprojects {
      apply plugin: 'java'
  }
  
  subprojects {
      apply plugin: 'scala'
  
      repositories {
          mavenCentral()
      }
  
      def isSpecialProject = project.name in ["submodule"]
  
      dependencies {
          compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.1'
          if (isSpecialProject) {
              compile project(':util')
          }
      }
  
      if (isSpecialProject) {
          def dependencyDir = "/deps"
  
          task setupJarManifest() {
              jar.manifest.attributes(
                      "Class-Path": (configurations.compile.collect {"$dependencyDir/$it.name"}.join(" ")))
          }
  
          jar.dependsOn setupJarManifest
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong>
  I found out that embracing <code>jar.manifest...</code> with <code>afterEvaluate</code> allows having this task in main build.gradle. However I'm still not sure what was the gradle's problem.</p>
  
  <p><strong>Update 2:</strong> 
  It seems moving setting manifest.attributes to the execution phase solves he problem. My final solution is:</p>
  
  <pre><code>if (isSpecialProject) {
      def dependencyDir = "deps"
  
      jar {
          doFirst {
              manifest.attributes(
                  "Class-Path": (configurations.compile.collect {"$dependencyDir/$it.name"}.join(" "))
              )
          }
      }
  }
  </code></pre>
  
  <p>My guess is:
  When submodule project uses <code>configurations.compile</code> in configuration phase, it's all dependencies are resolved. Then the subprojects closure is  evaluated for util project. Since util is dependency of submodule it can't have new dependency added as one moment ago we used it's all dependencies. It's reasonable but still I don't know how usage of configurations.compile "marks" util's dependencies as closed.</p>
  
  <p><strong>Explanation</strong></p>
  
  <p>After some more research I finally know what happened:</p>
  
  <ol>
  <li>Subproject <code>submodule</code> is evaluated first. It puts scala and subproject <code>util</code> into its dependencies. </li>
  <li>Then gradle evaluates <code>configurations.compile.collect</code> which internally calls <code>iterator</code> method calling <code>DefaultConfiguration#getFiles</code>. To find what files are specified for this configurations it resolves graph and artifacts (locking it's dependencies modification abilities).</li>
  <li>Then subprojects closure is evaluated for subproject <code>util</code>. It is trying to put scala in its dependencies but is locked because of point 2 so it throws an error.</li>
  </ol>
  
  <p>Moving <code>collect</code> to <code>afterEvaluation</code> or <code>doLast</code> executes it after correct resolution of graph and artifacts. It can be called as many times as you want in execution phase.</p>