Search code examples
validationgradleyamlsnakeyaml

Error when building the project "unable to resolve class org.yaml.snakeyaml.Yaml"


We are trying to validate an yaml at the time of build in a gradle project. After adding the dependency "org.yaml.snakeyaml:2.2", and trying to use that on a task, I'm getting the error unable to resolve class org.yaml.snakeyaml.Yaml . Its throwing the even when I try to do gradlew clean.

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.yaml:snakeyaml:2.0'
}

task validateYaml {
    doLast {

        def yaml = new org.yaml.snakeyaml.Yaml()
        def data = yaml.load(PATH_OF_YAML)
    }
}

I tried with the older versions of snakeyaml. Its the same issue with any versions. Kindly let me know if there is anything I'm missing on this.


Solution

  • When you write:

    dependencies {
        implementation 'org.yaml:snakeyaml:2.0'
    }
    

    you are making SnakeYAML a dependency of your application, not your build.

    To add the dependency to the build, you need to use the buildscript block, like so:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.yaml:snakeyaml:2.0'
        }
    }