English 中文(简体)
Is there a way to split/factor out common parts of Gradle build
原标题:
  • 时间:2010-04-02 12:45:46
  •  标签:
  • gradle
  • build

We have several independent builds (each independent build is a multi-project build). The main build scripts have become quite big as we have a set of common tasks reused by subprojects. There is also a lot of repetition between independent builds. What we are looking for is:

  1. A way to split the main build file into smaller files
  2. A way to reuse some parts of the build in other independent builds

What is the best way to achieve this in Gradle?

最佳回答

Gradle 0.9 allows you to import a build script from another build script. Have a look at: Configuring the project using an external build script. Basically it s apply from: other.gradle .

One thing the user guide doesn t mention is that the from parameter can be a URL, so you can make your shared scripts available via HTTP somewhere (eg your subversion repository), and import them from multiple builds.

问题回答

The solution I found implies mapping the things you have in your other.gradle file.

def getVersionName = { testParam ->
    println "${testParam}"

    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine  git ,  describe ,  --tags 
        standardOutput = stdout
    }
    return stdout.toString().trim()
}
ext{
    VERConsts = [:]
    VERConsts[ NAME ] = getVersionName("test param")
    VERConsts[ NAME_CALL ] = getVersionName
}

Then, in your build.gradle file:

apply from:  other.gradle 
// ...
android {
    defaultConfig {
        versionName VERConsts[ NAME_CALL ]("test param")
        // or
        versionName VERConsts[ NAME ]
    }
}

Then, the versionName will have the call result.

Notes:

  • VERConsts[ NAME ] = getVersionName() will call getVersionName() and store its result. Using it in your script e.g. versionName VERConsts[ NAME ] will then assign the stored value.
  • VERConsts[ NAME_CALL ] will instead store a reference to the function. Using VERConsts[ NAME_CALL ]() in your script will actually call the function and assign the result to your variable

The former will result in the same value being assigned across the script while the latter may result in different values (e.g. if someone pushes another version while your script is running).





相关问题
Elegant way building url request with parameters

There must me a more elegant way to build a URL with parameters in .NET then for example Response.Write("<a href=HeadOfMarketView.aspx"+Session["HOM"] != null ? Session["HOM"]+">Head of Market&...

buildforge problem

when i tried to run the job i am getting the error saying that No server could be found matching all conditions please any one help me on this

您能否防止MSBuild.exe经营活动?

我用文字建造一些项目,偶尔利用习俗制造事件,给建筑系统造成了很大困难。 如果有可能,我想援引MSBuild.exe。

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Keyboard shortcut to build only startup project?

I ve tried to find a keyboard shortcut to build only the startup project In VS2008. I only found a configurable shortcut to build the project which is currently viewed, which isn t as good. Any ...

Dependency bundle (jar-files/sources/API docs) in Eclipse

I m developing various in-house extensions for JIRA, the issue tracker we use. So far I worked with Netbeans and everything worked like a charm. However, now I need to switch to Eclipse and I m ...

How to link to a static library in C?

I use code::blocks to compile my static library. The output result is a libstatic.a file. Now, how do I link to my library to use functions that were compiled? (I tried to use #include "libstatic.a"...

热门标签