Including a groovy script in another groovy

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

    I think that the best choice is to organize utility things in form of groovy classes, add them to classpath and let main script refer to them via import keyword.

    Example:

    scripts/DbUtils.groovy

    class DbUtils{
        def save(something){...}
    }
    

    scripts/script1.groovy:

    import DbUtils
    def dbUtils = new DbUtils()
    def something = 'foobar'
    dbUtils.save(something)
    

    running script:

    cd scripts
    groovy -cp . script1.groovy
    

提交回复
热议问题