Reputation: 7965
I have an Android Studio project which consists of several modules.
Some of these modules need to run an external pre-processing tool before they're compiled. Plus, I also need to run another tool project-wide every time ANY module in the project is compiled
Unfortunately I'm new to Gradle and I'm being a bit overwhelmed by it. I've succesfully included a new Gradle script in my project by editing settings.gradle
and I've written in it some Gradle tasks that run the tools I want to run, and that is fine.
But the problem is I don't understand how to hook them up so they get executed at the right moment.
Basically, I want to know how to edit my build scripts in order to:
A) Always run a certain gradle task before any module in the project is built. (If possible, I'd want this to run only once, in the sense that if I rebuild the entire project, which is composed of 20+ modules, I don't want it to run 20 times, just once. This is secondary tho, the main thing is that it needs to run every time any module in the project is built)
B) Always run a certain gradle task before a certain module is built. In other words: how do I edit the build script of a single, specific module in order to run a certain task before compilation? Note that in this case the invoked task needs to be able to know which gradle project invoked it, ie: which module is being compiled.
Hope the questions are clear, I'll clarify if necessary (as I said, I'm new to Gradle, hope I didn't mess up the terminology too much)
Upvotes: 3
Views: 1812
Reputation: 318
Answer to A is task dependency, I'm assuming when you say modules you mean subprojects, the ones you included in your settings.gradle. If all these sub projects requires this task to run first, I would define the subprojects:
subprojects {
apply plugin: 'java'
task precompiletask() {
println "Executing pre-compile task"
}
compileJava.dependsOn precompiletask
}
The second part of A you might get for free if you setup your task correctly and put inputs/outputs. This is what gradle checks if it needs to rerun the task again or not. If nothing change in inputs/outputs then it won't run the precompiletask and would skip it.
For part B, what I would do with this is find a common attributes between these projects and configure them:
configure(someProjects()) {
// do whatever you want here to those projects
// for example, set up pre compile task like the one above
}
def someProjects() {
ext.someProjects = [] as Set
ext.someProjects.addAll subprojects.findAll { Project aProject ->
// filter here what's common with those projects
// for example, all projects that have yml file
}
logger.debug("Some projects [{}]", ext.someProjects)
ext.someProjects
}
Hope this helps, have fun :)
Upvotes: 1