diff --git a/apache-rat-gradle/src/gradle/src/main/groovy/org/apache/rat/gradle/RatTask.groovy b/apache-rat-gradle/src/gradle/src/main/groovy/org/apache/rat/gradle/RatTask.groovy index cf35f3eeb..96105b8d9 100644 --- a/apache-rat-gradle/src/gradle/src/main/groovy/org/apache/rat/gradle/RatTask.groovy +++ b/apache-rat-gradle/src/gradle/src/main/groovy/org/apache/rat/gradle/RatTask.groovy @@ -31,6 +31,10 @@ class RatTask extends DefaultTask { boolean failOnError = true boolean verbose = false + boolean xmlOutput = true + boolean htmlOutput = true + boolean plainOutput = false + @Input String inputDir = '.' @@ -50,16 +54,59 @@ class RatTask extends DefaultTask { if( !reportDir.exists() ) { reportDir.mkdirs() } - def xmlReport = generateXmlReport() - def errorCount = countUnaprovedUnknownLicenses( xmlReport ) - def htmlReport = generateHtmlReport( xmlReport ) + def mainReport = null + def errorCount = -1 + if( plainOutput ) { + mainReport = generatePlainReport() + errorCount = countUnaprovedUnknownLicensesFromPlain( mainReport ) + } + def xmlReport = null + if( xmlOutput || htmlOutput ) { + xmlReport = generateXmlReport() + mainReport = mainReport ?: xmlReport + errorCount = countUnaprovedUnknownLicenses( xmlReport ) + } + if( xmlReport != null && htmlOutput ) { + mainReport = generateHtmlReport( xmlReport ) + } + if( xmlReport && !xmlOutput ) { + xmlReport.delete() + } if( failOnError && errorCount > 0 ) { throw new GradleException( - "Found $errorCount files with unapproved/unknown licenses. See ${htmlReport.toURI()}" + "Found $errorCount files with unapproved/unknown licenses. See ${mainReport.toURI()}" ) } } + def generatePlainReport() { + def plainReport = new File( reportDir, 'rat-report.txt' ) + def antBuilder = services.get( IsolatedAntBuilder ) + def ratClasspath = project.configurations.rat + antBuilder.withClasspath( ratClasspath ).execute { + ant.taskdef( resource: 'org/apache/rat/anttasks/antlib.xml' ) + ant.report( format: 'plain', reportFile: plainReport.absolutePath ) { + fileset( dir: inputDir ) { + patternset { + excludes.each { exclude( name: it ) } + } + } + } + } + project.logger.info "Rat TXT report: ${plainReport.toURI()}" + return plainReport + } + + def countUnaprovedUnknownLicensesFromPlain( plainReport ) { + def errorCount = 0 + plainReport.readLines().each { line -> + if( line.endsWith( 'Unknown Licenses' ) ) { + errorCount = line.substring( 0, line.indexOf( ' ' ) ).toInteger() + } + } + return errorCount + } + def generateXmlReport() { def xmlReport = new File( reportDir , 'rat-report.xml' ) def antBuilder = services.get( IsolatedAntBuilder ) diff --git a/apache-rat-gradle/src/gradle/src/test/groovy/org/apache/rat/gradle/RatIntegrationSpec.groovy b/apache-rat-gradle/src/gradle/src/test/groovy/org/apache/rat/gradle/RatIntegrationSpec.groovy index 15ae854b8..8d1873e4c 100644 --- a/apache-rat-gradle/src/gradle/src/test/groovy/org/apache/rat/gradle/RatIntegrationSpec.groovy +++ b/apache-rat-gradle/src/gradle/src/test/groovy/org/apache/rat/gradle/RatIntegrationSpec.groovy @@ -132,4 +132,94 @@ class RatIntegrationSpec extends IntegrationSpec { fileExists( 'build/reports/rat-custom/index.html' ) } + def 'success on only plain text report'() { + setup: + fork = true + def inputDir = buildFile.parentFile.absolutePath.replaceAll('\\\\', '/') + buildFile << """ + apply plugin: 'java' + apply plugin: 'org.apache.rat' + rat { + verbose = true + inputDir = '$inputDir' + plainOutput = true + xmlOutput = false + htmlOutput = false + excludes = [ + 'build.gradle', 'settings.gradle', 'build/**', '.gradle/**', '.gradle-test-kit/**', + 'no-license-file.txt' + ] + } + """.stripIndent() + createFile( 'no-license-file.txt' ).text = 'Nothing here.' + + when: + ExecutionResult result = runTasksSuccessfully( 'check' ) + + then: + wasExecuted( 'rat' ) + fileExists( 'build/reports/rat/rat-report.txt' ) + !fileExists( 'build/reports/rat/rat-report.xml' ) + !fileExists( 'build/reports/rat/index.html' ) + } + + def 'fail the build when finding a file with unapproved/unknown license on plain text report only'() { + setup: + fork = true + def inputDir = buildFile.parentFile.absolutePath.replaceAll('\\\\', '/') + buildFile << """ + apply plugin: 'org.apache.rat' + rat { + verbose = true + inputDir = '$inputDir' + plainOutput = true + xmlOutput = false + htmlOutput = false + excludes = [ + 'build.gradle', 'settings.gradle', 'build/**', '.gradle/**', '.gradle-test-kit/**' + ] + } + """.stripIndent() + createFile( 'no-license-file.txt' ).text = 'Nothing here.' + + when: + ExecutionResult result = runTasksWithFailure( 'rat' ) + + then: + wasExecuted( 'rat' ) + fileExists( 'build/reports/rat/rat-report.txt' ) + !fileExists( 'build/reports/rat/rat-report.xml' ) + !fileExists( 'build/reports/rat/index.html' ) + } + + def 'success on plain text and html report'() { + setup: + fork = true + def inputDir = buildFile.parentFile.absolutePath.replaceAll('\\\\', '/') + buildFile << """ + apply plugin: 'java' + apply plugin: 'org.apache.rat' + rat { + verbose = true + inputDir = '$inputDir' + plainOutput = true + xmlOutput = false + htmlOutput = true + excludes = [ + 'build.gradle', 'settings.gradle', 'build/**', '.gradle/**', '.gradle-test-kit/**', + 'no-license-file.txt' + ] + } + """.stripIndent() + createFile( 'no-license-file.txt' ).text = 'Nothing here.' + + when: + ExecutionResult result = runTasksSuccessfully( 'check' ) + + then: + wasExecuted( 'rat' ) + fileExists( 'build/reports/rat/rat-report.txt' ) + !fileExists( 'build/reports/rat/rat-report.xml' ) + fileExists( 'build/reports/rat/index.html' ) + } } diff --git a/apache-rat-gradle/src/site/apt/index.apt b/apache-rat-gradle/src/site/apt/index.apt index 4722cade7..2de3a488a 100644 --- a/apache-rat-gradle/src/site/apt/index.apt +++ b/apache-rat-gradle/src/site/apt/index.apt @@ -66,6 +66,17 @@ rat { // Fail the build on rat errors, defaults to true failOnError = false + // Enable XML RAT output, defaults to true + xmlOutput = true + + // Enable HTML RAT output, defaults to true + htmlOutput = true + + // Enable plain text RAT output, defaults to false + // Please note that if xml or html output is enabled too, + // then two RAT runs will be needed to produce all reports. + plainOutput = false + } -------