OK, here is a way to do it:

In your build.gradle do:


    def configFileName = ...
    new GroovyShell(new Binding(project:project)).evaluate(new
File(configFileName))


Your config file would look like this:


    ...
    // add static project properties, e.g.
    project.myProperty = ...
    
    // set up project and tasks, e.g.
    project.dependencies {
        // add resolvers...
        ...
    }
    project.createTask(...)
    ...

    // configurations that are dependent on the task graph, e.g.
    project.init.doFirst { task, dag ->
        // configure compile tasks
        project.getTasks().each { myTaskName, myTask ->
            if (myTask instanceof Compile) {
                ((Compile) myTask).configure {
                    // this should be set by default, but is not (bug?)
                   
conventionMapping(DefaultConventionsToPropertiesMapping.COMPILE)
                }
            }
        }
        ...
    }


One special case are helper functions, you want to define.

You can add them as closure properties to the project, e.g. in your config
file:



    project.helperFct = this.&helperFct

    String helperFct(String param1, int param2 = 3) {
        ...
    }


In your build.gradle you have to put the closure into a local variable:



    // the following does not work
    //     project.helperFct(...)
    // nor does this:
    //     helperFct(...)
    // this would work, but it's not nice
    //     project.helperFct.call(...)
    // so copy the closure to a local variable
    def helperFct = project.helperFct // or even: def helperFct = helperFct
    // now you can call it like a function
    helperFct('a', 1)
    helperFct('b')


Maybe we could add a method to the Project:


    void include(String filename) {
        new GroovyShell(new Binding(project:project)).evaluate(new
File(filename))
    }


-- 
View this message in context: 
http://www.nabble.com/Common-functions-in-external-file--tp20131097p20146231.html
Sent from the gradle-user mailing list archive at Nabble.com.

Reply via email to