如何从 Gradle 中的构建脚本依赖项导入 class
How to import a class from a buildscript dependency in Gradle
我想在我的 Gradle 脚本中访问 StringUtils 方法。因此我在 buildscript 闭包中定义了它的 MavenRepo。添加导入语句后,我认为我可以使用这个 class。但是错误告诉我并非如此。我想念什么?
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
org.apache.commons.lang.StringUtils
,从它的包装来看,在 commons-lang 2.x 中。 commons-lang3
的软件包名称已更改为 org.apache.commons.lang3
。
因此,要么使用 org.apache.commons:commons-lang:2.6
作为依赖项,要么将导入更改为 org.apache.commons.lang3.StringUtils
我想在我的 Gradle 脚本中访问 StringUtils 方法。因此我在 buildscript 闭包中定义了它的 MavenRepo。添加导入语句后,我认为我可以使用这个 class。但是错误告诉我并非如此。我想念什么?
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
org.apache.commons.lang.StringUtils
,从它的包装来看,在 commons-lang 2.x 中。 commons-lang3
的软件包名称已更改为 org.apache.commons.lang3
。
因此,要么使用 org.apache.commons:commons-lang:2.6
作为依赖项,要么将导入更改为 org.apache.commons.lang3.StringUtils