简体   繁体   English

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

[英]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: 我试图在IntelliJ 13中使用Gradle项目,但我一直遇到以下问题:

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

I read that the groovy plugin allows Groovy and Java in mixed own source path, but Java wants its own. 我读到groovy插件允许Groovy和Java在混合自己的源路径中,但Java想要它自己的。 So I have the following directory structure: 所以我有以下目录结构:

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

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,那么我不会得到任何编译或执行错误。

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. 我希望这可以帮助您解决问题。

Like mentioned above, compiling with the groovy plugin will also compile the java classes. 如上所述,使用groovy插件进行编译也将编译java类。 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编译任务......

To to this, and retain the source folders (for eg: 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/groovy and main/java folders are identified as packages in eclipse... 如果你只是指定groovy { srcDir "src" } ,你的main/groovymain/java文件夹在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). 我的解决方案是将Java和Groovy类放在src / main / groovysrc / test / groovy (用于测试类)中。 From the compiler point of view it would result in something quite similar to the changes to the sourceSets suggested in the other answers. 从编译器的角度来看,它会产生与其他答案中建议的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. 主要的好处是它在Gradle的构建脚本中没有任何配置,开箱即用。 So it helps keeping things simple. 所以它有助于保持简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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