【问题标题】:Create custom plugin and using it in another buildscript创建自定义插件并在另一个构建脚本中使用它
【发布时间】:2021-11-14 03:15:36
【问题描述】:

尝试创建自定义插件以跨项目共享构建脚本逻辑。我已经添加了 buildscript 类路径依赖项,但它仍然说找不到插件。我不想在使用自定义插件的项目中手动操作它,因为我将来可能必须更改版本号。有什么解决办法吗?

DependencyManagementPlugin.java

public class DependencyManagementPlugin implements Plugin<Project> {

  @Override
  public void apply(Project project) {

    DependencyHandler dependencies = project.getBuildscript().getDependencies();
    dependencies.add("classpath", "org.springframework.boot:spring-boot-gradle-plugin:2.4.10");
    dependencies.add(
        "classpath",
        "io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:1.0.11.RELEASE");

    PluginContainer plugins = project.getPlugins();
    plugins.apply(MavenPublishPlugin.class);
    plugins.apply(JavaPlugin.class);
    plugins.apply(JacocoPlugin.class);
    plugins.apply("org.springframework.boot");
    plugins.apply("io.spring.dependency-management");
  }
}

build.gradle

plugins {
    id 'groovy-gradle-plugin'
    id 'maven-publish'
}

gradlePlugin {
    plugins {
        dependencyManagementPlugin {
            id = 'com.example.dependency-management'
            implementationClass = 'com.example.DependencyManagementPlugin'
        }
    }
}

在另一个 build.gradle 中使用插件

plugins {
    id 'com.example.dependency-management'
}

...

错误信息:

An exception occurred applying plugin request [id: 'com.example.dependency-management']
> Failed to apply plugin 'com.example.dependency-management'.
   > Plugin with id 'org.springframework.boot' not found.

【问题讨论】:

    标签: java spring spring-boot gradle groovy


    【解决方案1】:

    解决方法是在自定义gradle插件项目的build.gradle的dependencies块中添加依赖。

    https://docs.gradle.org/current/samples/sample_convention_plugins.html

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多