Gradle:如何使用自定义配置运行自定义任务?

时间:2019-06-27 18:15:21

标签: gradle build.gradle

我正在尝试添加一个自定义的Gradle任务,该任务运行一组测试并将结果上载到报表服务。该服务使用的依赖项仅应在运行此任务时解决。

我的理解是,通常要创建一个包含相关依赖项的自定义配置,我已经完成了。 现在如何将该配置添加到任务中?

我尝试使用from关键字,但出现以下错误:

> Could not find method from() for arguments [configuration ':rp'] on task ':regression' of type org.gradle.api.tasks.testing.Test.

我还按照IIFEs by default的描述使用了configurations.rp.each(),但是我遇到了NullPointerException。这是该堆栈跟踪:

Caused by: java.lang.NullPointerException
        at build_63dcrq96an3ts1o13nw201x2q$_run_closure6.doCall(C:\Users\memyselfandi\source\repos\serenitybdd\build.gradle:100)
        at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:71)
        at org.gradle.util.ConfigureUtil.configureTarget(ConfigureUtil.java:155)
        at org.gradle.util.ConfigureUtil.configureSelf(ConfigureUtil.java:131)
        at org.gradle.api.internal.AbstractTask.configure(AbstractTask.java:657)
        at org.gradle.api.internal.project.DefaultProject.task(DefaultProject.java:1239)
        at org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:479)
        at org.gradle.internal.metaobject.BeanDynamicObject.tryInvokeMethod(BeanDynamicObject.java:191)
        at org.gradle.internal.metaobject.CompositeDynamicObject.tryInvokeMethod(CompositeDynamicObject.java:98)
        at org.gradle.internal.metaobject.MixInClosurePropertiesAsMethodsDynamicObject.tryInvokeMethod(MixInClosurePropertiesAsMethodsDynamicObject.java:30)
        at org.gradle.groovy.scripts.BasicScript$ScriptDynamicObject.tryInvokeMethod(BasicScript.java:133)
        at org.gradle.internal.metaobject.AbstractDynamicObject.invokeMethod(AbstractDynamicObject.java:160)
        at org.gradle.groovy.scripts.BasicScript.invokeMethod(BasicScript.java:82)
        at build_63dcrq96an3ts1o13nw201x2q.run(C:\Users\memyselfandi\source\repos\serenitybdd\build.gradle:99)
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:90)
        ... 102 more

我的 build.gradle 的相关部分:

configurations {
    rp {
        extendsFrom compile
        transitive=true
    }
}

dependencies {
    compile "org.slf4j:slf4j-simple:1.7.7"
    rp "com.github.invictum:serenity-reportportal-integration:1.3.0"
    compile group: 'com.github.javafaker', name: 'javafaker', version: '0.14'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.3'
}

tasks.withType(Test) {

    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
    }

    systemProperties System.getProperties()

    test.finalizedBy(aggregate)
}

task regression(type: Test) {
    def env = !System.properties.containsKey("environment") ? "DEV" : System.getProperty("environment").toUpperCase()
    def zone = TimeZone.getTimeZone("US/Mountain")
    def cal = new GregorianCalendar(zone)
    def now = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a z").format(cal.getTime())
    systemProperty "rp.description", env + " " + now
    systemProperty "rp.launch", "Regression Suite"
}

gradle.startParameter.continueOnFailure = true

我的系统信息:

$ ./gradlew -v

------------------------------------------------------------
Gradle 4.10
------------------------------------------------------------

Build time:   2018-08-27 18:35:06 UTC
Revision:     ee3751ed9f2034effc1f0072c2b2ee74b5dce67d

Kotlin DSL:   1.0-rc-3
Kotlin:       1.2.60
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Windows 10 10.0 amd64

1 个答案:

答案 0 :(得分:0)

我想得太多了。解决方案最终只是在任务中添加依赖项:

dependencies {
    compile "org.slf4j:slf4j-simple:1.7.7"
    compile group: 'com.github.javafaker', name: 'javafaker', version: '0.14'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.3'
}

tasks.withType(Test) {

    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
    }

    systemProperties System.getProperties()

    test.finalizedBy(aggregate)
}

task regression(type: Test) {

    dependencies {
        compile "com.github.invictum:serenity-reportportal-integration:1.3.0"
    }

    def env = !System.properties.containsKey("environment") ? "DEV" : System.getProperty("environment").toUpperCase()
    def zone = TimeZone.getTimeZone("US/Mountain")
    def cal = new GregorianCalendar(zone)
    def now = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a z").format(cal.getTime())
    systemProperty "rp.description", env + " " + now
    systemProperty "rp.launch", "Proposal Regression Suite"
}

gradle.startParameter.continueOnFailure = true