【问题标题】:whenReady not setting gradle property before task runswhenReady 在任务运行之前未设置 gradle 属性
【发布时间】:2014-10-25 00:33:15
【问题描述】:

我的 gradle.properties 文件中有一个版本属性,它提供了我正在构建的版本。我在构建中有一个名为 release 的任务,如果任务图中存在,它将上传到快照存储库。然而发生的事情是,即使我在构建任务中包含发布任务,当 uploadArchives 运行时,快照也不会附加到我的版本属性中,因此它会尝试上传到错误的存储库并失败。准备就绪时运行,但它似乎没有在uploadArchives 之前运行。谁能解释这里发生了什么?

uploadArchives {
    repositories {
        ivy {
            credentials {
                username nexusUser
                password nexusPassword
            }
            if (version.endsWith("-SNAPSHOT")) {
                url nexusSnapshotRepository
            } else {
                url nexusReleaseRepository
            }
        }
    }
}

gradle.taskGraph.whenReady {taskGraph -> 
    if (!taskGraph.hasTask(release)) { 
        version = version + '-SNAPSHOT'
    }
    println "release task not included - version set to $version" 
}

task release(){
    doLast{
        println "Releasing"
    }
}

这与 gradle 网站上的示例非常相似,所以我看不出哪里出了问题。

http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html

【问题讨论】:

    标签: gradle


    【解决方案1】:

    脚本在配置阶段检查project.version 的值(不是在任务执行时),但仅在构建任务执行图后才对其进行修改。解决此问题的一种方法是从 taskGraph.whenReady 回调中覆盖存储库 url:

    uploadArchives {
        repositories {
            ivy {
                name "nexus"
                url nexusReleaseRepository
                ...
            }
        }
    }
    
    gradle.taskGraph.whenReady { taskGraph -> 
        if (!taskGraph.hasTask(release)) { 
            version += '-SNAPSHOT'
            uploadArchives.repositories.nexus.url nexusSnapshotRepository 
            // ps: println has to go inside here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2018-05-06
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多