简体   繁体   中英

how to run a groovy method from gradle task?

I am using

Gradle version 2.14

Groovy version 2.4.4

JVM: 1.8.0_121

I want to run a specific groovy method from a groovy class. How do I make associations with a groovy class from a gradle task ?

my task looks somewhat like this

task someTask << { 
  // Do something
  // Call method, which returns a java.io.File
  // Do something else
}

and my groovy class/method

File getSomeFile(String parameter) {
  // Do something
  // return an instance of java.io.File or maybe null, depending
}

So how do I call the method which takes a parameter and returns java.io.File ?

  • (hope this is not a dublicate, i looked around, not finding exactly what I need)
class Foo {
        void bar() { println 'bar'; }
    }
task someTask {
    doLast {
        new Foo().bar(); 
        }
    }

Gradle scripts ARE Groovy scripts, just do it as in any other Groovy script. Just make sure your class is in the classpath, eg by depending on the library that includes the file in builscript { dependencies {} } , or by stuffing the file into the buildSrc project of your Gradle project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM