在Gradle构建脚本中使用Java类 [英] Use Java class in Gradle build script

查看:109
本文介绍了在Gradle构建脚本中使用Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gradle构建脚本,它必须在Task中实例化一个Java类,并在创建的对象上调用一个方法。目前,我有以下几种:

  apply plugin:'java'

dependencies {
编译文件(libs / some.library.jar)
}

任务A<< {

def obj = new some.library.TestClass()
obj.doSomething()

}

问题是找不到类 some.library.TestClass()。我阅读了关于如何在Gradle中使用Groovy类的这篇文章,但我需要我的Java类来自外部JAR文件。我如何将jar添加到构建源?看来依赖项块没有做我期望的功能。任何人都可以给我一个正确的方向提示吗?

解决方案

依赖关系编译文件(libs / some.library.jar)被添加为项目依赖项,而不是脚本依赖项本身。你需要做的是在脚本的 classpath 作用域中添加这个依赖项。

  apply plugin:'java'

buildscript {
dependencies {
classpath files(libs / some.library.jar)
}
}

任务A<< {
def obj = new some.library.TestClass()
obj.doSomething()
}

现在它可以工作了。


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

}

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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆