How do I manage task generated files as inputs into other tasks across sibling projects?

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 HAD working code using external properites to pass these files, but doing it through the way I’m doing is difficult to manage so I feel there has to be a better way… Perhaps 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 between task1 and task2 tasks. 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);
}

I think you’ve copy+pasted the same thing inside itself?

Does task2 depend on the contents of the files generated from task1 or just the list of files generated by task1?

You can usually do something like this:

task task1(type: Exec) {
   outputs.files fileTree("generatedFiles/")
   // configure the exec task
}
  task task2() {
   inputs.files task1
   doLast() {
      exec {
         // configure the exec task
               args inputs.files.asPath // may have to do some mangling here to get it in the format you need
      }
   }
}

Take a look at 15.9.1. Declaring a task’s inputs and outputs in http://www.gradle.org/docs/current/userguide/more_about_tasks.html

My apologies… Included the tasks below how I have them but I think you may have answered my question. And embarrassingly simple compared to how I was going about it. I will try this and see if it works for me. Does task1 have to be declared as type Exec for this to work? Also I’ll need this from multiple project task 1 outputs.

Can I do this:

task task2() {
   inputs.files rootProj:AnotherProjectA:task1
   inputs.files rootProj:AnotherProjectB:task1
     doLast() {
       // Loop through files here to get multiple project task 1 outputs.
      exec {
         // configure the exec task
               args inputs.files.asPath // may have to do some mangling here to get it in the format you need
      }
   }
}

My original post:

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>();
                  //...Loop through inputs.files to build optionalArgs
                                              exec {
                    workingDir "$appHome/bin"
                    commandLine "myApp2.exe",
                    args optionalArgs
                }
                   }
               }
               task2.inputs.files task1generatedFile
           }
}

Yes, but you’d need to use tasks.getByPath() http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/TaskContainer.html#getByPath(java.lang.String) to reference tasks in another project.

edit: And no, this works for all tasks that have inputs/outputs.

This worked for me thanks! This is what my project looks like now:

:RootProject

subprojects {
 task task1(type: Exec) {
    outputs.files fileTree("generatedFiles/")
    // configure the exec task
 }
 task task2() {
       doLast() {
                    //Supply exec with inputs if they exist.
    exec {
    // configure the exec task
          args inputs.files.asPath // may have to do some mangling here to get it in the format you need
    }
    }
 }
}

:RootProject:ProjectA

//In ProjectA I set up a dependency on the output files of ProjectB
 task2
{
   inputs.files tasks.getByPath(":RootProject:ProjectB:task1")
  }