自定义了一个gradle插件也发行在了本地mavenlocal(),但在想使用这个自定义插件而配置依赖时classpath 报错
rootProject叫gradletest
自定义插件的所在module叫myplugin
自定义插件的class叫PluginTest
详细目录
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath group :'com.sunrain.plugin' ,name :'library' ,version :'1.1'//这里报错
}
}
plugins {
id 'java'
}
group = 'com.sunrian'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
test {
useJUnitPlatform()
}
apply plugin: 'groovy' //必须
apply plugin: 'maven-publish'
dependencies {
implementation gradleApi() //必须
implementation localGroovy() //必须
}
repositories {
google()
mavenLocal()
jcenter()
mavenCentral() //必须
}
sourceSets { main {
groovy {
srcDir 'src/main/groovy' }
}
}
publishing {
publications {
myLibrary(MavenPublication) {
groupId = 'com.sunrain.plugin' //指定GAV坐标信息
artifactId = 'library'
version = '1.1'
from components.java//发布jar包
//from components.web///引入war插件,发布war包
}
}
repositories {
mavenLocal()
}
}
implementation-class=com.sunrain.PluginTest
PS D:\workspace_idea\gradletest> gradle clean
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'gradletest'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.sunrain.plugin:library:1.1.
Required by:
project :
> No matching variant of com.sunrain.plugin:library:1.1 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute '
org.gradle.plugin.api-version' with value '8.0.2' but:
- Variant 'apiElements' capability com.sunrain.plugin:library:1.1 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2')
- Variant 'runtimeElements' capability com.sunrain.plugin:library:1.1 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2')
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
根据错误信息提示,可以看出是依赖查找失败导致的。现在我们需要排查一下问题的原因。
第一点,查看你的自定义插件的 group id、artifact id 和 version 是否与你在根项目 build.gradle 文件中配置依赖时填写的相同。可以在命令行中运行 gradlew build --refresh-dependencies
确保刷新本地仓库缓存。
第二点,检查一下 mavenLocal 本地仓库中是否真的有你的自定义插件。可以在本地仓库的路径下查找到你的插件的 jar 包。你的自定义插件本应该位于以下路径中:
~/.m2/repository/com/sunrain/plugin/library/1.1/library-1.1.jar
(注意: ~
表示 home 目录,/
表示根目录,具体路径根据个人操作系统和本地仓库路径可能会有所不同)
如果本地仓库中没有你的自定义插件,则可以重新将你的插件发布到本地仓库,使用以下命令重新执行插件发布:
./gradlew publishToMavenLocal
确保成功执行后,再次运行根项目的构建,如果依然报错,请往下阅读。
第三点,检查一下你的自定义插件是否已经加入到 classpath 中。通常在自定义插件的 build.gradle 文件中,都会声明 groupId、artifactId、version 等信息:
group = 'com.sunrain.plugin'
version = '1.1'
确保此处填写的信息与你在根项目 build.gradle 文件中配置依赖时填写的相同。然后在你的根项目 build.gradle 文件中加入以下依赖:
dependencies {
classpath 'com.sunrain.plugin:library:1.1'
}
确保此处的 groupId、artifactId、version 的信息与你在自定义插件中填写的信息相同。如果类路径范围出错,可以使用符号 +
来匹配更多的版本,如:
classpath 'com.sunrain.plugin:library:+'
最后,要在运行构建之前,执行以下命令使 Gradle 重新加载配置:
./gradlew --stop
./gradlew clean
如果执行了以上所有步骤后,问题依然存在,请在问题发生时附上详细的错误信息,具体的目录结构和文件名称等信息,方便深入分析。
如果我的回答解决了您的问题,请采纳!
如果在使用自定义Gradle插件时,classpath报错,可能有以下几种原因:
没有在项目的build.gradle文件中正确地引用插件。
在项目的build.gradle文件中需要使用apply plugin: '插件名称'指令来引用插件,例如:
apply plugin: 'com.example.myplugin'
引用插件时,需要使用插件的完整名称,这个名称通常在插件项目的build.gradle文件中定义(例如:id 'com.example.myplugin')。
./gradlew publishToMavenLocal
这个命令将会将插件发布到本地Maven仓库中,供其他项目使用。
dependencies {
classpath 'com.example:my-plugin:1.0.0'
}
其中,com.example:my-plugin:1.0.0表示自定义插件的Maven坐标,这个坐标需要与自定义插件的build.gradle文件中的定义一致。
java版本不兼容,把java8升级到java11试试
引用chatGPT作答,具体来说,错误信息中提到了以下问题:
找不到 com.sunrain.plugin:library:1.1 依赖
没有找到符合条件的依赖版本,以满足 Java 8 的运行时环境和使用jar打包方式
因此,可以尝试以下方法:
确认自定义插件是否已经正确发布到本地maven仓库中。
确认你的 build.gradle 文件中是否正确引入了自定义插件的依赖。这里可以使用 implementation 来引入依赖,例如:
implementation 'com.sunrain.plugin:library:1.1'
如果你的自定义插件是用Java 11编译的,则无法与Java 8兼容。可以考虑更新自定义插件以便支持Java 8。
另外,建议在运行Gradle命令时加上 --stacktrace 参数,以便查看详细的错误信息和堆栈跟踪,帮助你更快地定位问题。
不知道你这个问题是否已经解决, 如果还没有解决的话:以上答案是正确的,感谢资深IT专家提供的解答。