Gradle 7 迁移:无法应用 PublishPlugin (maven-publish):无法 运行 afterEvaluate 当项目已经被评估时

Gradle 7 Migration: Failed to apply PublishPlugin (maven-publish): Cannot run afterEvaluate when the project is already evaluated

将 Gradle 从 6.8 升级到 7.1.1 后,我得到:

A problem occurred evaluating script.
> Failed to apply plugin class 'org.gradle.api.publish.plugins.PublishingPlugin'.
   > Cannot run Project.afterEvaluate(Action) when the project is already evaluated.

从我的 publish.gradle 文件中抛出:

configure(subprojects.findAll({ it.name in ["subpr1", "subpr2", "subpr3"] })) {
    subproject ->
        apply plugin: 'com.android.library'
        apply plugin: 'maven-publish'  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

我没有在我的 build.gradle 文件中找到任何 evaluation*evaluate* 任务用法。

有什么解决办法吗?

我的根build.gradle:


buildscript {
    ext.gradle_version = "7.0.0"

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
    }
}

ext {
    ...
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

def properties = new Properties()
properties.load(new FileInputStream("local.properties"))

allprojects {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        maven {
            url = uri("...")
            credentials {
                username = properties.getProperty("...")
                password = properties.getProperty("...")
            }
        }
    }
}

apply plugin: 'android-reporting'

task publishAll() {
    subprojects.each { pr ->
        dependsOn {
            pr.tasks.findAll { task -> task.name.startsWith('publish') }
        }
    }
    outputs.upToDateWhen { false }
}
publishAll.outputs.upToDateWhen { false }

task clean(type: Delete) {
    delete rootProject.buildDir
}

apply from: 'publish.gradle'

我的猜测是这里有问题 and/or,它与迁移指南部分有关 "afterEvalute is now an error"

task publishAll() {
    subprojects.each { pr ->
        dependsOn {
            pr.tasks.findAll { task -> task.name.startsWith('publish') }
        }
    }
    outputs.upToDateWhen { false }
}
publishAll.outputs.upToDateWhen { false }

subproject1build.gradle:

plugins {
    id 'com.android.library'
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionName rootProject.ext.cyfaceBackendVersion
        testInstrumentationRunner rootProject.ext.testInstrumentationRunner
    }

    productFlavors {
        a {
            dimension "project"
        }
        b {
            dimension "project"
        }
        mock {
            dimension "mode"
        }
        full {
            dimension "mode"
        }
    }

    buildTypes {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }

    compileOptions {
        sourceCompatibility rootProject.ext.sourceCompatibility
        targetCompatibility rootProject.ext.targetCompatibility
    }

    lintOptions {
        disable 'ObsoleteLintCustomCheck'
    }
}

dependencies {...}

问题是:

apply from: 'publish.gradle'

evaluate 阶段之后执行,因此,错误:

Cannot run afterEvaluate when the project is already evaluated

解决方法是在buildSrc中写一个convention gradle插件。

要跟进,请参阅例如这个 video 来自 Gradle.

我遇到了几乎相同的问题。 root build.gradle

中有如下代码
plugins {
    id 'java'
}

description = "MyPrject"

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":module1"))
    implementation(project(":module2"))
    implementation(project(":module3"))
}

...

gradle.projectsEvaluated {

    subprojects {

        apply plugin: 'java'
        apply plugin: 'maven-publish'

        repositories {
            mavenCentral()
        }

        task sourcesJar(type: Jar, dependsOn: classes) {
            archiveClassifier.set("sources")
            from sourceSets.main.allSource
        }

        jar {
            manifest {
                attributes(
                        'Implementation-Title': 'MyPrject',
                        'Implementation-Version': project.version
                )
            }
        }

        publishing {
            publications {
                mavenJava(MavenPublication) {
                    artifactId "$archivesBaseName"

                    from components.java
                    artifact sourcesJar
                }
            }

            repositories {
                ...
            }
        }

    }
}

在我的例子中,解决方案是从子项目部分删除应用插件命令并将其添加到我所有的模块 build.gradle 文件(module1/build.gradle,module2/build.gradle 和 module3/build.gradle)

plugins {
    id 'java'
    id 'maven-publish'
}

之后 Gradle 7.X 版本构建成功