Publishing multiple artifacts from Exec task

I have a gradle task that executes a command-line tool that creates several zip files. After generating the files, I want Gradle to publish the resulting zip files to a Maven repository. Each zip file has its own artifactId and groupId (that can be derived from the filename of the zip file). The number and names of the zip files are not known beforehand and may be different on each run.

I’m not a Gradle expert but after studying the documentation I think I should declare the zip files as publications for the maven-publish plugin. I know how to do this with static files and tasks that generate a single archive file. I could not find examples on how to do this with multiple archives from a single task, as in my case.

Let’s say my build.gradle looks like this:

apply plugin: 'base'
apply plugin: 'maven-publish'

task init << {

task makeZipfiles(type: Exec, dependsOn: 'init') {
  workingDir buildDir
  commandLine 'touch', '', '' 
  // actual result files will be different on each run

publishing {
  publications {
    // ??? Publication of all files from task makeZipfiles, 
    // each with its own groupId and artifactId

I have been able to create publications by iterating over the files in the build directory, but that only works if I first run the makeZipfiles task and then run the publish task. What I want is to make the publish tasks depend on the makeZipfiles task, using the output files of the makeZipfiles task for publication.

What is a correct way of defining tasks, artifacts and/or publications to achieve the desired result?

I’ve worked through a similar set of issues recently.

We had a JavaExec task that generated zip files in a particular format and then tried to publish them using the maven-publish plugin. It was prone to error because there was no easy way to connect the publication of the artifact to a file that already exists. You’d have to parse the filename yourself, etc…

Your best way forward is to use a task of type Zip to create each individual zip file. If you need custom handling, consider extending the Zip task. If you need multiple zips, there are ways of auto-generating the tasks during configuration, by either using a pattern or a collection as a starting point.

This will make it VERY easy to connect through to the publications because the maven-publish plugin will just allow you to reference the task by name (as long as the type is a subclass of AbstractArchiveTask). It will also respect any classifier, group, and version you specifiy within your zip task.