marcoferrer / kroto-plus

gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Apache License 2.0
493 stars 28 forks source link

NoClassDefFoundError when applying Gradle plugin from Gradle Plugin Portal #112

Open gabrielhuff opened 4 years ago

gabrielhuff commented 4 years ago

How to reproduce

The following minimal build.gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
    id "com.google.protobuf" version "0.8.12"
    id "com.github.marcoferrer.kroto-plus" version "0.6.1"
    id "idea"
}

krotoPlus {
    config {
        main {}
    }
}

Will throw an exception (I'm using Gradle 5.6). Specifically, running this:

$ ./gradlew

Will throw this:

...
Caused by: java.lang.NoClassDefFoundError: com/google/protobuf/MessageOrBuilder
        at com.github.marcoferrer.krotoplus.gradle.compiler.CompilerConfigWrapper.<init>(CompilerConfigWrapper.kt:46)
        at com.github.marcoferrer.krotoplus.gradle.KrotoPlusPluginExtension$config$1.create(KrotoPlusPluginExtension.kt:31)
        at com.github.marcoferrer.krotoplus.gradle.KrotoPlusPluginExtension$config$1.create(KrotoPlusPluginExtension.kt:26)
        at org.gradle.api.internal.FactoryNamedDomainObjectContainer.doCreate(FactoryNamedDomainObjectContainer.java:106)
 ...

What fixes it

Getting the buildscript dependency from maven central instead of the Gradle plugin portal will fix the problem:

buildscript { // <-- add this
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.github.marcoferrer.krotoplus:kroto-plus-gradle-plugin:0.6.1"
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.61'
    id "com.google.protobuf" version "0.8.12"
    id "idea"
}

apply plugin: 'com.github.marcoferrer.kroto-plus' // <-- and this

krotoPlus {
    config {
        main {}
    }
}

Why this is happening

The plugin has a runtime dependency to com.google.protobuf:protobuf-java:3.9.0, which is not available on the Gradle Plugin Portal.


I just discovered this repo. You've done an amazing job! I've heard that Google is working on an official Kotlin API, but honestly it's going to be hard for them to catch up to this project :)

marcoferrer commented 4 years ago

Thanks for submitting this! Since we aren’t embedding the dependency in within the plugin this error is expected. One alternative to adding the maven repository within the build script block is using the pluginManagement in settings.gradle


pluginManagement{
    repositories {
        gradlePluginPortal()
        jcenter()
    }
}

This would let you continue to use the plugins block in your build.gradle. Since this Gradle plugin is experimental your feedback might be a sign that we should consider embedding the dependency in the future