简体   繁体   中英

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 "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
    }
}

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

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC4")
    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
    compile("com.h2database:h2:1.3.172")
    compile("joda-time:joda-time:2.3")
    compile("org.thymeleaf:thymeleaf-spring4")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
    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'
    }
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
    testCompile("junit:junit")
}

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/FileDownloadService.java:24:  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 eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM