如何配置Java和Groovy混合式开发

Question

I am trying to use a Gradle project in IntelliJ 13 but I keep running into issues such as:

  • Java files can't see Groovy files

  • IntelliJ seems to forget about Groovy and prompts me to configure a GDK for it

I read that the groovy plugin allows Groovy and Java in mixed own source path, but Java wants its own.

I have a mix of Java and Groovy classes in src/main/java directory.

Answer

Like mentioned above, compiling with the groovy plugin will also compile the java classes. We just have to make sure that the java compile task is not fired on the source as well as the groovy task...

To to this, and retain the source folders (for e.g.: in eclipse), you can use the following refined snippet in the build.graddle:

apply plugin: 'groovy'
//...
sourceSets {
  main {
    java { srcDirs = [] }    // no source dirs for the java compiler
    groovy { srcDirs = ["src/main/java", "src/main/groovy"] }  // compile   everything in src/ with groovy
  }
}

If you just specify the groovy { srcDir "src" }, your main/groovy and main/java folders are identified as packages in eclipse...

results matching ""

    No results matching ""