Skip to content

Commit 0b128c7

Browse files
uschindlercentic9
authored andcommitted
Issues/116: Compile the Groovy script only once when loading the plugin class (policeman-tools#117)
Compile the Groovy script only once when loading the Gradle plugin class. This closes policeman-tools#116
1 parent c869c3c commit 0b128c7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
import groovy.util.DelegatingScript;
2323

2424
import java.net.URL;
25-
import java.util.Collections;
2625

2726
import org.codehaus.groovy.control.CompilerConfiguration;
2827
import org.codehaus.groovy.control.customizers.ImportCustomizer;
2928
import org.gradle.api.Plugin;
3029
import org.gradle.api.Project;
31-
import org.gradle.api.plugins.PluginInstantiationException;
3230

3331
/**
3432
* Forbiddenapis Gradle Plugin (requires at least Gradle 2.3)
@@ -45,22 +43,29 @@ public class ForbiddenApisPlugin implements Plugin<Project> {
4543
/** Name of the extension to define defaults for all tasks of this module. */
4644
public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis";
4745

48-
@Override
49-
public void apply(final Project project) {
46+
private static final DelegatingScript compiledScript;
47+
static {
5048
final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName());
5149
final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer);
5250
configuration.setScriptBaseClass(DelegatingScript.class.getName());
5351
configuration.setSourceEncoding("UTF-8");
54-
final Binding binding = new Binding(Collections.singletonMap("project", project));
55-
final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), binding, configuration);
52+
final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), new Binding(), configuration);
5653
final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT);
5754
if (scriptUrl == null) {
58-
throw new PluginInstantiationException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT);
55+
throw new RuntimeException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT);
5956
}
6057
final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl);
61-
final DelegatingScript script = (DelegatingScript) shell.parse(csrc);
62-
script.setDelegate(this);
63-
script.run();
58+
compiledScript = (DelegatingScript) shell.parse(csrc);
59+
}
60+
61+
@Override
62+
public void apply(Project project) {
63+
synchronized(compiledScript) {
64+
compiledScript.setDelegate(this);
65+
compiledScript.setProperty("project", project);
66+
compiledScript.run();
67+
compiledScript.setProperty("project", null); // free resources
68+
}
6469
}
6570

6671
}

0 commit comments

Comments
 (0)