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

时间:2015-06-06 13:21:10

标签: gradle

我有两个gradle文件,setup.gradle和tests.gradle;每个人都有两个自定义类型的gradle任务' EMTest&#39 ;;

test.gradle应用' setup.gradle'适用于:' 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 / Jun

这是主文件:

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任务仅作为依赖项添加到两个任务unitTeststenantMgmtUITests,因此small_deploy_3n_blockextractTestClasses生成设置之前执行失败,这反过来导致测试失败。

1 个答案:

答案 0 :(得分:1)

当您引用没有对象的任务时,它隐式使用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 }
   }
}