在 Gradle 构建脚本中使用 Java 类 [英] Use Java class in Gradle build script
问题描述
我有一个 Gradle 构建脚本,它必须在 Task 中实例化一个 Java 类并在创建的对象上调用一个方法.目前,我有以下几点:
I have a Gradle build script which has to instantiate a Java class in a Task and call a method on the created object. Currently, I have the following:
apply plugin: 'java'
dependencies {
compile files("libs/some.library.jar")
}
task A << {
def obj = new some.library.TestClass()
obj.doSomething()
}
问题是没有找到 some.library.TestClass()
类.我阅读了这篇文章,了解如何在 Gradle 中使用 Groovy 类,但我需要我的 Java 类来自外部 JAR 文件.如何将 jar 添加到构建源?似乎 dependencies
块没有做我期望它做的事情.谁能给我一个正确方向的提示?
The problem is that the class some.library.TestClass()
is not found. I read this article about how to use Groovy classes in Gradle, but I need my Java class to come from an external JAR file. How can I add a jar to the build source? It seems that the dependencies
block doesnt do what I expect it to do. Can anyone give me a hint in the right direction?
推荐答案
依赖 compile files("libs/some.library.jar")
添加为项目依赖而不是脚本依赖本身.您需要做的是在脚本的 classpath
范围内添加此依赖项.
The dependency compile files("libs/some.library.jar")
is added as a project dependency not as the script dependency itself. What You need to do is to add this dependency in script's classpath
scope.
apply plugin: 'java'
buildscript {
dependencies {
classpath files("libs/some.library.jar")
}
}
task A << {
def obj = new some.library.TestClass()
obj.doSomething()
}
现在它应该可以工作了.
Now it should work.
这篇关于在 Gradle 构建脚本中使用 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!