I am writing a releasing plugin for my projects. One of the tasks I create should pack the output of several projects “distZip” outputs into a single zip file (and does several other activities, such as adding static files, calculating version, generates release notes, ets).
My plugin (tailored for the example) looks like this:
open class SemanticVersionPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.create("release", ReleaseTask::class.java) {
group = "releasing"
}
}
}
My task is:
open class ReleaseTask : Zip() {
// This fuction copies the content of the distZip output into this task
private fun addProjectResultToRelease(zip: Zip, projectPath: String) {
logger.quiet(" Adding $projectPath")
with(zip) {
val distTaskName = "$projectPath:distZip"
val task = project.tasks.findByPath(distTaskName) ?: throw GradleException("Task not found: $distTaskName")
task.outputs.files.filter { it.extension == "zip" }.forEach {
val srcZip = project.zipTree(it)
//includeEmptyDirs = false
from(srcZip) {
eachFile {
this.path = (this.path.replaceBefore(delimiter = "/", replacement = ""))
}
}
}
}
}
// Some default value overrides
init {
outputs.upToDateWhen { false }
destinationDirectory.file("_release")
archiveBaseName.set(project.name)
}
@Suppress("unused")
@TaskAction
fun action() {
val ext = project.extensions["semanticVersion"] as SemanticVersionExtension
println(dependsOn.joinToString { it.toString() })
// Iterating through dependencies
dependsOn.forEach {
it as String
println("DEP: $it")
addProjectResultToRelease(this, it.substringBeforeLast(":"))
}
from(project.file("version.txt"))
super.copy()
}
}
My intention, that in the build script, one simply add dependencies to all the other projects of which the output should add to:
tasks.withType<ReleaseTask> {
dependsOn(":core:distZip")
// Other dependencies
}
My first doubt is whether the init
block the right place to set up or override defaults for the task?
My other problem, that when I run this task, I got “NO-SOURCE” message:
> ...
> Task :core:distZip UP-TO-DATE
> Task :release NO-SOURCE
How can I specify that the outputs of the corresponding distZips are the inputs of this task?
Is there a hook to set up at the end of the task configuration? I tried to intercept the dependsOn of the task:
override fun dependsOn(vararg paths: Any?): Task {
super.dependsOn(*paths)
println("DO>" + paths.joinToString { it.toString() })
paths.forEach {
val t = project.tasks.getByPath(it.toString())
println(t)
inputs.files(t.outputs)
}
return this
}
This feels incorrect, and even more, this approach didn’t solve the problem of no source. (I tried to override the configure function as well, but it is not called at all.)
I stucked here. (The addProjectResultToRelease
function worked well when I used a simple buildSrc
class.)
So my questions are:
(1) Is the init block the right place to set up (override) default values?
(2) How can I tell to gradle, that it has sources?
Thanks for the help!