如何重用code块,描述了常规similiar蚂蚁构建逻辑? [英] How to reuse code block which describe similiar ant build logic in groovy?

查看:180
本文介绍了如何重用code块,描述了常规similiar蚂蚁构建逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重用code块,描述了常规similiar蚂蚁构建逻辑?

How to reuse code block which describe similiar ant build logic in groovy?

如果我们建立这是由Groovy的AntBuilder实现,就像下面code逻辑:

If we have build logic which was implemented by Groovy AntBuilder, just like code below:

ant.someTask(attr1:value1, attr2:value2) {
    configuration1(param1:args1, param2:args2){
        similiarStructure(additionalArgs:aaa){
            setting1(param5:value5) {
                //...blah blah blah
            }
            //further more settings, may be or may be not the same with similiarStructure below
        }
    }

    configuration2(param3:args3, param4:args4){
        similiarStructure(additionalArgs:aaa){
            setting1(param5:value5) {
                //...blah blah blah
            }
            //further more settings, may be or may be not the same with similiarStructure below
        }
    }
}

是否有重用的Groovy AntBuilder code座,这可能会在介绍中说明书的configuration2方式?

我已经尝试predefine封锁和在这两个配置它们注入,

但它未能与地产未发现异常,同时初始化关闭。

Are there any ways to reuse Groovy AntBuilder code block, which could brief the statment in configuration2 ?
I've try to predefine closures and inject them in both configuration,
but it fails with property not found exception while initializing closure.

推荐答案

我将提供两个答案,所以你可以选择哪一个更适合您的使用情况并进行测试。这些解决方案在您需要的共享配置什么水平取决于

I'll provide two answers so you can select which one is more appropriate for your use case and test it. The solutions depend on at what level you want the shared config.

如果你想要一个更通用的解决方案,它可以让你分享整个 similarStructure 块,您需要执行一些更高级的工作。关键是要保证的代表的共享配置关闭设置正确:

If you want a more general purpose solution that allows you to share the whole of the similarStructure block, you need to perform some more advanced work. The trick is to ensure that the delegate of the shared configuration closure is set appropriately:

def sharedConfig = {
    similarStructure(additionalArgs:aaa) {
        setting1(param5:value5) {
            //...blah blah blah
        }
    }
}

ant.someTask(attr1: value1, attr2: value2) {
    configuration1(param1:args1, param2:args2){
        applySharedConfig(delegate, sharedConfig)
    }

    configuration2(param3:args3, param4:args4){
        applySharedConfig(delegate, sharedConfig)
    }
}

void applySharedConfig(builder, config) {
    def c = config.clone()
    c.resolveStrategy = Closure.DELEGATE_FIRST
    c.delegate = builder
    c.call()
}

虽然 applySharedConfig()方法似乎丑陋的,它可以被用来在不同的任务共享多种配置。

Although the applySharedConfig() method seems ugly, it can be used to share multiple configurations across different tasks.

有一件事要记住这种解决方案是, resolveStrategy 关闭的可能是非常重要的。我认为无论 DELEGATE_FIRST OWNER_FIRST (默认)将正常工作在这里。如果碰上什么似乎是名称解析问题(丢失的方法或属性),你应该尝试切换解决策略。

One thing to bear in mind with this solution is that the resolveStrategy of the closure can be very important. I think both DELEGATE_FIRST and OWNER_FIRST (the default) will work fine here. If you run into what appear to be name resolution problems (missing methods or properties) you should try switching the resolution strategy.

这篇关于如何重用code块,描述了常规similiar蚂蚁构建逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆