I'm trying to run a Groovy class from within my build.gradle file. I'm following the direction in the use guide however I get an error.
The build file is:
apply plugin: 'java'
apply plugin: 'groovy'
main {
java {
srcDirs = ["$projectDir/src/java"]
}
groovy {
srcDirs = ["$projectDir/src/groovy"]
}
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.2.0', files(....)
}
task fooTask << {
groovyClass groovyClass = new groovyClass()
groovyClass.foo()
}
The groovy class is very simple:
public class groovyClass {
public void foo() {
println 'foo'
}
}
However when I try to run gradlew compile fooTask I get the following error:
unable to resolve class groovyClass
Any idea why?
Thanks
You need to add the class to buildSrc
if you want to reference it from the build (and not in a simple Exec task). Given this directory structure:
|-buildSrc
| |- src
| |- main
| |- groovy
| |- GroovyClass.groovy
|- build.gradle
Where GroovyClass.groovy
is:
class GroovyClass {
void foo() {
println 'foo'
}
}
And build.gradle
is:
apply plugin: 'groovy'
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.2.1'
}
task fooTask << {
GroovyClass g = new GroovyClass()
g.foo()
}
Then, running gradle fooTask
gives the output:
$ gradle fooTask
:buildSrc:compileJava UP-TO-DATE
:buildSrc:compileGroovy UP-TO-DATE
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes UP-TO-DATE
:buildSrc:jar UP-TO-DATE
:buildSrc:assemble UP-TO-DATE
:buildSrc:compileTestJava UP-TO-DATE
:buildSrc:compileTestGroovy UP-TO-DATE
:buildSrc:processTestResources UP-TO-DATE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test UP-TO-DATE
:buildSrc:check UP-TO-DATE
:buildSrc:build UP-TO-DATE
:fooTask
foo
BUILD SUCCESSFUL
Total time: 4.604 secs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With