如何使用gradle.Java 和 Groovy 在一起吗? [英] How to use gradle. Java, and Groovy together?
问题描述
我正在尝试在 IntelliJ 13 中使用 Gradle 项目,但我一直遇到以下问题:
I am trying to use a Gradle project in IntelliJ 13 but I keep running into issues such as:
- Java 文件看不到 Groovy 文件
- IntelliJ 似乎忘记了 Groovy 并提示我为其配置 GDK
我读到 groovy 插件允许 Groovy 和 Java 混合在自己的源路径中,但 Java 想要自己的.所以我有以下目录结构:
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:
- srcmaingroovy
- srcmainjava
- src estgroovy
我混合了 Java 和 Groovy 类
I have a mix of Java and Groovy classes
这是我的 build.gradle:
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'
}
这是我运行gradle clean build"时遇到的构建错误:
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;
如果我将所有东西都制作成 Java,那么我就不会遇到任何编译或执行错误.
If I make everything Java, then I don't get any compile or execution errors.
推荐答案
如上所述,使用 groovy 插件编译也会编译 java 类.我们只需要确保 java 编译任务不会在源代码和 groovy 任务上触发...
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...
为此,并保留源文件夹(例如:在eclipse中),您可以在build.gradle中使用以下精炼片段:
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
}
}
如果你只指定groovy { srcDir "src" }
,你的main/groovy
和main/java
文件夹被识别为包在日食中...
If you just specify the groovy { srcDir "src" }
, your main/groovy
and main/java
folders are identified as packages in eclipse...
这篇关于如何使用gradle.Java 和 Groovy 在一起吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!