Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '.'

Expand All @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
}
}
11 changes: 11 additions & 0 deletions apache-rat-gradle/src/site/apt/index.apt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an extremely expensive requirement for large code bases. We've seen with the RAT/gradle integration that the Samza team uses that it's definitely possible to generate multiple outputs from one run.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin uses the report ant task that runs rat and produces a single output.

plainOutput = true triggers a rat run and output plain text report
xmlOutput = true triggers a rat run and output xml report
htmlOutput = true triggers xmlOutput and xsl the xml output to html report

Defaults are respectively false, true, true.
If you only need the plain text output set all theses to their opposite.

However, it could be possible to get all reports with a single rat run by rewriting the plugin to use the RAT api directly. Or should we propose an enhancement for rat ant tasks?

plainOutput = false

}
-------

Expand Down