简体   繁体   English

如何在 gradle 中执行或构建子项目之前执行任务?

[英]How to execute a task before executing or building the subprojects in gradle?

How to execute a task before executing or building the subprojects in gradle?如何在 gradle 中执行或构建子项目之前执行任务?

I have a multi-project build with the following build.gradle in the root project我在根项目中有一个带有以下build.gradle的多项目构建

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.我希望在编译任何子项目之前先执行 protobuf 任务,因为我的子项目取决于从 protobuf 任务生成的 java 文件。 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.我不想在每个子项目中都有 protobuf 任务,我只想在根项目中但在编译子项目之前立即执行。

I have created a sample project for this.我为此创建了一个示例项目。 The root project is called ' protobuffer ' and it has two sub projects as follows:根项目称为“ protobuffer ”,它有两个子项目,如下所示:

  • java-project项目
  • proto原型

' proto ' project contains proto files for the java project. ' proto ' 项目包含 java 项目的 proto 文件。 proto project's build.gradle.kts file is as follows: proto 项目的build.gradle.kts文件如下:

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: root项目的build.gradle.kts文件如下:

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:根项目中的settings.gradle.kts文件如下:

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}")
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM