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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With