Gradle 任务失败时重试

标签 gradle groovy artifactory

这是间歇性失败的任务,当手动重新运行时,它通常会通过,我想以编程方式重试此任务 3 次,每次重试之间有 5 秒的延迟,直到将其声明为真正的失败:-

uploadWildFlyWar {
    dependsOn createAndCopySasJar
    description 'Uploads the Wild-fly war'
    group 'publishing'
    repositories {
        mavenDeployer {
            repository(url: repoProps["remoteRepoUrlVal"]) {
                authentication(userName: repoProps["remoteRepoUsernameVal"], password: repoProps["remoteRepoPasswordVal"])
            }
        }
    }
}

失败原因:-

What went wrong: Execution failed for task ':commons:src:uploadWildFlyWar'. Could not publish configuration 'wildFlyWar' Failed to deploy artifacts: Could not transfer artifact common:wildfly-startup:war:Branch-8.8.3.5c8e849b5 from/to remote (https://artifactory.abc.com/artifactory/cprms-build-branch): Failed to transfer file: https://artifactory.abc.com/artifactory/cprms-build-branch/com/ideas/cpro/common/wildfly-startup/Branch-8.8.3.5c8e849b5/wildfly-startup-Branch-8.8.3.5c8e849b5.war. Return code is: 502, ReasonPhrase: Bad Gateway.

最佳答案

假设您不追求“干净”的解决方案,您可以像这样解决它:

// configure the uploadWildFlyWar task
uploadWildFlyWar { task ->
    // remember the original task actions, copying them to a new list as the  returned list is a live view
    def actions = [*task.actions]
    // replace the original task actions with one own task action that does the error handling
    task.actions = [{
        // remembered exceptions from first two runs
        def exceptions = []
        // try up to three times
        for (i in 1..3) {
            try {
                // execute the original actions
                actions.each { it.execute(task) }
                // if the original actions executed successfully, break the loop
                break
            } catch (e) {
                // on the third try if still throwing an exception
                if (i == 3) {
                    // add the first two exceptions as "suppressed" ones
                    exceptions.each { e.addSuppressed(it) }
                    // rethrow the exception
                    throw e
                } else {
                    // remember the first two exceptions
                    exceptions.add(e)
                    // wait 5 seconds before retrying
                    sleep 5000
                }
            }
        }
    } as Action]
}

关于Gradle 任务失败时重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75975241/

相关文章:

android - 更新 Gradle

json - 使用 Groovy JsonBuilder 时如何保留时区?

gradle - 将Gradle从Groovy转换为Kotlin DSL(用于liquibase-gradle-plugin)

java - 模块依赖与 Artifactory

java - 如何将 cURL 放入 Java 中

jenkins-plugins - Jenkins Artifactory插件: Error occurred while requesting version information: Connection refused

gradle - 如何在 `org.ow2.asm:asm-tree:4.0 -> 5.0.3 (*)`中了解 `gradle dependencies`?

java - Gradle深度讲解中的build-by-convention是什么?

android - list 合并异常: SAXParseException

hibernate - 发生异常后如何从 `don' 恢复 t 刷新 session `错误?