Gradle测试任务testReport任务-Gradle 2.3/Java7或Java8的不推荐属性

Gradle测试任务testReport任务-Gradle 2.3/Java7或Java8的不推荐属性,gradle,java-8,deprecated,init.d,gradle-2,Gradle,Java 8,Deprecated,Init.d,Gradle 2,我正在尝试将我的项目从java7更改为java8。 因此,我对用于gradle 1.6的现有gradle extra.commons-myp.gradle脚本进行了更改 我将complie更改为JavaCompile,因为它在2.0之后被弃用。 我在测试任务中出错 testReportDir = file("$buildDir/reports/tests/UT") testResultsDir = file("$buildDir/test-results/UT") 嗯 在Gradle 1

我正在尝试将我的项目从java7更改为java8。 因此,我对用于gradle 1.6的现有gradle extra.commons-myp.gradle脚本进行了更改

我将complie更改为JavaCompile,因为它在2.0之后被弃用。 我在测试任务中出错

testReportDir = file("$buildDir/reports/tests/UT") testResultsDir = file("$buildDir/test-results/UT") 嗯

在Gradle 1.6或1.10之前(我猜),以下属性可用于测试任务

testReportDir = file("$buildDir/reports/tests/UT")
testResultsDir = file("$buildDir/test-results/UT")
如您所见,第一个创建了一个自定义报告文件夹(HTML index.HTML文件将放置在其中,即,我们不使用默认的build/reports/tests文件夹,而是将index.HTML/和其他文件放在build/reports/tests/UT文件夹下)第二个属性创建自定义测试结果文件夹(即,不再使用build/test results文件夹来放置单元测试结果*.xml文件/文件夹,而是将所有数据立即放置在build/test results/UT文件夹中)

这个想法是:在Gradle 1.6中,Gradle正在创建测试结果(.xml etc)/报告文件(.html etc),当您刚刚运行:Gradle clean build(作为测试任务,免费运行,即build调用test task来运行单元测试,而用户不显式调用测试任务)

现在,当您在Gradle 1.6<1.9/10中使用Java7时,一切都很好,但当您开始使用Java8时,您可能会看到Gradle 1.6与Java8不兼容的问题(由于ASM库和其他使用Java8的编译时问题,如果有的话),因此您从使用Gradle 1.6跳到了Gradle 2.3版本

PS:Gradle2.4是最新版本,但需要在额外的全局(init.d级别)Gradle文件中进行额外的调整。我会说现在就坚持Gradle 2.3

现在到您的?s-如何获取自定义测试结果和报告文件夹(创建了build/test results/UT和build/reports/tests/UT文件夹)

此时,您正在将Java8与Gradle 2.3(或Java7)一起使用。重要的是现在你有了Gradle 2.3(而不是1.6)

那么,改变什么以及如何找到改变什么

  • 参见Gradle文档中的2.3: (在这里,您必须首先看到并理解,在测试任务中从Gradle 1.6到2.3到底发生了什么变化,以及现在将如何生成报告)

  • 文档没有在其站点上定义所有字段(Gradle可以拥有/使用的字段)。不知道为什么,但幸运的是,我们可以找到您在test或testReport任务中定义的所有变量(这是一个新任务,在Gradle 1.6中不可用)

    For ex:仅显示可在测试任务中设置的主要属性或变量

    幸运的是,现在在测试任务(Gradle 2.3)中没有可用的testReportDirtestResultsDir属性

    看看所有格拉德尔都能看到什么。查看或运行:渐变属性

    上面的命令将列出所有变量/属性以及给定Gradle x.y版本可以看到的值

  • 新任务testReport或Gradle 2.3中的新任务有一个概念,即当Gradle在构建期间运行构建/测试并生成.xml或.html等文件时,您可以在显式调用testReport任务后,在最后使用报告(html部分)集体生成html报告。当您有一个多模块项目设置,并且每个子项目/模块中都有测试时,这基本上是有帮助的。如果您运行gradle clean build testReport,它将在默认位置生成.xml和.html报告(直到您在testReport任务而不是test任务中指定destinationDir、testResultDirs和reportsOn属性变量)

  • 现在,有人会说,如果我在多模块项目设置的命令行中调用gradle clean build testReport,您可以在给定位置为每个子项目/模块生成.xmls和.htmls。是的,没错。如果我们不想,那么我们必须在测试任务(而不是testReport任务)中使用以下属性来禁用为每个子项目创建html报告,很明显,我们将在最后调用testReport任务(或使用test.finalizedBy testReport)为所有测试生成报告(如Gradle 2.3文档中所述)

     test {
     ...
     .....
     reports.html.enabled = false
     ...
     .
     }
    
    参见本例:参见章节23.13.7和示例:23.14

    subprojects {
        apply plugin: 'java'
    
        // Disable the test report for the individual test task
        test {
            reports.html.enabled = false
        }
    }
    
    task testReport(type: TestReport) {
        destinationDir = file("$buildDir/reports/allTests")
        // Include the results from the `test` task in all subprojects
        reportOn subprojects*.test
    }
    
    上面的示例显示,它将在读取多模块项目结构中所有子项目的测试结果(.xml/etc info)后创建测试报告(HTML),并在build/tests/allTests文件夹中创建结果报告

    现在,我们没有多模块结构,因此必须执行以下操作:

    1.在extra1…init.d级别的gradle文件中添加一个新任务testReport

    task testReport(type: TestReport) {
        destinationDir = file("$buildDir/reports/tests/UT")
        testResultDirs = fileTree("$buildDir/test-results/UT")
        reportOn test
    }
    
    2.更改测试任务,即

    test {
         maxParallelForks = 5
         forkEvery = 50
         ignoreFailures = true
         //The following two properties DONT work in Gradle 2.3
         //testReportDir = file("$buildDir/reports/tests/UT")
         //testResultsDir = file("$buildDir/test-results/UT")
    
         //With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
         reports.html.enabled = false
    
         //OK - it took some time, but finally I can change the test-results
         //folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
         //As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable, 
         //the only way we can do it by adding the following line.
         testResultsDirName = "test-results/UT"
    
         //The following commented out lines are optional. Un-comment if you need to.
         //testLogging.showStandardStreams = true
    
         //onOutput { descriptor, event ->
         //    logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
         //}
    
         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {
            //Following two properties/variables works ONLY with 1.6 of Gradle
            //destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
    
    
            //Following two properties/variable works only with versions >= 1.7 version of Gradle
            destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //  classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
         }
    
         //Usually one should call testReport at command line while calling gradle clean build 
         // i.e. gradle clean build testReport
         // But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
         // PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
         finalizedBy testReport
       }
    
    如果您注意到,从一个任务到另一个任务,有一些变量从Gradle 1.6更改为Gradle 2.3,这些变量/属性的名称也有一些更改(例如:testResultsDir到testResultDirs)测试任务中不再有testReportDir属性,但现在testReport任务中有destinationDir

    另外,我在测试任务完成后调用testReport任务(作为test.finalizedBy testReport),以自动调用testReport任务(而不是要求用户显式调用它)

    另一种解决方法,如果您不需要进行上述更改(以获得您想要实现的目标),也不想在最后运行testReport任务来运行报告,那么您还可以执行以下操作。请仔细观察这次未注释/注释的内容

    //task testReport(type: TestReport) {
    //    destinationDir = file("$buildDir/reports/tests/UT")
    //    testResultDirs = fileTree("$buildDir/test-results/UT")
    //    reportOn test
    //}
    
    test {
         maxParallelForks = 5
         forkEvery = 50
         ignoreFailures = true
         //The following two properties DONT work in Gradle 2.3
         //testReportDir = file("$buildDir/reports/tests/UT")
         //testResultsDir = file("$buildDir/test-results/UT")
    
         //With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
         //This time you need to comment the following line, so that it'll actually create the reports/html file during test run.
         //reports.html.enabled = false
    
         doFirst {   
         //OK - it took some time, but finally I can change the test-results
         //folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
         //As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable, 
         //the only way we can do it by adding the following line.
         testResultsDirName = "test-results/UT"
    
         //Add the following if reports.html.enable = false line is commented out OR it's not commented but value is set to "true".
         //The following line will change the default build/reports/tests folder to build/reports/tests/UT for creating html reports for Unit tests.
         testReportDirName = "tests/UT"
         }
    
         //The following commented out lines are optional. Un-comment if you need to.
         //testLogging.showStandardStreams = true
    
         //onOutput { descriptor, event ->
         //    logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
         //}
    
         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {
            //Following two properties/variables works ONLY with 1.6 of Gradle
            //destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
    
    
            //Following two properties/variable works only with versions >= 1.7 version of Gradle
            destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //  classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
         }
    
         //Usually one should call testReport at command line while calling gradle clean build 
         // i.e. gradle clean build testReport
         // But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
         // PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
    
        //Now we don't need to call "gradle clean build testReport" anymore. 
        //finalizedBy testReport
       }
    
    正如您在上述代码中注意到的: 1.现在,我的全局init.d级别的gradle文件甚至没有testReport任务。 2.我已经注释掉了这一行:

    //reports.html.enabled = false
    
    3.我添加了另一个属性c
    //task testReport(type: TestReport) {
    //    destinationDir = file("$buildDir/reports/tests/UT")
    //    testResultDirs = fileTree("$buildDir/test-results/UT")
    //    reportOn test
    //}
    
    test {
         maxParallelForks = 5
         forkEvery = 50
         ignoreFailures = true
         //The following two properties DONT work in Gradle 2.3
         //testReportDir = file("$buildDir/reports/tests/UT")
         //testResultsDir = file("$buildDir/test-results/UT")
    
         //With Gradle 2.3 we now need the following line to disable HTML report creation during test run per project/sub-project as we'll take care of generating those reports by testReport task which is available in Gradle 2.3.
         //This time you need to comment the following line, so that it'll actually create the reports/html file during test run.
         //reports.html.enabled = false
    
         doFirst {   
         //OK - it took some time, but finally I can change the test-results
         //folder which Gradle generates and uses it to put the .xml/tests results files and etc folders.
         //As there is NO way to set a property in Gradle 2.3 to change the result directory property/variable, 
         //the only way we can do it by adding the following line.
         testResultsDirName = "test-results/UT"
    
         //Add the following if reports.html.enable = false line is commented out OR it's not commented but value is set to "true".
         //The following line will change the default build/reports/tests folder to build/reports/tests/UT for creating html reports for Unit tests.
         testReportDirName = "tests/UT"
         }
    
         //The following commented out lines are optional. Un-comment if you need to.
         //testLogging.showStandardStreams = true
    
         //onOutput { descriptor, event ->
         //    logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
         //}
    
         //Following Jacoco test section is required only in Jenkins instance extra common file
         jacoco {
            //Following two properties/variables works ONLY with 1.6 of Gradle
            //destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
    
    
            //Following two properties/variable works only with versions >= 1.7 version of Gradle
            destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
            //  classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
         }
    
         //Usually one should call testReport at command line while calling gradle clean build 
         // i.e. gradle clean build testReport
         // But using Gradle 2.3 finalizedBy feature, you can call testReport as soon as test task is run by Gradle during gradle clean build.
         // PS: This will bring confusion if you have a multi-module project structure as there, you should call testReport task explicitly at command line. So, comment or uncomment acc. to your use.
    
        //Now we don't need to call "gradle clean build testReport" anymore. 
        //finalizedBy testReport
       }
    
    //reports.html.enabled = false
    
    testReportDirName = "tests/UTnew"