Define and call custom methods in build.gradle

1. Global method

Created for project scope, ie. globally available to the project, you can call project.myMethod(p1, p2) anywhere in your build script with the equivalent of 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'
        }
    },

]

Call method:

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

Guess you like

Origin blog.csdn.net/github_37610197/article/details/127938499