为什么Gradle不了解该文件是Groovy?

时间:2019-07-15 11:37:13

标签: gradle groovy

  

我尝试在gradle指南中执行关于测试的示例https://guides.gradle.org/testing-gradle-plugins/

     

build build.gradle文件类似于

apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'
apply from: "$rootDir/gradle/integration-test.gradle"
apply from: "$rootDir/gradle/functional-test.gradle"

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
   mavenCentral()
   maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }

}

test {
    useJUnitPlatform()
}

dependencies {
    localGroovy()
    testCompile ('org.codehaus.groovy:groovy-all:2.5.7')
    testCompile('org.spockframework:spock-core:1.3-groovy-2.5') 
    testImplementation gradleTestKit()
    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.4.2'

}
  

我的functional-test.gradle文件看起来像这样   

ourceSets {
    functionalTest {
        groovy.srcDir file('src/functTest/groovy')
        resources.srcDir file('src/functTest/resources')
        compileClasspath += sourceSets.main.output + configurations.testRuntime
        runtimeClasspath += output + compileClasspath
    }
}

task functionalTest(type: Test) {
    description = 'Runs the functional tests.'
    group = 'verification'
    testClassesDirs = sourceSets.functionalTest.output.classesDirs
    classpath = sourceSets.functionalTest.runtimeClasspath
    mustRunAfter test, integrationTest
}

check.dependsOn functionalTest

// tag::source-set-config[]
gradlePlugin {
    testSourceSets sourceSets.functionalTest
}
  

插件的功能测试如下

package com.steinko.groovy.sample.http

import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.TempDir
import spock.lang.Specification
import java.io.File
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

class UrlVerifierPluginFunctionalTest extends Specification {

    @TempDir 
    private File testProjectDir;

    private File buildFile;

    def setup() {
        buildFile = testProjectDir.newFile('build.gradle')
        buildFile << """
            plugins {
                id 'org.gradle.sample.urlverifier'
            }
        """
    }

    def "can successfully configure URL through extension and verify it"() {
        buildFile << """
            verification {
                url = 'https://www.google.com/'
            }
        """

        when:
        def result = GradleRunner.create()
            .withProjectDir(testProjectDir.root)
            .withArguments('verifyUrl')
            .withPluginClasspath()
            .build()

        then:
        result.output.contains("Successfully resolved URL 'https://www.google.com/'")
        result.task(":verifyUrl").outcome == SUCCESS
    }
}
  

当我运行任务Gradle FunctionalTest时,出现以下错误:   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:1:错误:“;”预期   软件包com.steinko.groovy.sample.http                                         ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:3:错误:“;”预期   导入org.gradle.testkit.runner.GradleRunner                                                ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:4:错误:“;”预期   导入org.junit.jupiter.api.TempDir                                       ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:5:错误:“;”预期   导入spock.lang.Specification                                  ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:6:错误:“;”预期   导入java.io.File                      ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:7:错误:“;”预期   导入静态org.gradle.testkit.runner.TaskOutcome.SUCCESS                                                              ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:17:错误:未封闭的字符文字           buildFile = testProjectDir.newFile('build.gradle')                                              ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:17:错误:未封闭的字符文字           buildFile = testProjectDir.newFile('build.gradle')                                                           ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:17:错误:不是声明           buildFile = testProjectDir.newFile('build.gradle')                                                    ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:18:错误:未封闭的字符串文字           buildFile <<“”“                          ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:18:错误:不是声明           buildFile <<“”“                     ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:19:错误:未声明               插件{               ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:19:错误:“;”预期               插件{                      ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:20:错误:未封闭的字符文字                   id'org.gradle.sample.urlverifier'                      ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:20:错误:不是声明                   id'org.gradle.sample.urlverifier'                   ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:20:错误:字符文字中的非法行尾                   id'org.gradle.sample.urlverifier'                                                    ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:20:错误:不是声明                   id'org.gradle.sample.urlverifier'                                        ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:22:错误:未封闭的字符串文字           “”             ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:22:错误:不是声明           “”           ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:25:错误:预期       def“可以通过扩展名成功配置URL并进行验证”(){          ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:26:错误:未封闭的字符串文字           buildFile <<“”“                          ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:26:错误:不是声明           buildFile <<“”“                     ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:27:错误:不是声明               验证{               ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:27:错误:“;”预期               验证{                           ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:28:错误:未封闭的字符文字                   url ='https://www.google.com/'                         ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:29:错误:语句的非法开始               }               ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:30:错误:未封闭的字符串文字           “”             ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:30:错误:不是声明           “”           ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:35:错误:未封闭的字符文字               .withArguments('verifyUrl')                              ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:33:错误:此处不允许变量声明           def结果= GradleRunner.create()               ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:35:错误:未封闭的字符文字               .withArguments('verifyUrl')                                        ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:35:错误:不是声明               .withArguments('verifyUrl')                                ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:37:错误:“;”预期               。建立()                       ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:40:错误:“;”预期           result.output.contains(“成功解析的URL'https://www.google.com/'”)                                                                                        ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:41:错误:不是声明           result.task(“:verifyUrl”)。结果==成功                                             ^   /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:41:错误:“;”预期           result.task(“:verifyUrl”)。结果==成功                                                       ^   36错误   启动失败:   编译失败;有关详细信息,请参见编译器错误输出。

1个错误

  

任务:compileFunctionalTestGroovy失败

失败:构建失败,并出现异常。

  • 出了什么问题: 任务':compileFunctionalTestGroovy'的执行失败。

      

    编译失败;有关详细信息,请参见编译器错误输出。

  • 尝试: 使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。使用--scan运行以获取完整的见解。

  • https://help.gradle.org

  • 获得更多帮助

7秒内失败 3个可执行的任务:1个已执行,2个最新 Steins-MacBook-Air:如何测试steinkorsveien $ >

  

我看起来编译器将文件视为Java代码   我该如何解决,以便可以编译Groovy代码?

1 个答案:

答案 0 :(得分:0)

我将文件* .java重命名为* .groovy,然后可以正常工作