如何使用gradle.Java和Groovy在一起吗?

son*_*rin 15 java groovy gradle spring-boot

我试图在IntelliJ 13中使用Gradle项目,但我一直遇到以下问题:

  • Java文件无法看到Groovy文件
  • IntelliJ似乎忘记了Groovy并提示我为它配置GDK

我读到groovy插件允许Groovy和Java在混合自己的源路径中,但Java想要它自己的.所以我有以下目录结构:

  • SRC \主\常规
  • SRC \主\ java中
  • SRC \测试\常规

我有Java和Groovy类的混合

这是我的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'
}
Run Code Online (Sandbox Code Playgroud)

这是我运行" 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;
Run Code Online (Sandbox Code Playgroud)

如果我创建Java,那么我不会得到任何编译或执行错误.

小智 18

尝试追加文件"build.gradle"的下一行

sourceSets {
      main {
        java { srcDirs = [] }    // no source dirs for the java compiler
        groovy { srcDir "src" }  // compile everything in src/ with groovy
       }
    }
Run Code Online (Sandbox Code Playgroud)

请原谅我的英语不好.我希望这可以帮助您解决问题.


ler*_*yse 15

如上所述,使用groovy插件进行编译也将编译java类.我们必须确保不会在源代码和groovy任务上触发java编译任务......

为此,并保留源文件夹(例如:在eclipse中),您可以在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
  }
}
Run Code Online (Sandbox Code Playgroud)

如果你只是指定groovy { srcDir "src" },你main/groovymain/java文件夹在eclipse中被识别为包...