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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22369033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:19:10  来源:igfitidea点击:

How to use gradle. Java, and Groovy together?

javagroovygradlespring-boot

提问by sonoerin

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

我正在尝试在 IntelliJ 13 中使用 Gradle 项目,但我一直遇到问题,例如:

  • Java files can't see Groovy files
  • IntelliJ seems to forget about Groovy and prompts me to configure a GDK for it
  • Java文件看不到Groovy文件
  • IntelliJ 似乎忘记了 Groovy 并提示我为其配置 GDK

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:

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

  • src\main\groovy
  • src\main\java
  • src\test\groovy
  • src\main\groovy
  • 源\主\java
  • 源\测试\常规

I have a mix of Java and Groovy classes

我混合了 Java 和 Groovy 类

Here is my build.gradle:

这是我的 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":

这是我运行“ 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.

如果我将所有内容都制作成 Java,那么我不会收到任何编译或执行错误。

回答by arturoeanton

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

尝试将下一行附加到文件“build.gradle”

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.

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

回答by leroyse

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

如上所述,使用 groovy 插件编译也会编译 java 类。我们只需要确保 java 编译任务不会在源代码和 groovy 任务上触发......

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

为此,并保留源文件夹(例如:在 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
  }
}

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

如果您只指定groovy { srcDir "src" },则您的main/groovymain/java文件夹在 eclipse 中被标识为包...

回答by Tobias Kremer

My solution would be to just have both Java and Groovy classes located in src/main/groovyand 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.

我的解决方案是让 Java 和 Groovy 类都位于src/main/groovysrc/test/groovy(用于测试类)。从编译器的角度来看,它会导致与其他答案中建议的 sourceSets 更改非常相似的结果。

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.

从用户的角度来看,我认为将源文件保存在单独的文件夹层次结构中没有真正的好处,因为它会使查找变得更加困难。此外,您可能会在 Java 和 Groovy 之间迁移您的类以选择最佳实现。

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

主要好处是它开箱即用,无需在 Gradle 的构建脚本中进行任何配置。所以它有助于保持简单。