| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Licensed to the Apache Software Foundation (ASF) under one | 
					
						
							|  |  |  |  * or more contributor license agreements.  See the NOTICE file | 
					
						
							|  |  |  |  * distributed with this work for additional information | 
					
						
							|  |  |  |  * regarding copyright ownership.  The ASF licenses this file | 
					
						
							|  |  |  |  * to you under the Apache License, Version 2.0 (the | 
					
						
							|  |  |  |  * "License"); you may not use this file except in compliance | 
					
						
							|  |  |  |  * with the License.  You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, | 
					
						
							|  |  |  |  * software distributed under the License is distributed on an | 
					
						
							|  |  |  |  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
					
						
							|  |  |  |  * KIND, either express or implied.  See the License for the | 
					
						
							|  |  |  |  * specific language governing permissions and limitations | 
					
						
							|  |  |  |  * under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import org.gradle.api.internal.project.IsolatedAntBuilder | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-09 10:06:01 +08:00
										 |  |  | import java.nio.charset.StandardCharsets | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  | apply plugin: RatPlugin | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RatTask extends DefaultTask { | 
					
						
							|  |  |  |   @Input | 
					
						
							|  |  |  |   List<String> excludes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 23:18:28 +08:00
										 |  |  |   def reportDir = project.file('build/rat') | 
					
						
							|  |  |  |   def stylesheet = project.file('gradle/resources/rat-output-to-html.xsl').getAbsolutePath() | 
					
						
							|  |  |  |   def xmlReport = new File(reportDir, 'rat-report.xml') | 
					
						
							|  |  |  |   def htmlReport = new File(reportDir, 'rat-report.html') | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def generateXmlReport(File reportDir) { | 
					
						
							|  |  |  |     def antBuilder = services.get(IsolatedAntBuilder) | 
					
						
							|  |  |  |     def ratClasspath = project.configurations.rat | 
					
						
							| 
									
										
										
										
											2018-08-18 23:18:28 +08:00
										 |  |  |     def projectPath = project.getRootDir().getAbsolutePath() | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  |     antBuilder.withClasspath(ratClasspath).execute { | 
					
						
							|  |  |  |       ant.taskdef(resource: 'org/apache/rat/anttasks/antlib.xml') | 
					
						
							|  |  |  |       ant.report(format: 'xml', reportFile: xmlReport) { | 
					
						
							| 
									
										
										
										
											2018-08-18 23:18:28 +08:00
										 |  |  |         fileset(dir: projectPath) { | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  |           patternset { | 
					
						
							|  |  |  |             excludes.each { | 
					
						
							|  |  |  |               exclude(name: it) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def printUnknownFiles() { | 
					
						
							|  |  |  |     def ratXml = new XmlParser().parse(xmlReport) | 
					
						
							|  |  |  |     def unknownLicenses = 0 | 
					
						
							|  |  |  |     ratXml.resource.each { resource -> | 
					
						
							|  |  |  |       if (resource.'license-approval'.@name[0] == "false") { | 
					
						
							|  |  |  |         println('Unknown license: ' + resource.@name) | 
					
						
							|  |  |  |         unknownLicenses++ | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (unknownLicenses > 0) { | 
					
						
							|  |  |  |       throw new GradleException("Found " + unknownLicenses + " files with " + | 
					
						
							|  |  |  |                                 "unknown licenses.") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def generateHtmlReport() { | 
					
						
							|  |  |  |     def antBuilder = services.get(IsolatedAntBuilder) | 
					
						
							|  |  |  |     def ratClasspath = project.configurations.rat | 
					
						
							|  |  |  |     antBuilder.withClasspath(ratClasspath).execute { | 
					
						
							|  |  |  |       ant.xslt( | 
					
						
							|  |  |  |           in: xmlReport, | 
					
						
							|  |  |  |           style: stylesheet, | 
					
						
							|  |  |  |           out: htmlReport, | 
					
						
							|  |  |  |           classpath: ratClasspath) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     println('Rat report: ' + htmlReport) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @TaskAction | 
					
						
							|  |  |  |   def rat() { | 
					
						
							|  |  |  |     if (!reportDir.exists()) { | 
					
						
							|  |  |  |       reportDir.mkdirs() | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-13 07:28:29 +08:00
										 |  |  |     def origEncoding = System.getProperty("file.encoding") | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2021-02-09 10:06:01 +08:00
										 |  |  |       System.setProperty("file.encoding", StandardCharsets.UTF_8.name()) //affects the output of the ant rat task
 | 
					
						
							| 
									
										
										
										
											2016-10-13 07:28:29 +08:00
										 |  |  |       generateXmlReport(reportDir) | 
					
						
							|  |  |  |       printUnknownFiles() | 
					
						
							|  |  |  |       generateHtmlReport() | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       System.setProperty("file.encoding", origEncoding) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RatPlugin implements Plugin<Project> { | 
					
						
							|  |  |  |   void apply(Project project) { | 
					
						
							|  |  |  |     configureDependencies(project) | 
					
						
							|  |  |  |     project.plugins.apply(JavaPlugin); | 
					
						
							|  |  |  |     Task ratTask = project.task("rat", | 
					
						
							|  |  |  |         type: RatTask, | 
					
						
							|  |  |  |         group: 'Build', | 
					
						
							|  |  |  |         description: 'Runs Apache Rat checks.') | 
					
						
							|  |  |  |     project.tasks[JavaPlugin.TEST_TASK_NAME].dependsOn ratTask | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void configureDependencies(final Project project) { | 
					
						
							|  |  |  |     project.configurations { | 
					
						
							|  |  |  |       rat | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     project.repositories { | 
					
						
							|  |  |  |       mavenCentral() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     project.dependencies { | 
					
						
							| 
									
										
										
										
											2019-01-10 17:14:30 +08:00
										 |  |  |       rat 'org.apache.rat:apache-rat-tasks:0.13' | 
					
						
							| 
									
										
										
										
											2015-07-07 06:47:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |