Gradle插件将org.springframework.boot加载到项目中

时间:2019-05-17 15:56:24

标签: spring-boot gradle gradle-plugin spring-boot-gradle-plugin

我正在编写自定义gradle插件Foo,我想加载org.springframework.boot插件 进入应用Foo插件的项目。我可以用这种方式加载其他各种插件,但这 特定的插件不希望行为相同。

Foo build.gradle

buildscript {
    ext {
        springBootVersion = "2.1.3.RELEASE"
    }
}

apply plugin: "groovy"

repositories {
    maven { url "http://custom.repo/blah" }
}

dependencies {
    implementation gradleApi()
    implementation localGroovy()

    implementation("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}

Foo插件

class BuildPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        project.repositories {
            maven { url "http://custom.repo/blah" }
        }

        project.plugins.apply("org.springframework.boot")
    }
}

项目build.gradle

buildscript {
    dependencies {
        classpath files("/some/cool/path/foo-plugin.jar")
    }
}

apply plugin: "com.whatever.foo-id"

项目构建输出

$ ./gradlew --stacktrace clean build

FAILURE: Build failed with an exception.

* Where:
Build file '/cool/project/location/bar/build.gradle' line: 40

* What went wrong:
A problem occurred evaluating root project 'bar'.
> Failed to apply plugin [id 'com.whatever.foo-id']
   > Plugin with id 'org.springframework.boot' not found.

是否可以从插件2应用插件1,其中插件1是类路径依赖项?

1 个答案:

答案 0 :(得分:0)

这是不可能的。类路径是插入插件的原因,因此它们在插入之前就无法修改。