Gradle: How do I make one task depend on output files of another task across projects?

1.4k views Asked by At

I have two tasks which both call the Exec closure (task1 and task2). task1 exec call generates output files for task2 to take in as command line parameters of an input. task1 and task2 will be part of the same gradle file that is used in a multiproject build. I include the same gradle file in multiple project using "apply from:" I HAD working code using external properites to pass these files, but I have since broke this and I feel like there has to be a better way to manage this anyway. I imagine there is a better way using dependencies, and artifacts, but I'm having a hard time wrapping my brain around use of them outside of Java builds. The goal of this is to reuse this in multiple projects of this type and have it manage file dependencies among them appropriately. I could have n number of projects outputs task1 across projects that may be needed as input to task2 in another project. Including code for reference, but I feel like what I have is fundamentally flawed and convoluted.

ext.task1generatedFile = file()
ext.task2InputsFromTask1 = files() //This is set up in individual subprojects.

subprojects{
    afterEvaluate {     Project project ->  

        ext.task1generatedFile = file("$project.buildDir/$project.name" + ".projlib")   

        task task1 {
            doLast{

                println "Building task1 output for $project.name"

                //task1generatedFile is created by myApp.exe
                exec {
                    workingDir "$appHome/bin"
                    commandLine "${appHome}/bin/myApp.exe", 
                    '-n', task1generatedFile.toString()                                
                }
            }               
        }                   
        task1.outputs.files task1generatedFile             

        task task2  {                         

            doLast {        
                println "Building Project task2 for $project.name from task1 output file(s) across multiple projects"

                //Handle optional parameters that are output from any number of task1 outputs from other projects.  This list can be empty depending on the project.
                String projLibCommas = "";              
                ArrayList<String> optionalArgs = new ArrayList<String>();

                //Need to constructed comma separated list for task1 outputs generated in other projects
                if (!task2InputsFromTask1.isEmpty()) {
                    optionalArgs.add("-pl");
                    projLibCommas = "\"" + task2InputsFromTask1[0];

                    for (int i =1; i < task2InputsFromTask1.getFiles().size(); i++ ) {
                        projLibCommas += "," + task2InputsFromTask1[i];
                    }   
                    projLibCommas += "\"";
                    optionalArgs.add(projLibCommas);
                }               

                exec {
                    workingDir "$appHome/bin"
                    commandLine "myApp2.exe",
                    args optionalArgs
                }       
            }       
        }       
        task2.inputs.files task1generatedFile       
    }
}

//In some subprojects that require generated files from other projects I do this.

task2.dependsOn ':ParentProj:AnotherProject:task1'
task2 {
    Project anotherProject = project(':ParentProj:AnotherProject')      
    ext.task2InputsFromTask1 = files(anotherProject.task1generatedFile);
}
0

There are 0 answers