dweiss commented on a change in pull request #1157: Add RAT check using Gradle URL: https://github.com/apache/lucene-solr/pull/1157#discussion_r365109858
########## File path: gradle/validation/rat-sources.gradle ########## @@ -0,0 +1,167 @@ +import org.gradle.api.internal.project.IsolatedAntBuilder + +/* + * 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. + */ + +// This applies the Apache RAT plugin to our source and test files + +// Largely copied from Apache Kafka +apply plugin: RatPlugin +// This invocation needs to go out to each project instead of being here +rat { +} + +class RatTask extends DefaultTask { + @Input + List<String> includes = [] + + @Input + List<String> excludes = [] + + def reportDir = project.file('build/rat') + def xmlReport = new File(reportDir, 'rat-report.xml') + + def generateXmlReport(File reportDir) { + // Probably better to use the IsolatedAntBuilder if we can, but it seems to have issues with substringMatcher + // def antBuilder = services.get(IsolatedAntBuilder) + + def ratClasspath = project.configurations.rat + def projectPath = project.getRootDir().getAbsolutePath() + ant.taskdef(resource: 'org/apache/rat/anttasks/antlib.xml', classpath: ratClasspath.asPath) + ant.report(format: 'xml', reportFile: xmlReport, addDefaultLicenseMatchers: true) { + fileset(dir: projectPath) { + patternset { + includes.each { + include(name: it) + } + excludes.each { + exclude(name: it) + } + } + } + + // The license rules below were manually copied from lucene/common-build.xml, there is currently no mechanism to sync them + + // BSD 4-clause stuff (is disallowed below) + substringMatcher(licenseFamilyCategory: "BSD4 ", licenseFamilyName: "Original BSD License (with advertising clause)") { + pattern(substring: "All advertising materials") + } + + // BSD-like stuff + substringMatcher(licenseFamilyCategory: "BSD ", licenseFamilyName: "Modified BSD License") { + // brics automaton + pattern(substring: "Copyright (c) 2001-2009 Anders Moeller") + // snowball + pattern(substring: "Copyright (c) 2001, Dr Martin Porter") + // UMASS kstem + pattern(substring: "THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF MASSACHUSETTS AND OTHER CONTRIBUTORS") + // Egothor + pattern(substring: "Egothor Software License version 1.00") + // JaSpell + pattern(substring: "Copyright (c) 2005 Bruno Martins") + // d3.js + pattern(substring: "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS") + // highlight.js + pattern(substring: "THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS") + } + + // MIT-like + substringMatcher(licenseFamilyCategory: "MIT ", licenseFamilyName:"Modified BSD License") { + // ICU license + pattern(substring: "Permission is hereby granted, free of charge, to any person obtaining a copy") + } + + // Apache + substringMatcher(licenseFamilyCategory: "AL ", licenseFamilyName: "Apache") { + pattern(substring: "Licensed to the Apache Software Foundation (ASF) under") + // this is the old - school one under some files + pattern(substring: "Licensed under the Apache License, Version 2.0 (the "License")") + } + + substringMatcher(licenseFamilyCategory: "GEN ", licenseFamilyName: "Generated") { + // <!-- svg files generated by gnuplot --> + pattern(substring: "Produced by GNUPLOT") + // <!-- snowball stemmers generated by snowball compiler --> + pattern(substring: "This file was generated automatically by the Snowball to Java compiler") + // <!-- parsers generated by antlr --> + pattern(substring: "ANTLR GENERATED CODE") + } + + approvedLicense(familyName: "Apache") + approvedLicense(familyName: "The MIT License") + approvedLicense(familyName: "Modified BSD License") + approvedLicense(familyName: "Generated") + } + } + + 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.") + } + } + + @TaskAction + def rat() { + if (!reportDir.exists()) { + reportDir.mkdirs() + } + def origEncoding = System.getProperty("file.encoding") + try { + System.setProperty("file.encoding", "UTF-8") //affects the output of the ant rat task + generateXmlReport(reportDir) + printUnknownFiles() + } finally { + System.setProperty("file.encoding", origEncoding) + } + } +} + +class RatPlugin implements Plugin<Project> { Review comment: I wouldn't go into plugin creation at this moment. You don't need a plugin to define a task - just define the task class and add tasks of that type on projects where they're applicable. You can use ant tasks from gradle directly - no need to isolate anything. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org