lucene/gradle/validation/forbidden-apis.gradle

172 lines
5.7 KiB
Groovy

/*
* 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 configures application of forbidden API rules
// via https://github.com/policeman-tools/forbidden-apis
def resources = scriptResources(buildscript)
// Only apply forbidden-apis to java projects.
allprojects { prj ->
plugins.withId("java", {
prj.apply plugin: 'de.thetaphi.forbiddenapis'
// This helper method appends signature files based on a set of true
// dependencies from a given configuration.
def dynamicSignatures = { configuration, suffix ->
def resolvedMods = configuration.resolvedConfiguration.resolvedArtifacts
.collect { a -> a.moduleVersion.id }
def deps = resolvedMods
.collect { id -> [
"${id.group}.${id.name}.all.txt",
"${id.group}.${id.name}.${suffix}.txt",
]}
.flatten()
.sort()
deps += ["defaults.all.txt", "defaults.${suffix}.txt"]
deps.each { sig ->
def signaturesFile = file("${resources}/${sig}")
if (signaturesFile.exists()) {
logger.info("Signature file applied: ${sig}")
signaturesFiles += files(signaturesFile)
} else {
logger.debug("Signature file omitted (does not exist): ${sig}")
}
}
// commons-io is special: forbiddenapis has a versioned bundledSignature.
bundledSignatures += resolvedMods
.findAll { id -> id.group == 'commons-io' && id.name == 'commons-io' }
.collect { id -> "${id.name}-unsafe-${id.version}" as String }
}
// Configure defaults for sourceSets.main
tasks.matching { it.name ==~ /forbiddenApisMain\d*/ }.all {
bundledSignatures += [
'jdk-unsafe',
'jdk-deprecated',
'jdk-non-portable',
'jdk-reflection',
'jdk-system-out',
]
suppressAnnotations += [
"**.SuppressForbidden"
]
doFirst dynamicSignatures.curry(configurations.compileClasspath, "lucene")
}
// Configure defaults for the MR-JAR feature sourceSets by setting java version and ignore missing classes
// TODO:
// - Get hold of warning messages, see https://github.com/policeman-tools/forbidden-apis/issues/207
tasks.matching { it.name ==~ /forbiddenApisMain\d+/ }.all {
failOnMissingClasses = false
}
// Configure defaults for sourceSets.test
tasks.matching { it.name in ["forbiddenApisTest", "forbiddenApisTestFixtures"] }.all {
bundledSignatures += [
'jdk-unsafe',
'jdk-deprecated',
'jdk-non-portable',
'jdk-reflection',
]
signaturesFiles = files(
file("${resources}/defaults.tests.txt")
)
suppressAnnotations += [
"**.SuppressForbidden"
]
if (it.name == "forbiddenApisTestFixtures") {
doFirst dynamicSignatures.curry(configurations.testFixturesCompileClasspath, "lucene")
} else {
doFirst dynamicSignatures.curry(configurations.testCompileClasspath, "lucene")
}
}
// Configure defaults for sourceSets.tools (if present).
tasks.matching { it.name == "forbiddenApisTools" }.all {
bundledSignatures += [
'jdk-unsafe',
'jdk-deprecated',
'jdk-non-portable',
'jdk-reflection',
]
suppressAnnotations += [
"**.SuppressForbidden"
]
doFirst dynamicSignatures.curry(configurations.toolsCompileClasspath, "lucene")
inputs.dir(file(resources))
}
// We rely on resolved configurations to compute the relevant set of rule
// files for forbiddenApis. Since we don't want to resolve these configurations until
// the task is executed, we can't really use them as task inputs properly. This is a
// chicken-and-egg problem.
//
// This is the simplest workaround possible: just point at all the rule files and indicate
// them as inputs. This way if a rule is modified, checks will be reapplied.
configure(tasks.matching { it.name.startsWith("forbiddenApis") }) { task ->
task.inputs.dir(file(resources))
}
// Disable sysout signatures for these projects.
if (prj.path in [
":lucene:demo",
":lucene:benchmark",
":lucene:test-framework"
]) {
forbiddenApisMain.bundledSignatures -= [
'jdk-system-out'
]
}
// the test-framework module is special in that its main sources are exported for all tests.
// include the test signature file (defaults.tests.txt) to the main sourceset.
if (prj.path in [
":lucene:test-framework"
]) {
forbiddenApisMain.signaturesFiles += files(
file("${resources}/defaults.tests.txt")
)
}
// apply logging patterns for all modules except Luke.
if (prj.path !in [
":lucene:luke"
]) {
forbiddenApisMain.signaturesFiles += files(
file("${resources}/defaults.logging.txt")
)
}
// disable forbidden apis for snowball code
if (prj.path == ":lucene:analysis:common") {
forbiddenApisMain.exclude("org/tartarus/**")
}
})
}