Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish multiple artifacts from single task

Tags:

gradle

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 << {
  buildDir.mkdirs()
}

task makeZipfiles(type: Exec, dependsOn: 'init') {
  workingDir buildDir
  commandLine 'touch', 'test1.zip', 'test2.zip' 
  // 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?

like image 890
Pieter Kuijpers Avatar asked Apr 16 '15 18:04

Pieter Kuijpers


1 Answers

You should use the maven-publish plugin. Add the following to where you apply your plugins in your build.gradle file:

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

Then, somewhere down below add the following (or something like it reflecting your desired artifacts):

task sourcesJar(type: Jar) {
    description = 'Creates sources JAR.'
    classifier = 'sources'

    from project.sourceSets.main.allSource
}

artifacts {
    archives jar
    archives sourcesJar
}

publishing {
    publications {
        mavenJava(MavenPublication){
            artifact jar
            artifact sourcesJar
        }
    }
}

This will produce the binary jar and a jar of the sources. You can follow a similar pattern to produce other desired jars.

For your specific example I would suggest adding archives makeZipfiles to the artifacts block and similarly in the publications below.

like image 171
cjstehno Avatar answered Nov 15 '22 08:11

cjstehno