title: Gradle - Groovy and Java class dependency - Compile
body: <p>My project has both Java (N files) and Groovy code (1 file only). Java compile depends upon this single Groovy file's class file for Java compilation (compileJava task to succeed).</p>
  
  <p>When I don't use src/java as one of the srcDir within main>groovy> sourceSet section, then I get an error saying class/symbol not found which is in the groovy file/class. In ANT, it's easy that we are calling compile-groovy target first, before calling compile-java target but the same in Gradle is what I'm trying to find.</p>
  
  <p>I read some posts and found that if I make main>java section NULL and specify srcDir for main>java which is src/java inside main>groovy sourceSet section, then it compiles fine.</p>
  
  <p>My ?s:
  1. Is there any other way to do? for ex, the following should work:</p>
  
  <pre><code>   compileJava {
       dependsOn compileGroovy
     }
  </code></pre>
  
  <p>though, this goes to an infinte loop.</p>
  
  <p>OR </p>
  
  <p>what about using doFirst for compileJava task:</p>
  
  <pre><code>compileJava {
        doFirst {
              compileGroovy
        }
  }
  </code></pre>
  
  <p>this doesn't work either.</p>
  
  <p><br/><br/><br/><br/><br/></p>
  
  <p><strong>build.gradle</strong> This works, but compileJava in one sense becomes useless here even though the source code has N no. of java files in the src/java or src/java-test etc tree. I know this build script is working but logically it might bring some confusion to the developer if s/he is not familiar why sourceSet for Groovy MUST have "src/java" as its srcDir value.</p>
  
  <pre><code>apply plugin: 'java'
  apply plugin: 'groovy'
  
  sourceSets {
     main {
        groovy {
           srcDir 'src/groovy'
           srcDir 'src/java'
        }
        java {
         //The following needs to be commented out OR Gradle will always pick compileJava before compileGroovy
         //srcDir 'src/java'
         //srcDir 'src/java-test'
        }
     }
     test {
        groovy {
           srcDir 'test/groovy'
        }
        java {
           srcDir 'test/java'
        }
        resources {
           srcDir 'test/resources'
           srcDir 'conf'
        }
     }
     integrationTest {
        groovy {
           srcDir 'src/groovy-test'
        }
        java {
           srcDir 'src/java-test'
        }
        resources {
           srcDir 'test/resources'
           srcDir 'conf'
        }
     }
  }
  </code></pre>
  
  <p>Other links:
  <a href="https://stackoverflow.com/questions/19574078/how-to-make-gradle-compile-groovy-tests-before-java-tests">How to make Gradle compile Groovy tests before Java tests</a></p>