How to use gradle. Java, and Groovy together?

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. So I have the following directory structure:

  • src\main\groovy
  • src\main\java
  • src\test\groovy

I have a mix of Java and Groovy classes

Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'

buildscript {
    repositories {
        maven { url "" }
    dependencies {

jar {
    baseName = 'my-app'
    version = '0.1.0'

repositories {
    maven { url "" }

dependencies {
    compile ('org.codehaus.groovy:groovy-all:2.2.1')

    testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'

jacocoTestReport {
  <!-- not sure this is right  -->
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'

And here is a build error I get when I run "gradle clean build":

...src/main/java/com/product/service/  cannot find symbol 
symbol  : class FileDownload 
location: class com.product.service.FileDownloadService

private FileDownload fileDownload;

If I make everything Java, then I don't get any compile or execution errors.


tries append to file "build.gradle" the next lines

sourceSets {
      main {
        java { srcDirs = [] }    // no source dirs for the java compiler
        groovy { srcDir "src" }  // compile everything in src/ with groovy

excuse me for my bad english. I hope that this can help your solution.

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.gradle:

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...

My solution would be to just have both Java and Groovy classes located in src/main/groovy and src/test/groovy (for test classes). From the compiler point of view it would result in something quite similar to the changes to the sourceSets suggested in the other answers.

From a user perspective I do not see a real benefit to keep the source files in separate folder hierarchies as it makes finding things harder. Also you are likely to migrate your classes between Java and Groovy to select the optimal implementation.

The main benefit is it works out of the box without any configuration in Gradle's build script. So it helps keeping things simple.

Need Your Help

UILabels not overlaying correctly in UITextView

iphone objective-c uilabel uitextview

I'm trying to add syntax highlighting to my program.I'm using this code to add highlights: