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

标签 eclipse groovy gradle build.gradle spock

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

创建具有以下结构的新项目:
enter image description here

因为我不需要文件夹src/main/resourcessrc/test/resources,所以我删除它们,我也 refactor->将src/main/javasrc/test/java文件夹重命名为src/main/groovysrc/test/groovy。留给我这样的结构:
enter image description here

然后,我将项目转换为 Groovy项目,并通过右键单击该项目来执行以下操作:
enter image description here

现在,我开始编辑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 ,因此我添加:

文件开头的
  • apply plugin: 'groovy'
  • 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.gradlesourceCompatibility = 1.5更改为sourceCompatibility = 1.7,并且后续构建不再产生上述警告。

    由于我想使用 Spock -Testing,因此我打开build.gradle并在dependencies {...}下添加以下内容:
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
    

    再次,我执行构建,并再次成功完成

    现在我要开始编码,因此我添加了新文件:
  • Person2.groovysrc/main/groovy
  • 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
    我在这里很困惑。我在build.gradle中添加了 Spock ,还需要更改什么才能使其正常工作?

    最佳答案

    实际上,问题很简单,但也很细微:

    我必须右键单击该项目,然后对Gradle进行全部刷新:

    enter image description here

    关于eclipse - Eclipse-使用Spock和Groovy的新Gradle项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32817830/

    相关文章:

    android - 使用 shrinkResources 时 AppCompat V7 崩溃

    java - Eclipse Neon Tomcat插件下载报错

    java - 在 POM 文件中使用 Groovy 进行分隔符切换对于不同的开发人员来说工作方式不同

    sql - 在内存中运行时查看 grails 模式?

    jenkins - 在 Jenkins 的多个节点上运行同一阶段

    java - 为什么我的Gradle Java项目没有在Intellij中构建?

    java - 更新的信息没有反射(reflect)在mysql数据库中

    eclipse 工具提示中的 Java 注释?

    java - 使用 Eclipse 执行 JUnit 时出现内部错误

    c++ - Eclipse CDT 无法识别 <iostream>