Synchronization of @InputFile in Multi-module projects

plugins

(Siva Jeganathan) #1

Hi, I am running multiple projects with my custom plugin using parallel mode.

In one of my task, I download a file to central folder (@InputFile) and execute it in TaskAction.

Now, If I am executing that task in multiple projects, all the project execution are entering simultaneously inside InputFile and trying to download the file.

Is there any way I can synchronize this ?


(Chris Doré) #2

Instead of trying to synchronize multiple tasks, could you move the file download logic into a single task, which the other tasks depend on?

For example, create a download task on the root project and have all other projects reference it:

class MyDownloadTask extends DefaultTask {
    @Input
    String url
    @OutputFile
    File destFile
    @TaskAction
    void doDownload() {
        // download url as destFile
        println "Downloading ${url} as ${destFile}"
        destFile.text = 'hello world'
    }
}
class MyFileUserTask extends DefaultTask {
    def download
    @InputFiles
    def getDownloadedFile() {
        project.files( download )
    }
    @TaskAction
    void useFiles() {
        println downloadedFile.join( '\n' )
    }
}
class MyPlugin implements Plugin<Project> {
    @Override
    void apply( Project project ) {
        def downloadTask = project.rootProject.tasks.findByName( 'myDownloader' )
        if( downloadTask == null ) {
            downloadTask = project.rootProject.tasks.create( 'myDownloader', MyDownloadTask ) {
                url = 'http://some.file'
                destFile = new File( project.rootProject.buildDir, 'downloadedFile' )
            }
        }
        project.tasks.create( 'useDownloadedFile', MyFileUserTask ) {
            download = downloadTask  // creates an implicit task dependency, via project.files in getDownloadedFile
        }
    }
}

Example output with two subprojects a and b which apply the plugin:

./gradlew uDF
:myDownloader
Downloading http://some.file as /home/cdore/projects/gradle-forum/build/downloadedFile
:a:useDownloadedFile
/home/cdore/projects/gradle-forum/build/downloadedFile
:b:useDownloadedFile
/home/cdore/projects/gradle-forum/build/downloadedFile

(Siva Jeganathan) #3

Thanks @Chris_Dore, that was a good idea. But I don’t want to expose the Download part / task to external user (gradle tasks --all). I wanted to do it in background. Thats the big challenge :slight_smile: