Search code examples
groovygradlebuild-script

How to use buildscript dependencies in custom classes?


I am using a couple of helper classes with my build script, the folder structure looks like

buildSrc    
  -src   
     -main    
       -groovy
           GitUtils.groovy
build.gradle

In GitUtils I am trying to import a couple of classes (from grgit & http-builder in this example) used in a custom library. But it does not work, I get "Unable to resolve class XXX" exceptions. These classes are resolved fine if they are in build.gradle.

Relevant part of build.gradle:

buildscript {    
  repositories {
    jcenter()
    mavenCentral()
  }
  dependencies {
    classpath 'org.ajoberstar:grgit:1.3.0'
    classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
  }
}

dependencies {
  compile 'org.ajoberstar:grgit:1.3.0'
  compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}

...
GitUtils.doStuff();

Solution

  • The clean solution is to declare the dependencies you have in GUtils in your buildSrc/build.gradle file:

    apply plugin:'groovy'
    
    repositories {
      jcenter()
      mavenCentral()
    }
    dependencies {
      compile gradleApi()
      compile 'org.ajoberstar:grgit:1.3.0'
      compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
    }