|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import org.gradle.api.Plugin |
| 21 | +import org.gradle.api.Project |
| 22 | +import org.gradle.api.Task |
| 23 | +import org.gradle.api.internal.project.IsolatedAntBuilder |
| 24 | + |
| 25 | +apply plugin: RatPlugin |
| 26 | + |
| 27 | +class RatTask extends DefaultTask { |
| 28 | + @Input |
| 29 | + List<String> excludes |
| 30 | + |
| 31 | + def reportPath = 'build/rat' |
| 32 | + def stylesheet = 'gradle/resources/rat-output-to-html.xsl' |
| 33 | + def xmlReport = reportPath + '/rat-report.xml' |
| 34 | + def htmlReport = reportPath + '/rat-report.html' |
| 35 | + |
| 36 | + def generateXmlReport(File reportDir) { |
| 37 | + def antBuilder = services.get(IsolatedAntBuilder) |
| 38 | + def ratClasspath = project.configurations.rat |
| 39 | + antBuilder.withClasspath(ratClasspath).execute { |
| 40 | + ant.taskdef(resource: 'org/apache/rat/anttasks/antlib.xml') |
| 41 | + ant.report(format: 'xml', reportFile: xmlReport) { |
| 42 | + fileset(dir: ".") { |
| 43 | + patternset { |
| 44 | + excludes.each { |
| 45 | + exclude(name: it) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + def printUnknownFiles() { |
| 54 | + def ratXml = new XmlParser().parse(xmlReport) |
| 55 | + def unknownLicenses = 0 |
| 56 | + ratXml.resource.each { resource -> |
| 57 | + if (resource.'license-approval'.@name[0] == "false") { |
| 58 | + println('Unknown license: ' + resource.@name) |
| 59 | + unknownLicenses++ |
| 60 | + } |
| 61 | + } |
| 62 | + if (unknownLicenses > 0) { |
| 63 | + throw new GradleException("Found " + unknownLicenses + " files with " + |
| 64 | + "unknown licenses.") |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + def generateHtmlReport() { |
| 69 | + def antBuilder = services.get(IsolatedAntBuilder) |
| 70 | + def ratClasspath = project.configurations.rat |
| 71 | + antBuilder.withClasspath(ratClasspath).execute { |
| 72 | + ant.xslt( |
| 73 | + in: xmlReport, |
| 74 | + style: stylesheet, |
| 75 | + out: htmlReport, |
| 76 | + classpath: ratClasspath) |
| 77 | + } |
| 78 | + println('Rat report: ' + htmlReport) |
| 79 | + } |
| 80 | + |
| 81 | + @TaskAction |
| 82 | + def rat() { |
| 83 | + File reportDir = new File(reportPath) |
| 84 | + if (!reportDir.exists()) { |
| 85 | + reportDir.mkdirs() |
| 86 | + } |
| 87 | + generateXmlReport(reportDir) |
| 88 | + printUnknownFiles() |
| 89 | + generateHtmlReport() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class RatPlugin implements Plugin<Project> { |
| 94 | + void apply(Project project) { |
| 95 | + configureDependencies(project) |
| 96 | + project.plugins.apply(JavaPlugin); |
| 97 | + Task ratTask = project.task("rat", |
| 98 | + type: RatTask, |
| 99 | + group: 'Build', |
| 100 | + description: 'Runs Apache Rat checks.') |
| 101 | + project.tasks[JavaPlugin.TEST_TASK_NAME].dependsOn ratTask |
| 102 | + } |
| 103 | + |
| 104 | + void configureDependencies(final Project project) { |
| 105 | + project.configurations { |
| 106 | + rat |
| 107 | + } |
| 108 | + project.repositories { |
| 109 | + mavenCentral() |
| 110 | + } |
| 111 | + project.dependencies { |
| 112 | + rat 'org.apache.rat:apache-rat-tasks:0.11' |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments