Gradle 3:如何从自定义插件中添加到模型元素?

Gradle 3: how to add to model elements from inside a custom plugin?

我有一个 build.gradle 文件,用于跨多个平台构建 C++ 文件。这是在许多项目中完成的,所有这些项目都针对相同的平台。我想将这段代码移到一个单独的插件中,以尽量减少构建文件中的代码。

所以,我目前在 build.gradle 中有以下内容:

model {
    platforms {
        "osx-x86_64" {
            operatingSystem "osx"
            architecture "x86_64"
        }

        "stm32f4xx-arm" {
            architecture "arm"
        }

        "windows" {
            operatingSystem "windows"
            architecture "x86_64"
        }

        "linux" {
            operatingSystem "linux"
            architecture "x86_64"
        }

        "pi" {
            operatingSystem "linux"
            architecture "arm"
        }

    }

    toolChains {
        clang(Clang)

        gcc(Gcc) {

            target('stm32f4xx-arm') {
                def prefix = "arm-none-eabi-"
                cCompiler.executable         = prefix + cCompiler.executable
                cppCompiler.executable       = prefix + cppCompiler.executable
                assembler.executable         = prefix + assembler.executable
                linker.executable            = prefix + linker.executable
                staticLibArchiver.executable = prefix + staticLibArchiver.executable
            }

            target('windows') {
                def prefix = "x86_64-w64-mingw32-"
                cCompiler.executable         = prefix + cCompiler.executable
                cppCompiler.executable       = prefix + cppCompiler.executable
                assembler.executable         = prefix + assembler.executable
                linker.executable            = prefix + linker.executable
                staticLibArchiver.executable = prefix + staticLibArchiver.executable
            }

            target('linux') {
                def prefix = "x86_64-linux-"
                cCompiler.executable         = prefix + cCompiler.executable
                cppCompiler.executable       = prefix + cppCompiler.executable
                assembler.executable         = prefix + assembler.executable
                linker.executable            = prefix + linker.executable
                staticLibArchiver.executable = prefix + staticLibArchiver.executable
            }

            target('pi') {
                def prefix = "arm-none-linux-gnueabi-"
                cCompiler.executable         = prefix + cCompiler.executable
                cppCompiler.executable       = prefix + cppCompiler.executable
                assembler.executable         = prefix + assembler.executable
                linker.executable            = prefix + linker.executable
                staticLibArchiver.executable = prefix + staticLibArchiver.executable
            }

        }

    }
}

如何将其移动到插件中?我无法从 Plugin.apply() 方法内部访问模型元素,因为它使用项目 space,而不是模型 space。我不知道如何在 RuleSource 规则中使用它。

答案来自 Gradle 论坛网站 (https://discuss.gradle.org/t/gradle-3-how-to-add-to-model-elements-from-inside-a-custom-plugin/21280) 上的用户 jvff (Janito Vaqueiro Ferreira Filho),我将其复制到这里以防其他人访问此页面。

That would probably be implemented in a Groovy plugin like this:

import org.gradle.model.Mutate
import org.gradle.model.RuleSource
import org.gradle.nativeplatform.toolchain.Clang
import org.gradle.nativeplatform.toolchain.Gcc
import org.gradle.nativeplatform.toolchain.NativeToolChainRegistry
import org.gradle.platform.base.PlatformContainer

public class MyPlugin extends RuleSource {
    @Mutate
    void addPlatforms(PlatformContainer platforms) {
        platforms.create("osx-x86_64") { platform ->
            platform.operatingSystem "osx"
            platform.architecture "x86_64"
        }

        // ...
    }

    @Mutate
    void addToolChains(NativeToolChainRegistry toolChains) {
        toolChains.create("clang", Clang)

        toolChains.create("gcc", Gcc) { gcc ->
            gcc.target('stm32f4xx-arm') {
                def prefix = "arm-none-eabi-"
                cCompiler.executable = prefix + cCompiler.executable
                // ...
            }

            // ...
        }
    }
}
Hope this helps =)