简体   繁体   中英

Generate multiple report types using CodeNarc in Gradle

I want to generate both HTML and console report in CodeNarc in Gradle.

My build.gradle :

apply plugin: 'codenarc'
...
codenarc {
    toolVersion = '0.24.1'
    configFile = file('config/codenarc/codenarc.groovy')
    reportFormat = 'html'
}

This works fine, but I'd like also to have report displayed on console, as right now only link to HTML is displayed there. How can I request multiple report types?

Instead of running a second task to generate another report, you could make the following change to add another report format. Then grab one of the files and write it to the console. (You could just grab the HTML or XML report and write that to the console, but it may be hard to read without some formatting.)

Note: The reports closure will get you reports in different formats. The doLast will print the output of one of those reports to the console. If you do not need the console output, you can remove the doLast closure.

I would suggest changing your task like this:

task codenarcConsoleReport {
    doLast {
        println file("${codenarc.reportsDir}/main.txt").text
    }
}
codenarcMain {
    finalizedBy codenarcConsoleReport
    reports {
        text.enabled = true
        html.enabled = true
        xml {
            enabled =  true
            destination = file("${codenarc.reportsDir}/customFileName.xml")
        }
    }
}

Note: This will not cause your task to run twice.

In newer versions of Gradle (7+) I get a deprecation warning on:

reports {
    html.enabled = true
    xml.enabled = true
}

The format has changed to (for enabling html and xml reports)

reports {
    html.required = true
    xml.required = true
}

Best way I can think of is to create a separate task:

task codeNarcConsole(type: CodeNarc) {
  // other config
  reportFormat = 'console'
}

check.dependsOn('codeNarcConsole')

Not ideal, but workable. You could also post to Gradle Bugs about this to get it improved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM