BootJar + MavenJar. Artifact wasn't produced by this build

As stated by gradle documentation here:

Starting from Gradle 6.2, Gradle performs a sanity check before uploading, to make sure you don’t upload stale files (files produced by another build). This introduces a problem with Spring Boot applications which are uploaded using the components.java component

More explanation is available in the link above. They propose the following workaround that I personally tried and worked for me :

configure the outgoing configurations

configurations {
       [apiElements, runtimeElements].each {
           it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
           it.outgoing.artifact(bootJar)
       }
    }

here after the configuration from my build.gradle:

....
apply plugin: 'maven-publish'
...

configurations {
    [apiElements, runtimeElements].each {
        it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
        it.outgoing.artifact(bootJar)
    }
    ....
}


publishing {
    publications {
        myPublication(MavenPublication) {
            groupId groupId
            artifactId artifactId
            version version
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }
    }
    repositories {
        maven {
            url azureRepoUrl
            name azureRepoName
            credentials {
                username azureRepoUserName
                password azureRepoAccessToken
            }
        }
    }
}

Excerpt from

Starting from Gradle 6.2, the main jar task is disabled by the Spring Boot application, and the component expects it to be present. Because the bootJar task uses the same file as the main jar task by default, previous releases of Gradle would either:

  • publish a stale bootJar artifact
  • or fail if the bootJar task hasn’t been called previously

To simple workaround would be configuring the outgoing configurations. For multi-module Gradle project, place the below configuration in the service module(spring boot module).


dependencies {
          .....
}

configurations {
    [apiElements, runtimeElements].each {
        it.outgoing.artifacts.removeIf {
            it.buildDependencies.getDependencies(null).contains(jar)
        }
        it.outgoing.artifact(bootJar)
    }
}

Note: There is no need for changing anything with artifactory task if it was configured correctly. This working solution has been tested with Gradle 6.4.1.

Don't try the alternate suggestion that they provided, because classifier attribute is deprecated in recent versions, also altering the bootJar task with custom configuration would result in improper uber jar construction, and if you extract the generated jar distributive, you could find the missing BOOT-INF directory and necessary META-INF/MANIFEST.MF values.

jar {
   enabled = true
}

bootJar {
   classifier = 'application'
}


I could get this worked by just adding artifact bootJar in the publishing task as shown below and with out adding any configurations as suggested in the gradle documentation. I believe this could be working same as their first workaround in the documentation. Tested with gradle 6.5.1

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact bootJar
            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

project.tasks.publish.dependsOn bootJar