title: Setting up a multi-project Gradle build
body: <p>I'm setting up a multi-module Gradle build for a legacy system at work (replacing the current Ant build). However, I'm new to Gradle, and I'm not sure what's the best way to do it. And I want to do it right, because this build script will be around for a long time. I have found a way to do things that works, but when I google around and read answers on StackOverflow, I see people using a different way, which --in my case-- doesn't work. But maybe I'm doing something wrong. I've also been reading in the Gradle in Action book, but haven't found this particular issue there.</p>
  
  <p>I have a build.gradle file in the root of my project, with a bunch of subdirectories that each contain a sub-project. Most of these are regular Java projects, but there are some Wars and Ears in there, too, which require some special packaging love. Each sub-project has its own build.gradle file which, at this point, only contains a list of dependencies, nothing more.</p>
  
  <p>The build.gradle file in the root of my projects looks something like this (I left out the War stuff for brevity):</p>
  
  <pre><code>configure(javaProjects()) {
      jar.doFirst {
          manifest {
              ...
          }
      }
  }
  
  configure(earProjects()) {
      apply plugin: 'ear'
  
      ear.doFirst {
          manifest {
              ...
          }
      }
  }
  
  Set&lt;String&gt; javaProjects() {
      subprojects - earProjects()
  }
  
  Set&lt;String&gt; earProjects() {
      subprojects.findAll { it.name.endsWith(".ear") }
  }
  </code></pre>
  
  <p>The only reason why I'm doing things this way, is because it was the first solution I tried that I could get to work in my situation. Now that the script is growing, though, it starts to feel a little clunky. Also, the <code>doFirst</code> thing seems a little awkward.</p>
  
  <p>But when I look on StackOverflow, I see recommendations of using constructs like this:</p>
  
  <pre><code>allprojects {
      tasks.withType(Jar) {
          manifest {
              ...
          }
      }
  
      tasks.withType(Ear) {
          manifest {
              ...
          }
      }
  }
  </code></pre>
  
  <p>This seems much nicer, but I don't seem to be able to rewrite my script in that way. I get errors like this one:</p>
  
  <pre><code>Cannot change configuration ':some.subproject:compile' after it has been resolved.
  </code></pre>
  
  <p>I don't know what to do about this error, and I can't seem to google it either, for some reason.</p>
  
  <p>So, my question is: have I indeed been doing things the wrong way? Or rather: in a way that is not idiomatic Gradle? For the sake of maintainability, I'd like to do things as idiomatically as possible. And if so: what can I do about the error message?</p>