Including a groovy script in another groovy

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

    Groovy can import other groovy classes exactly like Java does. Just be sure the extension of the library file is .groovy.

        $ cat lib/Lib.groovy
        package lib
        class Lib {
           static saySomething() { println 'something' }
           def sum(a,b) { a+b }
        }
    
        $ cat app.gvy
        import lib.Lib
        Lib.saySomething();
        println new Lib().sum(37,5)
    
        $ groovy app
        something
        42
    

提交回复
热议问题