chriskelly
chriskelly

Reputation: 7736

gradle plugin dependencies not found under buildSrc

I have a working build.gradle that I'd like to refactor into the buildSrc directory but I'm having trouble finding the dependencies.

Working build.gradle:

import groovyx.net.http.HTTPBuilder

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

plugins {
  id 'groovy'
}

group "com.example"
version "0.0.1"

class Foo  {
  Foo() {
    new HTTPBuilder('http://www.example.com')
  }
}

Non-working refactored build.gradle:

However, when I try to split into the following:

build.gradle

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

plugins {
  id 'groovy'
}

group "com.example"
version "0.0.1"

and buildSrc/src/main/groovy/Foo.groovy

import groovyx.net.http.HTTPBuilder

class Foo  {
  Foo() {
    new HTTPBuilder('http://www.example.com')
  }
}

Gives the error:

C:\Project\buildSrc\src\main\groovy\Foo.groovy: 7: unable to resolve
class HTTPBuilder  @ line 5, column 26.
       HTTPBuilder client = new HTTPBuilder('http://www.example.com')

How can I get gradle to recognise the dependencies?

Upvotes: 7

Views: 5043

Answers (1)

Edumelzer
Edumelzer

Reputation: 1086

You need to create a build.gradle file for buildSrc directory. Try this:

C:\Project\buildSrc\build.gradle

apply plugin: 'groovy'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.2'
}

There is more details in this documentation section.

Upvotes: 9

Related Questions