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

Posted

技术标签:

【中文标题】如何为 gradle 发布任务设置超时?【英文标题】:How to set timeout for gradle publish task? 【发布时间】:2021-10-19 08:42:11 【问题描述】:

我使用“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)
        
    

【问题讨论】:

【参考方案1】:

Task 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

【讨论】:

以上是关于如何为 gradle 发布任务设置超时?的主要内容,如果未能解决你的问题,请参考以下文章

如何为查询执行设置语句超时

如何为 liquibase 变更集设置超时?

如何为 RabbitMQ RPC 请求设置超时?

如何为 cassandra 设置读取请求超时

如何为 mysql 设置 ActiveRecord 查询超时?

如何为网站设置 JSON API 超时?