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

时间:2017-01-20 17:31:22

标签: gradle gradle-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()方法内部访问模型元素,因为它使用项目空间,而不是模型空间。我不知道如何在RuleSource规则中使用它。

1 个答案:

答案 0 :(得分:0)

答案来自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 =)