如何在自定义gradle插件中解决NoClassDefFoundError问题?

时间:2018-09-10 13:39:47

标签: java maven gradle build.gradle gradle-plugin

我正在构建一个自定义的gradle插件。使用gradle构建插件时,会抛出NoClassDefFoundError:

[...]
Caused by: java.lang.reflect.InvocationTargetException
        at org.gradle.plugin.devel.tasks.ValidateTaskProperties$1.visitFile(ValidateTaskProperties.java:181)
        ... 54 more
Caused by: org.gradle.internal.impldep.com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: com/example/I18nBoundaryException
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2199)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache.get(LocalCache.java:3934)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4827)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.getTypeMetadata(DefaultPropertyMetadataStore.java:152)
        at org.gradle.api.internal.tasks.properties.PropertyValidationAccess$BeanTypeNodeFactory.createRootNode(PropertyValidationAccess.java:86)
        at org.gradle.api.internal.tasks.properties.PropertyValidationAccess.collectTaskValidationProblems(PropertyValidationAccess.java:69)
        ... 55 more
Caused by: java.lang.NoClassDefFoundError: com/example/I18nBoundaryException
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.getFields(DefaultPropertyMetadataStore.java:281)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.access$100(DefaultPropertyMetadataStore.java:81)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore$3.visitType(DefaultPropertyMetadataStore.java:164)
        at org.gradle.internal.reflect.Types.walkTypeHierarchy(Types.java:58)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.createTypeMetadata(DefaultPropertyMetadataStore.java:158)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.access$000(DefaultPropertyMetadataStore.java:81)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore$1.load(DefaultPropertyMetadataStore.java:114)
        at org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore$1.load(DefaultPropertyMetadataStore.java:111)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
        at org.gradle.internal.impldep.com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
        ... 62 more

I18nBoundaryException是在同一项目的另一个IntelliJ模块中定义的自定义Exception,因此我认为问题在于将类包含在插件的类路径中。

我的build.gradle文件如下:

apply plugin: "java-gradle-plugin"
apply plugin: "groovy"

gradlePlugin {
    plugins {
        i18nGradlePlugin {
            id = 'i18n-gradle-plugin'
            implementationClass = 'com.example.I18nPlugin'
        }
    }
}

dependencies {
    compile gradleApi()
    compile project(":library")

    compileOnly libraries.lombok

    testCompile libraries.junit
    testCompile gradleTestKit()
}

:library中定义了Exception类。通过Intellij构建代码(而不是通过gradle构建)可以正常工作。

如何在类路径中包含必要的依赖项?

更新:构建Maven插件时也会出现此问题。插件jar和库jar均上载到了maven可以在其中找到的存储库中。存储库中有一个pom文件,清楚表明library.jar是maven插件的依赖项。

0 个答案:

没有答案