Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame^] | 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 = '.' |
| 32 | def xmlReport = reportPath + '/rat-report.xml' |
| 33 | |
| 34 | def generateXmlReport(File reportDir) { |
| 35 | def antBuilder = services.get(IsolatedAntBuilder) |
| 36 | def ratClasspath = project.configurations.rat |
| 37 | antBuilder.withClasspath(ratClasspath).execute { |
| 38 | ant.taskdef(resource: 'org/apache/rat/anttasks/antlib.xml') |
| 39 | ant.report(format: 'xml', reportFile: xmlReport) { |
| 40 | fileset(dir: ".") { |
| 41 | patternset { |
| 42 | excludes.each { |
| 43 | exclude(name: it) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | def printUnknownFiles() { |
| 52 | def ratXml = new XmlParser().parse(xmlReport) |
| 53 | ratXml.resource.each { resource -> |
| 54 | if (resource.'license-approval'.@name[0] == "false") { |
| 55 | println('Unknown license: ' + resource.@name) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @TaskAction |
| 61 | def rat() { |
| 62 | File reportDir = new File(reportPath) |
| 63 | if (!reportDir.exists()) { |
| 64 | reportDir.mkdirs() |
| 65 | } |
| 66 | generateXmlReport(reportDir) |
| 67 | printUnknownFiles() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | class RatPlugin implements Plugin<Project> { |
| 72 | void apply(Project project) { |
| 73 | configureDependencies(project) |
| 74 | project.plugins.apply(JavaBasePlugin); |
| 75 | Task ratTask = project.task("rat", |
| 76 | type: RatTask, |
| 77 | group: 'Build', |
| 78 | description: 'Runs Apache Rat checks.') |
| 79 | project.tasks[JavaBasePlugin.CHECK_TASK_NAME].dependsOn ratTask |
| 80 | } |
| 81 | |
| 82 | void configureDependencies(final Project project) { |
| 83 | project.configurations { |
| 84 | rat |
| 85 | } |
| 86 | project.repositories { |
| 87 | mavenCentral() |
| 88 | } |
| 89 | project.dependencies { |
| 90 | rat 'org.apache.rat:apache-rat-tasks:0.8' |
| 91 | } |
| 92 | } |
| 93 | } |