如何从Gradle中的buildscript依赖项导入类

时间:2017-02-08 05:55:30

标签: java gradle apache-commons

我想在Gradle Script中访问StringUtils方法。因此我在buildscript闭包中定义了它的MavenRepo。在我添加了import语句后,我以为我能够使用这个类。但错误告诉我不然。我错过了什么?

gradle.build

import org.apache.commons.lang.StringUtils

apply plugin: "java"
apply plugin: "application"

mainClassName = "com.xetra11.test.Main"

task execute(){
     File file = new File("/log/file.txt")
     StringUtils.touch("/log/file.txt")
}

buildscript {
    repositories{
        mavenCentral()
    }
    dependencies {
        classpath group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
    }
}

repositories{
    mavenCentral()
 }

dependencies{
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
}

错误输出

~/Workspace/Development/Java/gradle_test/ gradle buildDependents
gradle buildDependents

FAILURE: Build failed with an exception.

* Where:
Build file '/home/xetra11/Workspace/Development/Java/gradle_test/build.gradle' line: 1

* What went wrong:
Could not compile build file '/home/xetra11/Workspace/Development/Java/gradle_test/build.gradle'.
> startup failed:
  build file '/home/xetra11/Workspace/Development/Java/gradle_test/build.gradle': 1: unable to resolve class org.apache.commons.lang.StringUtils
   @ line 1, column 1.
     import org.apache.commons.lang.StringUtils
     ^

  1 error


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

1 个答案:

答案 0 :(得分:2)

org.apache.commons.lang.StringUtils,从它的包中暗示,是在公共场所2.x. commons-lang3的软件包名称已更改为org.apache.commons.lang3

因此,要么使用org.apache.commons:commons-lang:2.6作为依赖关系,要么将导入更改为org.apache.commons.lang3.StringUtils