Including a groovy script in another groovy

前端 未结 12 1514
走了就别回头了
走了就别回头了 2020-11-27 12:07

I have read how to simply import a groovy file in another groovy script

I want to define common functions in one groovy file and call those functions from other groo

12条回答
  •  情歌与酒
    2020-11-27 12:36

    Another way to do this is to define the functions in a groovy class and parse and add the file to the classpath at runtime:

    File sourceFile = new File("path_to_file.groovy");
    Class groovyClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile);
    GroovyObject myObject = (GroovyObject) groovyClass.newInstance();
    

提交回复
热议问题