如何为 gradle 发布任务设置超时?

How to set timeout for gradle publish task?

我使用“gradle publish”命令发布到 nexus repo。

配置如下所示:

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'org.gradle.sample'
            artifactId = 'library'
            version = '1.1'

            from components.java
        }
    }
}

任务超时可以这样设置:

task someTask {
    timeout = Duration.ofMinutes(25)
}

我想为发布配置部分生成的所有发布任务(我认为发布配置会生成更多任务)设置超时。 这样的事情似乎不起作用:

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'org.gradle.sample'
            artifactId = 'library'
            version = '1.1'

            from components.java

            timeout = Duration.ofMinutes(25)
        }
    }
}

任务 publish 是一个聚合任务,没有任何操作。它将根据您的配置生成一个名为 publishPubNamePublicationToRepoNameRepository 的任务,并替换为 PubName。可以用--dry-run查找它生成的是哪个任务:

$ gradle publish --dry-run
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
:generateMetadataFileForMavenPublication SKIPPED
:generatePomFileForMavenPublication SKIPPED
:publishMavenPublicationToMavenRepository SKIPPED
:publish SKIPPED

BUILD SUCCESSFUL in 1s

所以我的配置会生成一个发布任务publishMavenPublicationToMavenRepository。然后我可以找到那个任务并设置它的超时时间:

plugins {
    id("java")
    id("maven-publish")
}

project.afterEvaluate {
    tasks.findByName("publishMavenPublicationToMavenRepository").configure {
        timeout = Duration.ofMillis(1)
    }
}

请注意 tasks.findByName 被放入 project.afterEvaluate 块中,因为 publish 动态生成该任务。

这是预期的结果:

$ gradle publish

> Task :publishMavenPublicationToMavenRepository FAILED
Requesting stop of task ':publishMavenPublicationToMavenRepository' as it has exceeded its configured timeout of 1ms.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMavenPublicationToMavenRepository'.
> Timeout has been exceeded

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
5 actionable tasks: 3 executed, 2 up-to-date