在build.gradle中定义和调用自定义方法

1.全局方法

为项目范围创建的,即.全局可用于项目,可以在构建脚本中的任何位置使用myMethod(p1, p2)等效的方式调用project.myMethod(p1, p2)

ext.myMethod = { param1, param2 ->
    // Method body here
}
ext.hg = [

    cloneOrPull: { source, dest, branch ->
        if (!dest.isDirectory())
            hg.clone(source, dest, branch)
        else
            hg.pull(dest)
        hg.update(dest, branch)
    },

    clone: { source, dest, branch ->
        dest.mkdirs()
        exec {
            commandLine 'hg', 'clone', '--noupdate', source, dest.absolutePath
        }
    },

    pull: { dest ->
        exec {
            workingDir dest.absolutePath
            commandLine 'hg', 'pull'
        }
    },

]

调用方式:

hg.clone('path/to/repo')

猜你喜欢

转载自blog.csdn.net/github_37610197/article/details/127938499