Eclipse - 使用 Spock 和 Groovy 的新 Gradle 项目

Eclipse - New Gradle project with Spock and Groovy

我正在 Eclipse 中创建一个新的Gradle项目

已创建具有以下结构的新项目:

因为我不需要文件夹 src/main/resourcessrc/test/resources 删除了 它们并且我还 refactor->rename 将文件夹 src/main/javasrc/test/java 放入 src/main/groovysrc/test/groovy。给我留下这样的结构:

然后我 convert 项目 to a Groovy project右键单击 项目:

现在我开始编辑我的 build.gradle 文件,它的开头如下所示:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

因为我想使用Groovy,所以我添加:

  1. apply plugin: 'groovy'在文件开头
  2. compile 'org.codehaus.groovy:groovy-all:2.4.3' 变成 dependencies{...}

然后我单击构建,它成功,但通知我一个警告:

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      build
[sts] -----------------------------------------------------
:compileJava UP-TO-DATE
:compileGroovywarning: [options] bootstrap class path not set in conjunction with -source 1.5
1 warning

:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestGroovywarning: [options] bootstrap class path not set in conjunction with -source 1.5
1 warning

:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 2.496 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 2 sec
[sts] -----------------------------------------------------

为了摆脱这个警告我再次编辑build.gradle
sourceCompatibility = 1.5更改为sourceCompatibility = 1.7
并且 连续构建 不再产生上述警告。

由于我想使用Spock-Testing,所以我打开build.gradle并在dependencies {...}下添加以下内容:

testCompile "org.spockframework:spock-core:1.0-groovy-2.4"

我再次执行 build 并再次 成功 完成。

现在我想开始编码,因此我添加了新文件

  1. Person2.groovysrc/main/groovy
  2. Person2Test.groovysrc/test/groovy

Person2Test.groovy 看起来像这样:

package org.gradle
import spock.lang.Specification 
class Person2Test {
}

Person2Test.groovy 显示以下错误:
Groovy:unable to resolve class spock.lang.Specification


我在这里很困惑。我将 Spock 添加到 build.gradle,我还需要更改什么才能使其正常工作?

其实问题很简单,也很微妙:

我必须右键单击该项目并为 Gradle 执行全部刷新: