Use Java class in Gradle build script
I have a Gradle build script that needs to instantiate a Java class in a Task and call a method on the created object. I currently 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 was some.library.TestClass()
not found. I read this article on how to use Groovy classes in Gradle, but I need my Java class from an external JAR file. How do I add a jar to an assembly source? The block dependencies
doesn't seem to do what I expect it to do. Can anyone give me a hint in the right direction?
source to share
The dependency is compile files("libs/some.library.jar")
added as a project dependency not as the w370 dependency itself. What you need to do is add this dependency to the script scope classpath
.
apply plugin: 'java'
buildscript {
dependencies {
classpath files("libs/some.library.jar")
}
}
task A << {
def obj = new some.library.TestClass()
obj.doSomething()
}
It should now work.
source to share