Gradle

Could not get unknown property &

2019-08-19  本文已影响1人  手心de温暖

在gradle 与jacoco集成以后build时会报以上错误,原因是SourceSetOutput.getClassesDir() 是 Gradle 4中的方法,在Gradle 5中被标记过时,替代方法是 getClassesDirs() ,此时返回值是FileCollection。要想获得File,需调用getSingleFile()。

解决方式如下:

将  output.classesDir 替换成  output.classesDirs.singleFile

修改后的集成脚本如下:

//Additional SourceSets can be added to the jacocoOfflineSourceSets as needed by

project.ext.jacocoOfflineSourceSets = [ 'main' ]

task doJacocoOfflineInstrumentation(dependsOn: [ classes, project.configurations.jacocoAnt ]) {

    inputs.files classes.outputs.files

    File outputDir = new File(project.buildDir, 'instrumentedClasses')

    outputs.dir outputDir

    doFirst {

        project.delete(outputDir)

        ant.taskdef(

                resource: 'org/jacoco/ant/antlib.xml',

                classpath: project.configurations.jacocoAnt.asPath,

                uri: 'jacoco'

        )

        def instrumented = false

        jacocoOfflineSourceSets.each { sourceSetName ->

            if (file(sourceSets[sourceSetName].output.classesDirs.singleFile).exists()) {

                def instrumentedClassedDir = "${outputDir}/${sourceSetName}"

                ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {

                    fileset(dir: sourceSets[sourceSetName].output.classesDirs.singleFile, includes: '**/*.class')

                }

                //Replace the classes dir in the test classpath with the instrumented one

                sourceSets.test.runtimeClasspath -= files(sourceSets[sourceSetName].output.classesDirs.singleFile)

                sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)

                instrumented = true

            }

        }

        if (instrumented) {

            //Disable class verification based on https://github.com/jayway/powermock/issues/375

            test.jvmArgs += '-noverify'

        }

    }

}

test.dependsOn doJacocoOfflineInstrumentation

jacocoTestReport {

    reports {

        xml.enabled true

        csv.enabled true

        html.enabled true

    }

}

check.dependsOn jacocoTestReport

Gradle 5.6 源码链接: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html?_ga=2.59713389.1546270617.1566195252-732042021.1552302913#getClassesDirs--

上一篇下一篇

猜你喜欢

热点阅读