gradle -在执行gradle存储库解析之前调用自定义任务

eqqqjvef  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在使用gradle-7.2,我想调用自定义任务来派生svn分支,然后在仓库中使用相同的URL,但找不到这样做的方法.
这是我在build.gradle中自定义任务:

task svninfo {
    println "***svninfo***"
    doLast {
        new ByteArrayOutputStream().withStream { os ->
            def result = exec {
                executable = 'svn'
                args = ['info']
                standardOutput = os
            }
            def outputAsString = os.toString()
            println "outputAsString #: $outputAsString"
            def matchSvnBranch = outputAsString =~ /URL:.*\/branches\/(.*)\/deployready\/.*/
            //def matchLastChangedRev = outputAsString =~ /Last Changed Rev: (\d+)/
            def branchName=${matchSvnBranch[0][1]}
            println "svn branch #: ${matchSvnBranch[0][1]}"
            println "svn.branch #: ${branchName}"
            ext.branch = branchName
            
        }
    }
}

repositories {
    //svninfo() --- not working, getting error
    maven {
            url "myurl-$branch"
            
            allowInsecureProtocol = true
        }
}

**更新:**根据注解,我更改为doFirst {},但仍然出现错误。我无法在repositories闭包中调用svninfo任务。出现以下错误:

Could not find method svninfo() for arguments [] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

请指导我如何在仓库演化之前调用svninfo()任务,这样我就可以在maven Url中使用变量branch了。

p8ekf7hl

p8ekf7hl1#

好吧,经过几次试验和错误,最后这个工作:

def svninfo() {
    println "***svninfo***"
        new ByteArrayOutputStream().withStream { os ->
            def result = exec {
                executable = 'svn'
                args = ['info']
                standardOutput = os
            }
            def outputAsString = os.toString()
            println "outputAsString #: $outputAsString"
            def matchSvnBranch = outputAsString =~ /URL:.*\/branches\/(.*)\/deployready\/.*/
            def branchName=matchSvnBranch[0][1]
            println "svn branch #: ${matchSvnBranch[0][1]}"
            println "svn.branch #: ${branchName}"
            ext.branch = branchName
            
        }
}

svninfo()

repositories {
                maven {
                    url "myurl-$branch"
                    
                    allowInsecureProtocol = true
                }
            }

不知道为什么定义为任务不工作。现在我试图移动这个逻辑到初始化脚本,因为这将是模块的公共逻辑。

相关问题