Artifact reference in maven-publish publication

I’m creating a MavenPublication in a plugin that is applied by other consumers. The plugin references a task that creates a zip file as an artifact. The task may or may not actually produce the zip file if the project doesn’t have anything in the source folders. Here’s an example:

task dbZip(type: Zip, group: 'Build', description: 'Packages up the DB scripts') {
   	classifier = 'db'
   	from 'src/main/db'
}

publishing {
	publications {
		mainJava(MavenPublication) {
			artifactId 'some-artifact'
			from components.java
			artifact dbZip
		}
	}
}

artifactoryPublish {
	publications 'mainJava'
}

The issue we’re having is that an application applying this plugin may not have anything in src/main/db, which will cause the dbZip task to be skipped. Then when the artifactoryPublish task runs it blows up with

Execution failed for task ':artifactoryPublish'.
> File '/jenkins_home/jenkins/21d10a93/workspace/commercial_poc/app 1.0 (pipeline)/app/target/distributions/app-1.0.1.78-db.zip' does not exist, and need to be published from publication mainJava

Is there a way I can tell the publish to only publish the dbZip artifact if and only if it’s output exists, otherwise skip that artifact?

Hi @e.deandrea,

It seems like a desired behavior is to fail if an artifact is missing from the publication. But if you don’t want that to happen, one workaround is putting readme or manifest file in the zip, so even when nothing is there, you still publish the zip. But that’s not ideal either: you could try using onlyIf

I got this to work, but it could probably be cleaned up:

tasks.withType(AbstractPublishToMaven) {
    onlyIf {
        publication.artifacts.matching {
            it.classifier = 'db'
        }.find().getFile().exists()
    }
}

Thanks @w25r for the pointer - I’ll give it a try. My post was really only an example - as part of our suite of plugins we’re going to have a convention in place of lots of places in the source tree where, if something is there, zip up the contents & publish along with the publication. Having to check for specific classifiers like this isn’t ideal. There will always be the pom, war, sources.jar, & javadoc.jar that will be published, then in addition whichever of the other source folders might have “stuff” in it, those will be published as well. If they don’t exist then I don’t want a “dummy” artifact to be published.

The onlyIf block you’ve posted will only run the entire publication if and only if the db.zip file exists, which is not the functionality I am looking for. I always want the publication to run, but just exclude publishing the artifacts that don’t exist. All the artifacts should have the same pom, groupId, artifactId, & version, which means they should be published from the same publication.