gradle - Gradle 应用插件与插件
问题描述
我有一个简单的插件, greet
任务是打印一些“Hello World”。
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group = 'standalone.plugin2.greeting'
version = '1.0'
gradlePlugin {
plugins {
greeting {
id = 'standalone.plugin2.greeting'
implementationClass = 'standalone.plugin2.StandalonePlugin2Plugin'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'standalone.plugin2.greeting'
version = '1.0'
from components.java
}
}
}
现在,我有运行应用程序来运行greet task
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'standalone.plugin2.greeting:standalone-plugin2:1.0'
}
}
apply plugin: 'standalone.plugin2.greeting'
使用apply plugin
natation 可以正常工作,但是当我使用插件表示法时,如下所示:
plugins {
id 'standalone.plugin2.greeting' version '1.0'
}
它不起作用。
错误信息是:
* What went wrong:
Plugin [id: 'standalone.plugin2.greeting', version: '1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'standalone.plugin2.greeting:standalone.plugin2.greeting.gradle.plugin:1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
有什么区别?根据文档apply plugin
是旧的,不应该使用。
解决方案
在引入plugins
块之前,插件依赖项必须以与常规项目依赖项相同的方式使用 和 的组合来repositories
解决dependencies
。由于它们需要在运行实际构建脚本之前解决,因此需要在特殊buildscript
块中定义它们:
buildscript {
repositories {
// define repositories for build script dependencies
}
dependencies {
// define build script dependencies (a.k.a. plugins)
}
}
repositories {
// define repositories for regular project dependencies
}
dependencies {
// define regular project dependencies
}
一旦解决了依赖关系,就可以使用apply plugin:
.
默认情况下,新plugins
块仅解析来自Gradle Plugin Repository的插件。这是一个常规的 Maven 存储库,因此也可以使用旧的方式使用它:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
在您的情况下,该插件仅存在于 中mavenLocal
,因此该plugins
块无法解析它,因为它仅查看 Gradle Central Plugin Repository。您可以使用该pluginManagement
块来解析自定义存储库中的插件。
如上面链接的文章中所述,有必要在插件标识符(在plugins
块内使用)和提供相应插件的 Maven 坐标之间创建链接。要创建此链接,必须发布遵循特定约定的标记工件。如果与Maven Publish Plugin结合使用,Gradle Plugin Development Plugin会自动发布此标记工件。
推荐阅读
- cs50 - 我不断收到分段错误,我不知道为什么。我可能错误地使用 strcasecmp 函数,但我不确定
- swift - 如何在导航栏(Xcode)中将右栏按钮项放在彼此之上?
- amazon-web-services - 如何限制对 REST API 的访问以仅匹配用户的 ID?
- xamarin.forms - Xamarin Forms两个ListView垂直并排
- design-patterns - 如何在 Rust 中可变地使用堆栈上多个实例中的一个实例?
- c++ - 如何为多个队列设计出队算法
- python - 有没有一种方法可以让我的乒乓球根据它在球拍上的位置移动?游戏
- numpy - 如何在numpy数组的特定位置添加数字
- javascript - 如何从反应中的锚标签或链接标签中删除虚线?
- linux - 为什么我们在 Linux 命令中使用 -i?