Reputation: 6364
How to execute a task before executing or building the subprojects in gradle?
I have a multi-project build with the following build.gradle
in the root project
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
protobuf {
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.10.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
}
}
generateProtoTasks {
ofSourceSet('main')*.plugins {
grpc {}
}
}
}
tasks.withType(JavaCompile) {
dependsOn 'protobuf'
}
allprojects {
}
subprojects {
}
I want the protobuf tasks to be executed first before compiling any of the subprojects because my subprojects depends on the generated java files from protobuf task. so how can I achieve this? I don't want to have the protobuf task in each subproject instead I just want to do it at once at the root project but before compiling the subprojects.
Upvotes: 0
Views: 1669
Reputation: 2502
I have created a sample project for this. The root project is called 'protobuffer' and it has two sub projects as follows:
'proto' project contains proto files for the java project. proto project's build.gradle.kts file is as follows:
import com.google.protobuf.gradle.*
plugins {
id ("com.google.protobuf")
}
tasks.check { dependsOn("generateProto") }
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.6.1"
}
generatedFilesBaseDir = File(project(":java-project").projectDir.toString(), "src").toString()
}
dependencies {
implementation("com.google.protobuf:protobuf-java:3.6.1")
}
Root project's build.gradle.kts file is as follows:
import com.google.protobuf.gradle.GenerateProtoTask
plugins {
java
id ("com.google.protobuf") version ("0.8.8") apply false
}
allprojects {
repositories {
mavenCentral()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}
subprojects {
apply(plugin="java")
apply(plugin="idea")
if (name != "proto") {
tasks.withType<JavaCompile> {
dependsOn(project(":proto").tasks.withType<GenerateProtoTask>())
}
}
}
The settings.gradle.kts file in the root project is as follows:
rootProject.name = "protobuffer"
include("proto")
include("java-project")
pluginManagement {
repositories {
mavenLocal()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.google.protobuf") {
useModule("com.google.protobuf:protobuf-gradle-plugin:${requested.version}")
}
}
}
}
Upvotes: 1