首页 > 解决方案 > build.gradle 中使用 Gradle 属性

问题描述

我通读了这个,官方 Gradle参考和迁移指南,但我仍然无法弄清楚为什么以下构建脚本不起作用。

apply plugin: 'java-library'

repositories {
    jcenter()
}

dependencies {
    testImplementation group: 'org.testng', name: 'testng', version: '6.14.3'    
}

def t_threads = test_threads

test {
    useTestNG() {
        setParallel('methods');
        setThreadCount(t_threads)
    }
    maxParallelForks = t_threads
}

task printProps {
        println test_threads
}

我的gradle.properties文件:

test_threads = 1

gradle printProps失败,但setThreadCount(t_threads)出现异常:在 org.gradle.api.tasks.testing.testng.TestNGOptions类型的对象上找不到参数1的方法 setThreadCount()。如果我将代码更改为def t_threads = 1,则gradle printProps完成后不会显示错误1

标签: javagradle

解决方案


答案很简单。setThreadCount期望Integer作为参数,但属性是 a String,所以我所要做的就是转换StringIntegerusing test_threads as Integer


推荐阅读