newSunrain 2023-04-21 12:33 采纳率: 80%
浏览 104
已结题

自定义了一个gradle插件也发行在了本地mavenlocal(),但在想使用这个自定义插件而配置依赖时classpath 报错

问题遇到的现象和发生背景

自定义了一个gradle插件也发行在了本地mavenlocal(),但在想使用这个自定义插件而配置依赖时classpath 报错

遇到的现象和发生背景,请写出第一个错误信息

rootProject叫gradletest
自定义插件的所在module叫myplugin
自定义插件的class叫PluginTest
详细目录

img

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
  1. 这是rootProject的build.gralde
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()
}



  1. 这是自定义插件所在module的build.gralde
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()
    }
}



  1. 这是myplugin下的properties文件内容
implementation-class=com.sunrain.PluginTest

  1. 这是实际发行在mavenLoca()上的自定义插件位置

img

运行结果及详细报错内容
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


我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
  1. 首先的看一下mavenLocal有没有自定义插件,但好像有
  2. 然后是classpath要求的gav坐标对不对,但好像也对
  3. 不知道了
    我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
    可以通过依赖找到在mavenLocal中的自定义插件
  • 写回答

5条回答 默认 最新

  • 极客智能体-在线 2023-04-21 23:57
    关注

    以下答案由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
    

    如果执行了以上所有步骤后,问题依然存在,请在问题发生时附上详细的错误信息,具体的目录结构和文件名称等信息,方便深入分析。
    如果我的回答解决了您的问题,请采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月30日
  • 已采纳回答 4月22日
  • 赞助了问题酬金15元 4月21日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果