Publishing artifacs that are first available at exec phase

Hi.
I need to publish a zip file that is created during execution of a build script.
This build script creates an output directory with a time stamp and puts the zip file there.
I tried different things, but the best thing I can achieve is publishing the zip of the previous run.
The problem seems that all input and output configuration is done in the config phase. At this phase the new output directory is not created yet.

What I have so far:

task deleteOut(type: Delete) {
	delete('out')
}

task buildOut(type: Copy) {
	dependsOn deleteOut
	outputs.dir('out/')
	def now = new Date()
	def filename = now.format("yyyyMMddHHmmss")
	from ('original')
	into ('out/'+filename)
}
	
task buildFeature(type: Zip) {
	from buildOut
}

task buildTemplate(type: Zip) {
	classifier = 'template'
    from(zipTree(buildFeature.outputs.files.singleFile).matching { include '**/*_install.zip'}.collect{zipTree(it)})
}

publishing {
        publications {
            maven(MavenPublication) {
                artifact source: buildFeature
                artifact source: buildTemplate
            }
        }
    }

This script should delete the out folder, create it new with a subfolder with the timestamp in the name. Then the zip file should be copied into this subfolder (that’s a mock of the actual build). I don’t know the exact name of the zip, only the post part.
Now one task should create a zip file of the out folder (that’s working) and one task for somehow publishing the zip file included in the out folder. This is my last try to use the output of the output folder zip task and unzip the content of the file and zip it again to get it into a publication.

Is there any way to accomplish this. Again my goal: Publish the whole output folder and publish only the special zip file included in this folder.

Your instinct that something is happening too early is on the right track.

When from is called during the configuration phase, zipTree(.... needs to be called first. This is going to try and unzip the buildFeature output file. If that filename isn’t changing between builds then that zip will contain the previous build’s contents. In order to defer this to a later time, pass the from method a closure that returns what you wish to include.

from {
    zipTree(buildFeature.outputs.files.singleFile).matching { include '**/*_install.zip'}.collect{zipTree(it)}
}

Secondly, with this arrangement there is no way for Gradle to infer that buildTemplate depends on buildFeature. So you should add a dependsOn buildFeature to buildTemplate.

Other idea; Since buildFeature is zipping the output of buildOut and then buildTemplate is unzipping the output of buildFeature, could buildTemplate instead be based on buildOut and save some time?

When using

from {
    	buildOut.outputs.files.asFileTree.filter {file->file.name.endsWith('_install.zip')}.collect{zipTree(it)}
    }

it also works using buildOut. Thanks a lot.