DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replace your Scripts with Gradle Tasks
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • The Complete Gradle Plugin Tutorial
  • Zero Trust for Agents: A New Lens to Secure Agents

Trending

  • Choosing a BBD Framework
  • PWA on iOS, Part 4: What Add to Home Screen Really Does
  • From Legacy ETL to Modern ELT: Engineering a Governed dbt and Databricks Architecture
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Gradle: Upload a List of JARs Into Nexus/Artifactory

Want to see the beauty of Gradle in action? Here we'll learn how to build a custom task that uploads a list of JARs to Nexus or Artifactory.

By 
Peng Zhou
·
May. 22, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

More and more Java projects are starting to use Gradle as their build tool. As a coder, you'll find the power back in your hands again after using it. Because Gradle itself is a Groovy script, a custom task can be easily and concisely created. Here is an example of loading a list of libraries into a Nexus or Artifactory server. Before trying to load it, let's start with a custom task.

configurations {
    oneLib
}

artifacts {
    oneLib file("path-to/one-lib.jar")
}

apply plugin: 'maven'

project.tasks.create('uploadOneLib', Upload.class) {
    configuration = configurations.oneLib
    repositories {
        mavenDeployer {
            repository(url: "${protReleaseUrl}") {
                authentication(userName: "${protReleaseUser}", password: "${protReleasePswd}")
            }
            pom.version = "0.0.1"
            pom.artifactId = "one-lib"
            pom.groupId = "com.onelib"
        }
    }
}


The above Gradle DSL defines an "uploadOneLib" task to load one-lib.jar into the release repository as com.onelib:one-lib-0.0.1.jar (URL and credentials are provided by gradle.properties under ${user.home}/.gradle or the current folder). It extends the built-in task "Upload" to use Maven Deploy. If one-lib-0.0.1.jar has 5 dependent JARs, to load all of them, we can simply duplicate it for each one with slight modifications. But since Gradle has a loop construct, it would be nice to just list the libraries to be uploaded, the target URLs/credentials, some consideration to allow different URLs/credentials for different libraries, and some different classifiers for some libraries. The meta-data structure will look like:

{
    "libs": [
        {
          "cfgName": "uploadInternal1"
          , "groupId": "com.prot.zbank.client"
          , "artifactId": "client-model"
          , "version": "0.0.8"
          , "jarLocation": "client/client-model.jar"
        }
        , {
          "cfgName": "uploadInternal2"
          , "groupId": "com.prot.zbank.client"
          , "artifactId": "client-model"
          , "classifier": "test"
          , "version": "0.0.8"
          , "jarLocation": "client/unit-test.jar"
        }
        , {
          "cfgName": "uploadInternal3"
          , "groupId": "com.prot.zbank.client"
          , "artifactId": "client-service"
          , "version": "0.0.9"
          , "jarLocation": "client/rest-api.jar"
        }
        , {
          "cfgName": "uploadOracleDriver"
          , "groupId": "com.oracle"
          , "artifactId": "ojdbc8"
          , "version": "0.2b"
          , "jarLocation": "3rd/ojdbc8.jar"
          , "repoUrl": "http://a.b.com/repository/third-party/"
          , "repoUser": "david"
          , "repoPswd": "Passw0rd888"
        }
    ]
    , "repoUrl": "http://a.b.com/repository/zbank-project-release/"
    , "repoUser": "george"
    , "repoPswd": "Passw0rd999"
}


The model says to:

  1. Upload from client/client-model.jar to com.prot.zbank.client:client-model-0.0.8.jar

  2. Upload from client/unit-test.jar to com.prot.zbank.client:client-model-0.0.8-test.jar

  3. Upload from client/rest-api.jar to com.prot.zbank.client:client-service-0.0.9.jar

  4. Upload from 3rd/ojdbc8.jar to com.oracle:ojdbc8-0.2b.jar

The first 3 JARs are uploaded to http://a.b.com/repository/zbank-project-release/, and the last one will be uploaded to http://a.b.com/repository/third-party/.

Gradle shines for this kind of task. In about 50 lines of code, a custom task — "batchUpload" — is created for our needs.

apply plugin: 'maven'

def f = file("upload-list.json")
def m = f.exists() ? new groovy.json.JsonSlurper().parse(f) : null

task batchUpload() {
}

if (m == null || m.libs == null || m.libs.isEmpty()) {
    batchUpload.doLast {
        println "WARNING: no file to upload"
    }
} else {
    m.libs.each { lib ->
        configurations.create( "${lib.cfgName}" )
        if (lib.classifier == null) {
            artifacts.add("${lib.cfgName}", file("${lib.jarLocation}"))
        } else {
            artifacts.add("${lib.cfgName}", file("${lib.jarLocation}")) {
                setClassifier( "${lib.classifier}" )
            }
        }

        def repoUrl = lib.repoUrl != null ? lib.repoUrl : m.repoUrl;
        def repoUser = lib.repoUser != null ? lib.repoUser : m.repoUser;
        def repoPswd = lib.repoPswd != null ? lib.repoPswd : m.repoPswd;
        def classifier = lib.classifier == null ? "" : ("-" + lib.classifier)
        println "define task '${lib.cfgName}' to upload ${lib.jarLocation} to ${repoUrl} as ${lib.groupId}:${lib.artifactId}-${lib.version}${classifier}.jar"

        project.tasks.create("${lib.cfgName}", Upload.class) {
            configuration = configurations[lib.cfgName]
            repositories {
                mavenDeployer {
                    repository(url: "${repoUrl}") {
                        authentication(userName: "${repoUser}", password: "${repoPswd}")
                    }
                    pom.version = "${lib.version}"
                    pom.artifactId = "${lib.artifactId}"
                    pom.groupId = "${lib.groupId}"
                }
            }
        }

        batchUpload.dependsOn "${lib.cfgName}"
    }
}


It loops through each lib from the JSON model, creates a custom task to perform the upload, then tells the "batchUpload" task to depend on this task, so running "gradle batchUpload" will try to upload all libraries. Running "gradle ${lib.cfgName}" will just upload that ${lib} only. I can feel the beauty of this simplicity after using Gradle for one year. 

Upload Gradle JAR (file format) Task (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Replace your Scripts with Gradle Tasks
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • The Complete Gradle Plugin Tutorial
  • Zero Trust for Agents: A New Lens to Secure Agents

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook