Java Compile with Groovy class

How can I compile a Java file that use a Class defined in Groovy file in a mixed Java and Groovy project?

Basically, you need to place both Java and Groovy files under the same source root. By default, this is ‘src/main/groovy’.

Thank you, Peter. I have solved my problem through your advice. Another question. If there is a way to compile Java files that use Groovy classes under the different source root? For example, compile Java file under src/main/java use groovy class under src/main/groovy.

I have compile them by maven(through a gmaven plugin), now I want to use gradle build my project. How can I do this?

sourceSets {
  main {
    java.srcDirs = []
    groovy.srcDir "src/main/java"
  test {
    java.srcDirs = []
    groovy.srcDir "src/test/java"

I am very appriciate of your answer. They are helpful. I just got another question, I want to read a resource file. when compile with maven, I just use the method “getResourceAsStream” with parameter “/” of the Class Object, then I would get the class root directory.

But when I use gradle test, the above code would get a directory of gradle cache at user home. Can tell me why?

Please create a new topic and provide

  • the exact code that you are using * the exact expected result * the exact actual result