Gradle 分发插件将公共部分提取到闭包

Gradle distribution plugin extract common parts to closure

我正在尝试使用 gradle 分发插件为我的项目创建多个分发版。

我成功了,但是有很多重复,我想知道是否有一种方法可以定义闭包以涵盖不同分布中的相似性?

像这样的东西会很棒:

apply plugin: 'distribution'

def commonPart = { location ->
    into('a') {
        from("$projectDir/src/main/config/$location/A")
    }
    into('b') {
        from("$projectDir/src/main/config/$location/B")
    }
    ..
    <lots more>
}

distributions {

    firstPackage {
        contents {
            ['shared', 'concrete-a'].each commonPart
        }
    }

    secondPackage {
        contents {
            ['shared', 'concrete-b'].each commonPart
        }
    }
}

但我明白了:

Could not find method firstPackage() for arguments [build_dt0cpe0f6o27n2ggb10318bwh$_run_closure2$_closure10@5e60e639] on project ':test.project'.

它将是:

apply plugin: 'distribution'

def commonPart = { location ->
    return {
         into('a') {
         from("$projectDir/src/main/config/$location/A")
       }
       into('b') {
         from("$projectDir/src/main/config/$location/B")
       }
    }
}

distributions {

    firstPackage {
        ['shared', 'concrete-a'].collect { contents commonPart(it) }
    }

    secondPackage {
        ['shared', 'concrete-b'].collect { contents commonPart(it) }  
    }
}

Here你可以找到一个演示。