Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametrize gradle task

Tags:

gradle

Is it possible to parametrize task? For example I want to create one zip task which will be used by two other tasks but those tasks needs to pass some additional information info this zip task (ex. zip name).

This is how I want it to work. If it is not possible, how can I do it? Or if I can is there some better solution?

task zipFile(type: Zip) {
    from files('dist/zip')
    destinationDir = file('dist')
    archiveName = myArchiveName // myArchiveName should be passed as property
}

task zipTwoFiles << {
    // create first archive
    zipFile.execute() // how to pass property to this task?
    // create second archive
    zipFile.execute() // how to pass property to this task?
}

I want to do it because this is my first step in migrating project from ant to gradle. In ant it looks like this:

<target name="zipFile">
    <zip destfile="dist/${myArchiveName}" basedir="dist/zip" />
</target>

<target name="zipFile1">
    <antcall target="zipFile">
        <param name="myArchiveName" value="myarchive.zip" />
    </antcall>
    <antcall target="zipFile">
        <param name="myArchiveName" value="myarchive2.zip" />
    </antcall>
</target>
like image 297
pepuch Avatar asked Oct 18 '25 15:10

pepuch


1 Answers

Explicitly calling a task is not supported by Gradle. Instead, you should declare two Zip tasks. If necessary, you can declare another task that has no behavior on its own but depends on the two Zip tasks. Such a task is called lifecycle task.

like image 149
Peter Niederwieser Avatar answered Oct 22 '25 09:10

Peter Niederwieser