How to script Gradle in order to publish shadowjar into Artifactory

It's a bit late, but I'd like to show you how I got it to work. Maybe it helps someone stumbling across this question as I did:

build.gradle.kts:

plugins {
    java
    `maven-publish`
    id("com.github.johnrengelman.shadow") version "5.1.0"
}
publishing {
    publications {
        create<MavenPublication>("maven") {
            from(components["java"])
            artifact(tasks["shadowJar"])
        }
    }
    repositories {
        maven {
          /* ... */
        }
    }
}

The example above is written in Kotlin, if you prefer Groovy, you would need to write this instead:

build.gradle:

publishing {
  publications {
    shadow(MavenPublication) {
      from components.java
      artifact shadowJar
    }
  }
}

(found it here: https://libraries.io/github/johnrengelman/shadow)


The publication section determines what you're publishing using the maven-publish plugin.

In your current config, from components.java is going to publish the default jar artifact of your project and artifact sourceJar publishes the sourceJar. In order to publish a different jar, you need to modify (or add a new) publication.

shadowJar {
  baseName = 'myproject-shadow'
  classifier = ''
}

publishing {
  publications {
    shadow(MavenPublication) {
      from components.shadow
      artifactId = 'myproject-shadow'
    }
  }
}

The version used in the name of the jar comes from project.version.

Tags:

Java

Build

Gradle