gpt4 book ai didi

gradle tasks.withType 找不到其他文件中定义的任务

转载 作者:行者123 更新时间:2023-12-03 03:40:36 25 4
gpt4 key购买 nike

我有两个 gradle 文件 setup.gradle 和 tests.gradle;每个都有两个自定义类型“EMTest”的 gradle 任务;

test.gradle 应用 'setup.gradle' 作为 apply from: 'setup.gradle'

我想配置 EMTest 类型的所有任务;为此,我在 tests.gradle 末尾添加了以下代码

tasks.withType(EMTest) {
println it.name
}

但这只会打印 tests.gradle 中的任务名称;

当我跑
tasks.all { 
println it.name + " " + it.class
}

但是,它列出了 setup.gradle 中定义的任务名称和类型为 EMTest_Decorated (适用于所有 4 种类型)

注意:我使用 gradle 1.11(无法控制升级);这里有什么问题?

更新于 09/6 月

这是主文件:
apply plugin: 'java';
apply plugin: 'maven'
apply from: 'emcpsrvs_3n_setup.gradle'

buildscript {
repositories {
maven {
url = "${artifactory_contextUrl}/repo" }
}
dependencies {

classpath group:"com.mycompany.myprod.mymodule", name: "TestInfraPlugin", version: "${testinfraVersion}", transitive: true
classpath group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version:'1.9.13'
classpath group: 'com.mycompany.myprod', name: 'common',version:'0.1'
classpath group: 'org.codehaus.jackson', name: 'jackson-core-asl', version:'1.9.13'
classpath group: 'com.oracle.weblogic',name: 'jettison-1.1', version: '12.1.2-0-0'
}
}

repositories {
/* To check if the jar is available in local maven repository */
mavenLocal()
maven {
url = "${artifactory_contextUrl}/repo"
}
}

apply plugin: 'TestInfraPlugin'
import com.mycompany.myprod.gradle.testinfra.tasks.EMTest;

repositories {
maven {
url = "${artifactory_contextUrl}/repo"
}
}
dependencies {
testConfig group:'com.mycompany.myprod',name:'ui-integ-tests', version: '1.+'
testConfig group: 'com.mycompany.myprod', name: 'emaas-platform-tenant-sdk', version: '0.1+'
}

task unitTests(type: EMTest){
}

// Three tests are disabled due to JIRA-900
task tenantMgmtUITests(type: EMTest,dependsOn: [cleanSmall_deploy_3n_block,small_deploy_3n_block]) {
useWebdriver = true
small_deploy_3n_block.mustRunAfter ([cleanSmall_deploy_3n_block])

options.suiteXmlBuilder().suite('parallel': 'none','name': 'TenantManagementUI') {
test('name': 'TenantManagementUI') {
classes([:]) {

'class'('name': 'com.mycompany.package.MyTest')
}
}
}
}

small_deploy_3n_cleanup.mustRunAfter ([tenantMgmtUITests])
task emcpsrvs_tenant_mgmt_ui_3n(dependsOn: [tenantMgmtUITests,small_deploy_3n_cleanup])

这是上面应用的“emcpsrvs_3n_setup.gradle”
buildscript {
repositories {
maven {
url = "${artifactory_contextUrl}/repo"
}
}
dependencies {
classpath group: 'com.mycompany.myprod.emdi', name: 'TestInfraPlugin', version: "${testinfraVersion}", transitive: true
}
}

apply plugin: 'TestInfraPlugin'
repositories {
maven {
url = "${artifactory_contextUrl}/repo"
}
}

import com.mycompany.myprod.gradle.testinfra.tasks.EMTest;

ext.integDeployVersion='1.1+'
dependencies {
testConfig group: 'com.mycompany.myprod.test', name: 'smalldeployment', version: "${integDeployVersion}"
}

/* Setup EMaaS Small Deployment */
task small_deploy_3n_block(type: EMTest) {
outputs.upToDateWhen { false }
onlyIf {!System.env.SMALLDEPLOY_IGNORESETUP}
options.suiteXmlBuilder().suite('name': 'setup_3n_env') {
test('name': 'emaas_setup_small_deploy') {
classes([:]) {
'class'('name': 'mycompany.sysman.test.emaas.integ.EmaasSmallDeploy3n') {
methods([:]) {
'include' ('name': 'setupEmaasSmallDeploy')
}
}
}
}
}
useWebdriver = true
useRestAssured = true
}

/* Cleanup EMaaS Small Deployment */
task small_deploy_3n_cleanup(type: EMTest) {
onlyIf {!System.env.SMALLDEPLOY_IGNORESETUP}
options.suiteXmlBuilder().suite('name': 'setup_3n_env') {
test('name': 'emaas_setup_small_deploy') {
classes([:]) {
'class'('name': 'mycompany.sysman.test.emaas.integ.EmaasSmallDeploy3n') {
methods([:]) {
'include' ('name': 'logCollectionAndPostCleanup')
}
}
}
}
}
mustRunAfter ([small_deploy_3n_block])
}

最后是来自 TestInfraPlugin.groovy 的片段(gradle 插件):
logger.debug "Configuring the EMTest task with default values."
project.afterEvaluate {
project.ext.testClassesDir = new File(project.properties['emdi.T_WORK'] + '/testClasses')

def testTasks = project.tasks.withType(EMTest)

if (testTasks != null && testTasks.size() == 0) {
logger.info "There are no tasks of type EMTest."
return
}

def extractTask = project.tasks.findByPath('extractTestClasses') ?:
project.task('extractTestClasses', type: ExtractConfiguration) {
configuration = project.configurations.testConfig
to = project.testClassesDir
}
/*
* 1. Adding the 'extractTask' to all EMTest, to ensure that 'extractTask' is run before any 'EMTest'.
* 2. For lazy evaluation of lrgConfig, we are NOT running the task here, but just adding as dependent task.
*/
testTasks.each { task ->
logger.debug "Adding dependsOn extractTask for task: ${task.name}"
task.dependsOn extractTask
}
} // end afterEvaluate

}

什么是 afterEvaluate{}阻止做:

它检查是否有任何类型的任务 EMTest如果有创建任务来提取配置(名为 testConfig )。此提取任务被添加为对 EMTest 类型的所有任务的依赖。这样提取任务在运行任何其他任务之前作为第一个任务运行。

怎么了
extractTestClasses任务被添加为仅对两个任务 unitTests 的依赖项和 tenantMgmtUITests因此 small_deploy_3n_blockextractTestClasses 之前执行导致设置失败,进而导致测试失败。

最佳答案

当您引用没有对象的任务时,它会隐式使用 project.tasks.

tasks.withType(EMTest) {
println it.name
}

这就是为什么您只会从当前项目中获得任务。

要包含您可以使用的所有项目:
allprojects {
tasks.withType(EMTest) { println it.name }
}

在此闭包中,您引用了 it.tasks , 其中 it循环遍历每个单独的项目。

这将 不是 按预期工作,因为此时 Gradle 可能不会加载您的所有子项目,并且没有达到在完整配置阶段找到的每个任务定义。因此,您必须定义在所有项目都经过全面评估后运行的闭包:
allprojects {
afterEvaluate {
tasks.withType(EMTest) { println it.name }
}
}

关于gradle tasks.withType 找不到其他文件中定义的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com