Jar task publish artifacts also if disabled

If the jar task is disabled with:

jar.enabled = false

then also artifacts are registered which result in an error on uploadArchives because the file does not exists. A workaround is to remove the artifacts with the follow code. But this is very urgly:

preparePublish {
    doFirst {
        if( !jar.enabled ) {
            configurations.archives.artifacts.removeAll{ it.name == jar.baseName && it.extension == 'jar' && it.classifier == jar.classifier }

Unfortunately applying the ‘java’ plugin automatically adds the jar task as an artifact. Simply disabling the task will (as you discovered) result in an error because we will attempt to publish an artifact that doesn’t exist. Might I suggest using the ‘maven-publish’ plugin instead? That way you can explicitly configure your publications.

We already use such plugin for publishing. But this does not solve the problem. The central publishing task iterate over the artifacts to configure the publishing. Also a check with File.exists does not help. Because a missing artifact is of course an error condition.

Which task exactly is failing?

It is a self written task. Which then use ivy.