buildSrc

时间:2018-01-24 16:44:26

标签: gradle groovy

我有一个工作build.gradle,我想重构到buildSrc目录,但我找不到依赖项。

使用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')
  }
}

非工作重构的build.gradle:

但是,当我尝试分成以下内容时:

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"

buildSrc/src/main/groovy/Foo.groovy

import groovyx.net.http.HTTPBuilder

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

给出错误:

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')

如何让gradle识别依赖项?

1 个答案:

答案 0 :(得分:3)

您需要为build.gradle目录创建buildSrc文件。试试这个:

C:\Project\buildSrc\build.gradle

apply plugin: 'groovy'

repositories {
    mavenCentral()
    jcenter()
}

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

this文档部分中有更多详细信息。