创建自定义插件并在另一个构建脚本中使用它
Create custom plugin and using it in another buildscript
正在尝试创建自定义插件以跨项目共享构建脚本逻辑。我已经添加了 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.
解决方案是在自定义 gradle 插件项目的 build.gradle 的依赖块中添加依赖。
https://docs.gradle.org/current/samples/sample_convention_plugins.html
正在尝试创建自定义插件以跨项目共享构建脚本逻辑。我已经添加了 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.
解决方案是在自定义 gradle 插件项目的 build.gradle 的依赖块中添加依赖。
https://docs.gradle.org/current/samples/sample_convention_plugins.html