grgit NoClassDefFoundError

grgit NoClassDefFoundError

Gradle 在尝试执行 grgit 任务时抛出 NoClassDefFoundError。

build.gradle 开始:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'org.ajoberstar:gradle-git:1.2.0'
    }
}

apply plugin: 'com.android.application'
//
//

import org.ajoberstar.grgit.*

task clone << {
    File dir = new File('contrib/otherstuff')
    if(!dir.exists()) {
        def grgit = Grgit.clone(dir: dir, uri: 'https://github.com/someguy/otherstuff.git')
    }
    // TODO else (pull)
}


project.afterEvaluate {
    preBuild.dependsOn clone
}

// rest omitted

输出:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:src:myproject:clone FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/me/src/myproject/build.gradle' line: 20

* What went wrong:
Execution failed for task ':src:myproject:clone'.
> java.lang.NoClassDefFoundError: org/codehaus/groovy/runtime/typehandling/ShortTypeHandling

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 16.937 secs

第 20 行是对 Grgit.clone().

的调用

我是否需要添加 groovy 作为构建依赖项(错误消息似乎表明了这一点)?我应该如何以及在哪里添加它?

编辑:gradle 版本是 1.10,如果重要的话。

我已经解决了。

grgit-1.2.0 似乎依赖于 groovy。在 buildscript/dependencies 块中为 groovy 添加 classpath 条目导致不同的错误:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:src:myproject:clone FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/me/src/myproject/build.gradle' line: 23

* What went wrong:
Execution failed for task ':src:myproject:clone'.
> java.lang.IncompatibleClassChangeError: the number of constructors during runtime and compile time for org.ajoberstar.grgit.auth.AuthConfig$Option do not match. Expected -1 but got 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 12.295 secs

进一步的研究表明,这可能源于版本不兼容(因为其他原因我坚持使用 Gradle 1.10)。

最终我回到 grgit-0.7.0 解决了这个问题。现在,我的 git 任务开始运行并且存储库已被克隆。

正如@user149408 指出的 Gradle 版本(v1.10 vs v2.10)不匹配,我进一步挖掘了一点:

gradle-git-plugin commit for v0.7.0 指定使用的 Gradle 版本 (v1.11),因此使用 v1.10 的构建工作正常。

因为 Gradle 插件总是使用来自 Gradle 的 compile localGroovy()compile gradleApi() 构建,那么如果它使用 Gradle 2.x,它会导致 Groovy 不匹配错误。

  • What went wrong: Execution failed for task ':src:myproject:clone'. java.lang.NoClassDefFoundError: org/codehaus/groovy/runtime/typehandling/ShortTypeHandling

事实上,Gradle v2.10 和 gradle-git v1.2.0 的组合工作正常。

一些样本 build.gradle 与问题中的结构相似。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.ajoberstar:gradle-git:1.2.0'
    }
}

import org.ajoberstar.grgit.*

task clone << {
    File dir = new File('contrib/gs-spring-boot')
    if(!dir.exists()) {
        def grgit = Grgit.clone(dir: dir, uri: 'https://github.com/chenrui333/gs-spring-boot.git')
    }
    // TODO else (pull)
}

./gradlew clone 会给你:

$ ls contrib/gs-spring-boot/
CONTRIBUTING.adoc   LICENSE.code.txt    LICENSE.writing.txt README.adoc         complete            initial             test

希望对您有所帮助!