簡體   English   中英

Gradle / Groovy:將復制閉包移動到單獨的util方法

[英]Gradle/Groovy: move copy closure to seperate util method

我有這個自定義gradle插件,它基於一些自定義規格創建tar文件:

tar.into("${project.name}-${project.version}"){
    into('lib'){
        from project.tasks.jar
        from project.configurations.runtime
    }

    //fix line endings for *.sh and *.conf files
    from("src/main/assembly"){
        include '**/*.sh'
        include '**/*.conf'
        filter(FixCrLfFilter, eol:FixCrLfFilter.CrLf.newInstance("unix")) // change line endings to unix format
        fileMode = 0744
        dirMode = 0755
    }

    //leave rest of the assembly untouched except for the filtered files above
    from("src/main/assembly"){
        exclude '**/*.sh'
        exclude '**/*.conf'
        fileMode = 0744
        dirMode = 0755
    }
}

我想將兩個“ from(“ src / main / assembly”)“塊提取到一個單獨的util類中,以便我可以在另一個插件中重用它們。 像這樣:

class AssemblyUtil {

    def static CopySpec assemblyFiles = copySpec {
        //fix line endings for *.sh and *.conf files
        from("src/main/assembly"){
            include '**/*.sh'
            include '**/*.conf'
            filter(FixCrLfFilter, eol:FixCrLfFilter.CrLf.newInstance("unix")) // change line endings to unix format
            fileMode = 0744
            dirMode = 0755
        }

        //leave rest of the assembly untouched except for the filtered files above
        from("src/main/assembly"){
            exclude '**/*.sh'
            exclude '**/*.conf'
            fileMode = 0744
            dirMode = 0755
        }
    }
}

然后能夠將原始方法重構為:

tar.into("${project.name}-${project.version}"){
    into('lib'){
        from project.tasks.jar
        from project.configurations.runtime
    }

    with AssemblyUtil.assemblyFiles
}

這樣,我可以在其他插件中重用閉包塊。

它不起作用。 我不確定語法。 這可能嗎? 有人可以幫我弄對嗎?

謝謝!

只是一個瘋狂的猜測,但這可能與在靜態上下文中定義規范有關。 也許以下工作有效:

class AssemblyUtil {

    def static CopySpec assemblyFiles(Project project) = project.copySpec {
        ...
    }
}

然后將項目傳遞給此方法

tar.into("${project.name}-${project.version}"){
    into('lib'){
        from project.tasks.jar
        from project.configurations.runtime
    }

    with AssemblyUtil.assemblyFiles(project)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM