We have a very large infrastructure (500+ projects being built). Because of this we have developed custom plugins/tasks to gradle. When a new project is created, we ship a default build.gradle file to the project (which the project can not modify). This build.gradle sets up the bootstrapping between Gradle and our custom plugins (shipped as a jar file). The build.gradle file looks like this:
buildscript {
dependencies {
classpath files(fileTree(dir: 'lib/build', includes: ['*.jar']))
// Wire in the custom plugins/tasks into Gradle's bootstrapping
}
}
apply plugin: 'tiaa-base'
// Apply our default base plugin which does some filesystem setup
apply from: 'project-build.gradle'
// Apply the project's specific build instructions
What we expect is that each project can supply its own file called project-build.gradle where it can do anything specific to itself. This approach seems to work fine, although I notice something strange and I’m not sure if it is a bug within Gradle or something I’m not doing correctly.
If we look at a sample project’s project-build.gradle:
apply plugin: 'tiaa-war'
// Apply our own custom war plugin
buildDistributionZip {
// Configure the task for creating our distribution
from(buildDir) {
include 'docs/**'
include 'libs/**'
include 'reports/**'
include 'test-results/**'
}
}
// Declare a task which uses a custom type for generating sources off of an XSD schema
task generateContactsRSv1Schema(type: org.tiaa.reco.build.types.Xjc, description: 'Generate Contacts RS v1 sources from the schema') {
javaPackageName = 'org.tiaa.prism.microsites.rest.services.types.contacts.v1'
schemaFile = "contacts-rs-v1/contacts-rs-v1.xsd"
}
What’s happening when I try & run the build is I get this message:
FAILURE: Build failed with an exception.
* Where:
Script 'C:\Projects1\MicrositesRS\project-build.gradle' line: 18
* What went wrong:
A problem occurred evaluating script.
> Could not find property 'org' on root project 'MicrositesRS'.
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating script.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory
.java:54)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:12
7)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyScript(DefaultObjectConfigurationAction
.java:82)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.access$000(DefaultObjectConfigurationAction.
java:32)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction$1.run(DefaultObjectConfigurationAction.java:
54)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.execute(DefaultObjectConfigurationAction.jav
a:114)
at org.gradle.api.internal.project.AbstractProject.apply(AbstractProject.java:841)
at org.gradle.api.Project$apply.call(Unknown Source)
at org.gradle.api.internal.project.ProjectScript.apply(ProjectScript.groovy:34)
at org.gradle.api.Script$apply.callCurrent(Unknown Source)
at build_4ufgq0n1cmv0vgp71ugqv0ekjg.run(C:\Projects1\MicrositesRS\build.gradle:10)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory
.java:52)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:12
7)
at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)
at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:43)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:463)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:75)
at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:23)
at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:21)
at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:38)
at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:35)
at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:439)
at org.gradle.api.internal.project.AbstractProject.allprojects(AbstractProject.java:434)
at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:139)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:110)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:78)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:38)
at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.
java:39)
at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.
java:25)
at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:30)
at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:22)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:20
0)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:17
3)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:138)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
at org.gradle.launcher.Main.doAction(Main.java:48)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at org.gradle.launcher.Main.main(Main.java:39)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32)
at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
Caused by: groovy.lang.MissingPropertyException: Could not find property 'org' on root project 'MicrositesRS'.
at org.gradle.api.internal.AbstractDynamicObject.propertyMissingException(AbstractDynamicObject.java:43)
at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:35)
at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeDynamicObject.java:60)
at org.gradle.groovy.scripts.BasicScript.propertyMissing(BasicScript.java:70)
at project_build_2lgaaa41l51un4uapfke0r5n76.run(C:\Projects1\MicrositesRS\project-build.gradle:18)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory
.java:52)
... 44 more
BUILD FAILED
Total time: 2.48 secs
If, at the top of the project-build.gradle file I include the same block that is in the build.gradle file
buildscript {
dependencies {
classpath files(fileTree(dir: 'lib/build', includes: ['*.jar']))
}
}
Then everything works. What’s weird is even though the block is not declared in the project-build.gradle file, gradle doesn’t complain about the line where I apply my custom plugin:
apply plugin: 'tiaa-war'
But it breaks at the line where I declare the task of the type org.tiaa.reco.build.types.Xjc, saying that the property ‘org’ can’t be found on the project. Obviously the classes are there on the classpath, otherwise the lines above it (applying the plugin, configuring the buildDistributionZip task) would fail, since the custom plugins introduce that stuff.
Why doesn’t declaring the task work? Why do I need to include the buildscript block again in my project-build.gradle file? Is this a bug with Gradle or am I not doing something correctly?