Created
April 8, 2015 13:57
-
-
Save johnrengelman/9a20697b2246a9bfaca2 to your computer and use it in GitHub Desktop.
Self applying Gradle plugin project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//place this content in your buildSrc/build.gradle file | |
//Then apply your plugin to its own build in build.gradle | |
import org.codehaus.groovy.control.CompilerConfiguration | |
apply plugin: 'groovy' | |
repositories { | |
jcenter() | |
} | |
// IntelliJ doesn't let you set the source path for a module outside of its directory. | |
// This logic basically only performs this configuration when not being resolved in IntelliJ | |
if (!project.plugins.collect { it.class.name}.any { it.endsWith('JetGradlePlugin')}) { | |
//Set the source directories for buildSrc to contain the source from the main project | |
sourceSets { | |
main { | |
java.srcDirs = ['src/main/java', '../src/main/java'] | |
groovy.srcDirs = ['src/main/groovy', '../src/main/groovy'] | |
resources.srcDirs = ['src/main/resources', '../src/main/resources'] | |
} | |
} | |
} | |
// Prepare to have your mind blown | |
// This section evaluates the `build.gradle` from the root diretory and | |
// then grabs the dependencies block from it and evaluates it on this project. | |
// So in effect, we are building the main project with itself. | |
// BOOM | |
ScriptHolder holder = new ScriptHolder() | |
CompilerConfiguration cc = new CompilerConfiguration() | |
cc.setScriptBaseClass(DelegatingScript.class.name) | |
GroovyShell sh = new GroovyShell(Project.class.classLoader, new Binding(), cc) | |
//The build file for the main project | |
File projectBuildFile = file('../build.gradle') | |
//Use this parse command because Groovy wants to use the file name as the classname | |
//which fails if your Gradle build file has been renamed to contain an invalid character (i.e. '-') | |
DelegatingScript script = (DelegatingScript)sh.parse(projectBuildFile.text, 'GradlePlugins') | |
script.setDelegate(holder) | |
//Resolve the project main Gradle file against our ScriptHolder | |
script.run() | |
//Class for holding the evaluation of a Gradle script | |
//You may need to add some extra methods here depending on what you have all placed in build.gradle | |
class ScriptHolder { | |
Closure dependencies | |
void dependencies(Closure c) { | |
this.dependencies = c | |
} | |
void apply(Map map) { | |
} | |
} | |
//Grab the dependencies closure and resolve it against | |
//the buildSrc project dependencies | |
//This effectively applies the same dependenices from build.gradle into buildSrc/build.gradle | |
//This is required so that when buildSrc is compiled it has the dependencies to compile the source code | |
def closure = holder.dependencies.clone() | |
closure.delegate = project.dependencies | |
closure() |
I found it - I had to add:
class ScriptHolder {
...see gist...
Closure test
void test(Closure c) {
this.test = c
}
}
def test = holder.test.clone()
test.delegate = project.test
test()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm seeing this error while trying to upgrade Gradle 6.8 to 7.2 (and JUnit 4 to 5, etc.):
This error is related to adding this code:
Any ideas on how to fix this?