Including a groovy script in another groovy

前端 未结 12 1469
走了就别回头了
走了就别回头了 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:27

    The way that I do this is with GroovyShell.

    GroovyShell shell = new GroovyShell()
    def Util = shell.parse(new File('Util.groovy'))
    def data = Util.fetchData()
    
    0 讨论(0)
  • 2020-11-27 12:29

    As of Groovy 2.2 it is possible to declare a base script class with the new @BaseScript AST transform annotation.

    Example:

    file MainScript.groovy:

    abstract class MainScript extends Script {
        def meaningOfLife = 42
    }
    

    file test.groovy:

    import groovy.transform.BaseScript
    @BaseScript MainScript mainScript
    
    println "$meaningOfLife" //works as expected
    
    0 讨论(0)
  • 2020-11-27 12:31

    Groovy doesn't have an import keyword like typical scripting languages that will do a literal include of another file's contents (alluded to here: Does groovy provide an include mechanism?).
    Because of its object/class oriented nature, you have to "play games" to make things like this work. One possibility is to make all your utility functions static (since you said they don't use objects) and then perform a static import in the context of your executing shell. Then you can call these methods like "global functions".
    Another possibility would be using a Binding object (http://groovy.codehaus.org/api/groovy/lang/Binding.html) while creating your Shell and binding all the functions you want to the methods (the downside here would be having to enumerate all methods in the binding but you could perhaps use reflection). Yet another solution would be to override methodMissing(...) in the delegate object assigned to your shell which allows you to basically do dynamic dispatch using a map or whatever method you'd like.

    Several of these methods are demonstrated here: http://www.nextinstruction.com/blog/2012/01/08/creating-dsls-with-groovy/. Let me know if you want to see an example of a particular technique.

    0 讨论(0)
  • 2020-11-27 12:34
    evaluate(new File("../tools/Tools.groovy"))
    

    Put that at the top of your script. That will bring in the contents of a groovy file (just replace the file name between the double quotes with your groovy script).

    I do this with a class surprisingly called "Tools.groovy".

    0 讨论(0)
  • 2020-11-27 12:35

    Here's a complete example of including one script within another.
    Just run the Testmain.groovy file
    Explanatory comments included because I'm nice like that ;]

    Testutils.groovy

    // This is the 'include file'
    // Testmain.groovy will load it as an implicit class
    // Each method in here will become a method on the implicit class
    
    def myUtilityMethod(String msg) {
        println "myUtilityMethod running with: ${msg}"
    }
    

    Testmain.groovy

    // Run this file
    
    // evaluate implicitly creates a class based on the filename specified
    evaluate(new File("./Testutils.groovy"))
    // Safer to use 'def' here as Groovy seems fussy about whether the filename (and therefore implicit class name) has a capital first letter
    def tu = new Testutils()
    tu.myUtilityMethod("hello world")
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题