title: Add a 2nd source folder on a Gradle project which uses my Gradle plugin
body: <p>I created a Gradle plugin which generates some Java code we want.</p>
  
  <p>I tested my plugin on a test project and it worked perfectly, the files were correctly generated.</p>
  
  <p>Now I have to apply my plugin on the project I work on, for doing this I added the following lines to the <code>build.gradle</code> of the project :</p>
  
  <p><code>buildscript {
      repositories {
          [...]
          mavenLocal()    &lt;--
      }
      dependencies {
          [...]
          classpath 'my.plugin.path:app:1.0'  &lt;-- the maven local path of my plugin
      }
  }
  apply plugin: 'myPlugin'  &lt;--</code></p>
  
  <p>Then, the files that my plugin generates are placed into the <code>src</code> folder. In that case the project build correctly.</p>
  
  <p>BUT, I would like to place the generated files into the <code>target</code> folder.</p>
  
  <p>I tried two ways to resolve it :</p>
  
  <ol>
  <li>Add a sourceSets block into the <code>build.gradle</code> like :</li>
  </ol>
  
  <p><code>sourceSets {
      main {
          java {
              srcDirs = ['src/main/java', 'target/java']
          }
      }
  }</code></p>
  
  <p>And when I try to <code>gradle build</code>, the <code>:compileDebugJavaWithJavac</code> task fail, because some code in the <code>src/main/java</code> folder needs the code I generate. But the code in the target folder seems to not be compiled.</p>
  
  <ol start="2">
  <li>Add the source folder through the plugin code</li>
  </ol>
  
  <p>Here is my plugin class (in Groovy) :</p>
  
  <pre><code>import com.android.build.gradle.AppPlugin
  import org.gradle.api.Plugin
  import org.gradle.api.Project
  import org.gradle.api.Task
  
  class MyPlugin implements Plugin&lt;Project&gt; {
      @Override
      void apply(Project project) {
          def hasAppPlugin = project.plugins.hasPlugin AppPlugin
  
          project.afterEvaluate({
              def variants = hasAppPlugin ? project.android.applicationVariants : project.android.libraryVariants
              variants.all { variant -&gt;
                  def compileJavaTask = variant.javaCompile
  
                  [...] // Adds tasks to the plugins
  
                  compileJavaTask.source =  project.files("${project.rootDir}/target/java", output)
  
                  compileJavaTask.dependsOn javaTask
              }
          })
      }
  }
  </code></pre>
  
  <p>The 2 last lines should be enough, but I have the same problem than in case 1.</p>
  
  <p>I actually have no idea why this doesn't work, do someone have any idea ?</p>