Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a task in another sub project build file with parameters

I'm working on creating a multi project build file using Gradle. Many sub projects need to execute a task which exists in another sub project by passing in certain parameters. How can this be achieved in Gradle?

for example :

root project
   - project B : task X
   - project A : task Y (param m, param n)

I need projectB.taskX to call projectA.taskY(m,n)

Update:

Sub-Project A has a task of type JavaExec which needs an input parameter to the location of the properties file

task generateCode(dependsOn:['classes','build'], type: JavaExec) {

    main = 'jjrom.ObjectGen'
    classpath = sourceSets.main.runtimeClasspath

    args 'arg1', 'arg2', file(propertiesFilePath).path
}

Now, there are 10 sub projects, all of which need to call this task 'generateCode' with a parameter that contains the location to the properties file. Also, this task should be executed before building each sub-project which can be achieved using dependsOn.

My java project code organisation:

trunk/
     projA/src/java/../ObjectGen.java
     projB/src/java/../properties.xml
     projC/src/java/../properties.xml
     projD/src/java/../properties.xml
     ....
like image 327
amitbansalite Avatar asked Oct 20 '22 22:10

amitbansalite


1 Answers

A task cannot call another task. Instead, the way to solve this problem is to add a generateCode task to all ten subprojects. You can do this from the root build script with code similar to the following:

subprojects {
    apply plugin: 'java'
    configurations {
        codegen
    }
    dependencies {
        // A contains the code for the code generator
        codegen project(':A') 
    }
    task generateCode(type: JavaExec) {
        main = 'jjrom.ObjectGen'
        classpath = configurations.codegen
        args 'arg1', 'arg2'
    }
    compileJava.dependsOn(generateCode)
}

If there is no general pattern as to where the properties file is located, this information can be added in the subprojects' build scripts:

generateCode {
    args file('relative/path/to/properties/file')
}
like image 80
Peter Niederwieser Avatar answered Oct 23 '22 23:10

Peter Niederwieser