使用gradle插件配置另一个gradle插件

时间:2019-03-27 13:46:39

标签: gradle groovy kotlin gradle-plugin

我想实现一个gradle插件,该插件根据插件的配置更改project.version,然后使用更改后的project.version来配置另一个gradle插件,例如用于建造容器。现在的问题是,同时评估两个配置,因此在配置第二个插件时,project.version的更改未应用。

我搜索了Gradle文档,但没有发现有关我的问题的信息。

build.gradle

class VersionPluginExtension {
    String major
    String minor
    String patch
}

class VersionPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('versionPlugin', VersionPluginExtension)
        project.afterEvaluate {
            project.version = "${extension.major}.${extension.minor}.${extension.patch}"
        }
        project.task('showVersion') {
            doLast {
                println "${project.version}"
            }
        }
    }
}

class ContainerPluginExtension {
    String version
}

class ContainerPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('containerPlugin', ContainerPluginExtension)
        project.task('build') {
            doLast {
                println "${extension.version}"
            }
        }
    }
}

apply plugin: VersionPlugin
apply plugin: ContainerPlugin

versionPlugin {
    major = '1'
    minor = '1'
    patch = '1'
}

containerPlugin {
    version = project.version
}

我希望任务构建返回1.1.1而不是unspecified,但是我认为这种方式是不可能的。我希望有人能指出我正确的方向。

谢谢!

2 个答案:

答案 0 :(得分:0)

version的定义从containerPlugin块移动到插件定义:

class ContainerPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('containerPlugin', ContainerPluginExtension)
        project.afterEvaluate {
            extension.version = project.version
        }
        project.task('build') {
            doLast {
                println "${extension.version}"
            }
        }
    }
}

结果:

$ gradle build

> Task :build
1.1.1

答案 1 :(得分:0)

上述问题的解决方案如下:

class VersionPluginExtension {
    String major
    String minor
    String patch
    private String version

    String getVersion() {
        if (!version)
            return "${major}.${minor}.${patch}"
        return version
    }
}

class VersionPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('versionPlugin', VersionPluginExtension)

        project.task('showVersion') {
            doLast {
                println "${extension.version}"
            }
        }
    }
}

class ContainerPluginExtension {
    String version
}

class ContainerPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('containerPlugin', ContainerPluginExtension)

        project.task('build') {
            doLast {
                println "${extension.version}"
            }
        }
    }
}

apply plugin: VersionPlugin
apply plugin: ContainerPlugin

versionPlugin {
    major = '1'
    minor = '1'
    patch = '1'
}

project.version = versionPlugin.version

containerPlugin {
    version = project.version
}

结果:

> Configure project :
1.1.1

> Task :showVersion
1.1.1

> Task :build
1.1.1