I have a multi-project build in which I am trying to generate some (java) source from a template file in a subproject.
However my genSrc task never executes, even if I invoke it directly:
Your copy task never executes because by the time it executes, it has no inputs configured, so Gradle thinks it’s up to date. You can remove the << after the task declaration, and the closure will be executed immediately in order to configure the task. Afterwards, when configuration of the project ends, the task is executed (if requested or is a dependency of a requested task).
The << operator calls the Task.leftShift() method, which appends the closure to the task action list. Therefore, the closure after the << contains the code to be executed when the task is executed, which in this case configures the task, but because the task wasn’t configured previously, it’s never executed.
For this particular case there is one template file that all the projects should use. Therefore, just the root project has a templates source set. Also, not all projects need/use this task.
If I move the SourceSets to the subprojects block then I run into other issues (such as property not defined) so it seems that having this task defined there is not the right approach.
I suppose it would be better to put this task in a separate build script/plugin and only include it in the subprojects that require it. I’m not exactly sure how to go about doing that but if I recall correctly it is possible.
I was able to solve my issue by putting the task in a seperate .gradle file and using apply from: "$rootProject.projectDir/genSrc.gradle"
in the subprojects I need
Here is the build file with the task to generate the required files
###genSrc.gradle
apply plugin: 'de.fuerstenau.buildconfig'
project.ext {
props = ['packageName': project.packageName]
}
buildConfig {
appName = project.name
clsName = 'VersionInfo' // sets the name of the config class
packageName = 'com.xzy.util'// sets the package of the config class,
charset = 'UTF-8' // sets charset of the generated class,
buildConfigField 'String', 'TARGET_JRE', String.valueOf(project.targetJRE)
buildConfigField 'String', 'BUILD_JRE', String.valueOf(JavaVersion.current())
buildConfigField 'String', 'BUILD_DATE', project.buildDate
}
task genSrc(type: Copy) { task ->
def toDir = "$task.project.buildDir/gen/src/main/java/${task.project.props['packageName'].replace('.', '/')}"
from rootProject.sourceSets.templates.allSource
into toDir
rename('.template', '')
eachFile { file ->
println "transform file " + file.name
expand(props)
}
}
compileJava {
dependsOn genSrc
source 'out/gen/src/main/java/'
}