【问题标题】:Why dont the Gradle understand that file is Groovy?为什么 Gradle 不理解该文件是 Groovy?
【发布时间】:2019-11-24 02:33:13
【问题描述】:

我尝试在 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 文件看起来像这样 s

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:错误:未闭合的字符串文字 构建文件 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").outcome == SUCCESS ^ /Users/steinkorsveien/Development/TestWorkSpace/HowToTest/src/functTest/groovy/com/steinko/groovy/sample/http/UrlVerifierPluginFunctionalTest.java:41:错误:';'预期的 result.task(":verifyUrl").outcome == SUCCESS ^ 36 个错误 启动失败: 编译失败;有关详细信息,请参阅编译器错误输出。

1 个错误

任务:compileFunctionalTestGroovy 失败

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

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

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

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

  • 通过https://help.gradle.org获得更多帮助

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

我看起来编译器将文件视为 Java 代码 我该如何解决这样我才能编译 groovy 代码?

【问题讨论】:

  • 你必须将你的文件*.java重命名为*.groovy,然后编译器就会知道这是一个groovy文件

标签: gradle groovy


【解决方案1】:

我将文件 *.java 重命名为 *.groovy 然后就可以了

【讨论】:

    猜你喜欢
    • 2022-06-21
    • 1970-01-01
    • 2013-11-28
    • 2017-11-29
    • 1970-01-01
    • 2017-12-05
    • 2016-08-10
    • 2016-10-17
    • 2014-01-22
    相关资源
    最近更新 更多