-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathplugin-init.groovy
77 lines (69 loc) · 3.37 KB
/
plugin-init.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* (C) Copyright Uwe Schindler (Generics Policeman) and others.
*
* Licensed 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.
*/
/** Initializes the plugin and binds it to project lifecycle. */
import java.lang.reflect.Modifier;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.PluginInstantiationException;
if (project.plugins.withType(JavaBasePlugin.class).isEmpty()) {
throw new PluginInstantiationException('Forbidden-apis only works in projects using the java plugin.');
}
// check if running in Gradle Daemon?
// see: http://stackoverflow.com/questions/23265217/how-to-know-whether-you-are-running-inside-a-gradle-daemon
boolean isGradleDaemon = System.getProperty('sun.java.command', '').startsWith('org.gradle.launcher.daemon.') ||
Thread.currentThread().stackTrace.any { it.className.startsWith 'org.gradle.launcher.daemon.' };
if (isGradleDaemon) {
project.logger.info('You are running forbidden-apis in the Gradle Daemon; disabling classloading cache to work around resource leak.');
}
// create Extension for defaults:
def extension = project.extensions.create(FORBIDDEN_APIS_EXTENSION_NAME, CheckForbiddenApisExtension.class);
extension.with{
signaturesFiles = project.files();
disableClassloadingCache |= isGradleDaemon;
}
def extensionProps = CheckForbiddenApisExtension.class.declaredFields.findAll{ f ->
int mods = f.modifiers;
return Modifier.isPublic(mods) && !f.synthetic && !Modifier.isStatic(mods)
}*.name;
// Create a convenience task for all checks (this does not conflict with extension, as it has higher priority in DSL):
def forbiddenTask = project.tasks.create(FORBIDDEN_APIS_TASK_NAME) {
description = "Runs forbidden-apis checks.";
group = JavaBasePlugin.VERIFICATION_GROUP;
}
// Define our tasks (one for each SourceSet):
project.sourceSets.all{ sourceSet ->
def getSourceSetClassesDirs = { sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : project.files(sourceSet.output.classesDir) }
project.tasks.create(sourceSet.getTaskName(FORBIDDEN_APIS_TASK_NAME, null), CheckForbiddenApis.class) { task ->
description = "Runs forbidden-apis checks on '${sourceSet.name}' classes.";
conventionMapping.with{
extensionProps.each{ key ->
map(key, { extension[key] });
}
classesDirs = { getSourceSetClassesDirs() }
classpath = { sourceSet.compileClasspath }
targetCompatibility = { project.targetCompatibility?.toString() }
}
// add dependency to compile task after evaluation, if the classesDirs collection has overlaps with our SourceSet:
project.afterEvaluate{
def sourceSetDirs = getSourceSetClassesDirs().files;
if (classesDirs.any{ sourceSetDirs.contains(it) }) {
task.dependsOn(sourceSet.output);
}
}
forbiddenTask.dependsOn(task);
}
}
// Add our task as dependency to chain
project.tasks[JavaBasePlugin.CHECK_TASK_NAME].dependsOn(forbiddenTask);