1

execカスタムgradleプラグインから、jacocoファイルの場所をプロジェクトディレクトリの外に変更するにはどうすればよいですか。プラグインに以下のコードがあります:

project.afterEvaluate {
    def testTasks = project.tasks.withType(Test)
    testTasks.each { task ->
        task.jacoco.destinationFile = file("/home/skgupta/jacoco/${task.name}.exec")
    }
}

しかし、テストを実行すると、.execファイルは次の場所に生成されます<projectDir>/jacoco/<taskName>.exec

destinationFileディレクトリの値を変更すると、build.gradle完全に正常に動作します。

ここで何が欠けていますか?

編集:プラグインの完全なコードはこちら

class TestInfraPlugin implements Plugin<Project> {

    Project project

    void apply(Project target) {
        this.project = target
        logger = target.logger
        configureConfigurations(target)
        configureTestNG()
        configureCodeCoverage(target)
    }

    public void configureTestNG() {
        logger.debug "Configuring the EMTest task with default values."
        project.afterEvaluate {
            project.ext.testClassesDir = new File(project.properties['emdi.T_WORK'] + '/testClasses')
            /* Create one task of type ExtractConfiguration, to extract the lrgConfig configuration to $T_WORK/testClasses
             * The extraction of the test jar before the tests are run is mandatory because the 'Test' task 
             * ONLY works on the files in directory and NOT on the jars. Thus we extract the jar and point the testClasses 
             * property to the location where the jar is extracted.
             *  
             * NOTE: 
             *  1. This is only one task per gradle. 
             *  2. All the jars in the lrgConfig are extracted before any testblock is run.
             *  3. The LRG will not run if user uses other configuration than 'lrgConfig'
             *     (which applies without cutover to 'Test' task also.)
             */
            def testTasks = project.tasks.withType(EMTest)
            /* Silently log and return if there are no EMTest tasks. 
             * This case would be hit :
             *     a) When testinfraPlugin is applied to build.gradle
             *     b) (in Future)if <lrg>.gradle has test-blocks of non-Java types.
             */
            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
                /* TODO: The code below is to change the location of the jacoco file, but it is not working
                 * When the same is tried directly from build.gradle (tried as fresh project), it works.
                 * I have opened SO question as: http://stackoverflow.com/questions/28039011/create-jacoco-exec-file-out-of-gradle-project-dir
                 * If we have solution for this, we can get-away with the workaround we applied in the TestResultsHandler.groovy
                 *
                 * NOTE: I tried with beforeEvaluate, and it really does not work because EMTest tasks are not evaluated by then.
                 * I also tried with 'java.io.File', etc other options. Nothing works.
                 *
                 */
                task.jacoco.destinationFile = task.project.file( '/scratch/skgupta/git_storage/emdi/work/jacocobuild/' + task.name + '.exec')
                println "Value of destinationFile in jacoco extension is "+ task.jacoco.getDestinationFile()
            }
        }

       logger.debug "Applying plugins implicitly."
        applyPlugin('java')
    /* Apply jacoco plugin */
        applyPlugin('jacoco')

    }

    /**
     * Apply plugin with given name.
     * 
     * @param pluginName - The name of the plugin to apply.
     * 
     */
    protected void applyPlugin(String pluginName) {
        logger.debug "Checking if the plugin, ${pluginName} is already applied."
        if (project.plugins.hasPlugin(pluginName)) {
            logger.debug "Plugin is already applied."
            return
        }
        logger.debug "Applying plugin, ${pluginName}"
        project.apply plugin: pluginName

        logger.debug "Verifying if the plugin is applied."
        if (project.plugins.hasPlugin(pluginName)) {
            logger.debug "Successfully applied plugin, ${pluginName}"
        }
        else {
            logger.error "Failed to apply plugin, ${pluginName}"
        }
    }

    /**
     * Add the configuration, lrgConfig, to the current project.
     *
     * The configuration lrgConfig defines the dependencies for the tools like testng, restassured, etc.
     * @param project - the project to which the config will be added.
     */
    protected void configureConfigurations(Project project) {
        logger.debug "Creating the configurations to hold tools GAV"
        project.configurations {
            /* This will be empty configuration, and will be populated by lrg owner with gav of the lrg jar */
            testConfig

            /* This will be empty configuration, and will be populated by lrg owner during test jar build */
            lrgConfig

            /* Configuration to hold the gav of the testng jar(s) */
            testNG

            /* Configuration to hold the GAV of the webdriver jar(s) */
            webdriver

            /* Configuration to hold the GAV of the restAssured jar(s) */
            restAssured
        }
        project.dependencies {
            /* 
             * Currently adding opensource testng jar path here. This will be replaced by
             * GAV of the tools testng only jar once they decouple it.
             * Bug 19276096 - SPLIT OUT TESTNG JAR FROM EM-QATOOL-UIFWK-WEBDRIVER.JAR| 
             */
            testNG group: 'com.mycomp.myprod.emdi', name: 'em-qatool-uifwk-webdriver',version: '1.0.0', transitive: false

            /*
             * webdriver config would contain reference to the tools webdriver jar, which currently
             * also has testng fwk in it. This will be replaced by gav of the webdriver only jar once
             * it is decoupled
             * Bug 19276096 - SPLIT OUT TESTNG JAR FROM EM-QATOOL-UIFWK-WEBDRIVER.JAR| 
             */
            webdriver group: 'com.mycomp.myprod.emdi', name: 'em-qatool-uifwk-webdriver',version: '1.0.0', transitive: false

            /* Add rest-assured libraries */
            restAssured group: 'org.codehaus.groovy', name:'groovy-all', version:'2.2.1'
            restAssured group: 'com.jayway.restassured', name:'json-path', version:'1.8.1'
            restAssured group: 'org.apache.commons', name:'commons-lang3', version:'3.1'
            restAssured group: 'org.hamcrest', name:'hamcrest-core', version: '1.3'
            restAssured group: 'org.apache.httpcomponents', name:'httpclient', version:'4.3.1'
            restAssured group: 'org.apache.httpcomponents', name:'httpcore', version:'4.3'
            restAssured group: 'org.apache.httpcomponents', name:'httpmime', version:'4.3.1'
            restAssured group: 'com.jayway.restassured', name:'xml-path', version:'1.9.0'
            restAssured group: 'com.jayway.restassured', name:'rest-assured-common', version:'2.3.1'
            restAssured group: 'com.jayway.restassured', name:'rest-assured', version:'2.3.1'
        }
        /* 
         * Currently the QA's build.gradle use lrgConfig for compilation. 
         * Hence setting lrgConfig as well with the testng gav for backward compatibility
         * TODO: This must be removed in future versions of testinfra 
         * 
         */
        project.configurations.lrgConfig.extendsFrom(project.configurations.testNG, project.configurations.restAssured)
    }
}
4

1 に答える 1