Using a Groovy class defined in buildSrc - get java.lang.NoClassDefFoundError:

(Paul Grove) #1

I have a multi project build in one of the sub-projects I would like to use some Groovy classes. After looking through the docs I created the buildSrc/src/main/groovy in the root project and placed a Groovy classes in there, see below

import org.yaml.snakeyaml.Yaml
  class DatabaseHelper {
        Database targetTestDatabase
        String databasesYaml = getLocalResource('databases.yaml')
        Yaml beanLoader = new Yaml()
        databases = beanLoader.loadAs(databasesYaml, HashMap.class)

Interestingly I do not declare any dependencies for org.yaml.snakeyaml.Yaml in the root project but build for buildSrc seems to work fine

:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE

In my sub project I have the below method which is called by a task

def getTargetDb(){
    def databaseHelper = new DatabaseHelper()
    Database targetTestDatabase = databaseHelper.targetTestDatabase
    return targetTestDatabase

But when it runs I get the below

Caused by: java.lang.NoClassDefFoundError: org.yaml.snakeyaml.Yaml
 at DatabaseHelper.class$(DatabaseHelper.groovy)
 at DatabaseHelper.$get$$class$org$yaml$snakeyaml$Yaml(DatabaseHelper.groovy)
 at DatabaseHelper.getLocalResource(DatabaseHelper.groovy:50)
 at DatabaseHelper.<init>(DatabaseHelper.groovy:22)

(Luke Daley) #2

I’ve no idea why it compiles in the first place. Any chance you could provide a sample project that exhibits the behaviour?

To make it work, you just need to add the dependency to ‘buildSrc/build.gradle’.

(Paul Grove) #3

Can you please provide an and example what the build.gradle should look like as I tried this and still no joy

(Luke Daley) #4

Please post what you tried.

(Paul Grove) #5
    maven{url ""}
  dependencies {
    compile 'org.yaml:snakeyaml:1.10'

(Luke Daley) #6

Same result?

(Paul Grove) #7


(Luke Daley) #8

Something very strange is going on.

Any chance you could put together a sample project that’s doing this? I can’t reproduce it.

(Paul Grove) #9

Sorry I’m off on holiday for 10 days wife has banned me from the computer. When I get back I will have a look.

thanks for the help

(Andrew Beckwith) #10

Could it be that

    maven{url ""}
dependencies {
    compile 'org.yaml:snakeyaml:1.10'

…just needs to be wrapped in:

buildscript {
      maven{url ""}
  dependencies {
      compile 'org.yaml:snakeyaml:1.10'


(Luke Daley) #11

If I understand what you are doing correctly, that’s not it.

That’s how you control dependencies for the build itself, not what you are building.