grgit NoClassDefFoundError

时间:2017-07-17 22:10:40

标签: java gradle groovy grgit

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,如果重要的话。

2 个答案:

答案 0 :(得分:1)

由于@ 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不匹配错误。

  
      
  • 出了什么问题:任务执行失败':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

希望它有所帮助!

答案 1 :(得分:0)

我设法解决了它。

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

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任务正常工作,repo被克隆了。