How to Dynamically Resolve Private JARs in Gradle for Jenkins CI

This article explains two practical approaches for handling private JAR dependencies in a Gradle‑based Jenkins CI pipeline, including fixed path assignment and automated network‑based retrieval, and provides a complete Gradle script example illustrating the implementation.

FunTester
FunTester
FunTester
How to Dynamically Resolve Private JARs in Gradle for Jenkins CI

When using Jenkins for continuous integration of a Java test project, the build tool is Gradle, but some required JARs reside in a private repository that is not yet set up. This causes inconsistent JAR locations across developers' machines and the build server, leading to frequent build failures when the build.gradle file is committed with hard‑coded paths.

Two solutions are presented:

Fixed path method: Define a constant JAR location for each developer, detect the current user, and assign the appropriate path to the compile files argument. This approach is simple and low‑maintenance because the core framework JARs rarely change.

Dynamic download method: On each build, download the required JAR from a LAN server, compare versions, and store it in the project directory before assigning it to compile files. This enables automatic synchronization when the server‑side JAR is updated by Jenkins.

The following Gradle script implements the dynamic approach. It defines helper functions getPath and getPath2 to locate a JAR locally or fetch it from a remote HTTP server, then adds the resolved file to the dependencies block.

buildscript {
    ext {
        springBootVersion = '1.5.13.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.fission.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

def jarversion = "api_test_najm-1.0-SNAPSHOT.jar"

def path = getPath2(jarversion)

def getPath() {
    def local
    File file1 = new File("/Users/Vicky/Documents/workspace/api_test_najm/build/libs/api_test_najm-1.0-SNAPSHOT.jar")
    File file2 = new File("/go/jar/api_test_najm-1.0-SNAPSHOT.jar")
    if (file1.exists()) {
        local = file1.getAbsoluteFile()
    } else if (file2.exists()) {
        local = file2.getAbsoluteFile()
    }
    println local
    return local
}

def getPath2(String v) {
    def jarpath = new File("").getAbsolutePath() + "/long/" + v
    if (new File(jarpath).exists()) return jarpath
    def url = new URL("http://**.***.**.**:****/go/jar/" + v)
    def out = new FileOutputStream(jarpath)
    out << url.newInputStream()
    return jarpath
}

dependencies {
    runtime 'org.springframework.boot:spring-boot-devtools'
    compile 'org.springframework.boot:spring-boot-starter-web'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    compile "org.springframework.boot:spring-boot-starter-thymeleaf"
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.8.0-beta2'
    compile files(path)
}

The author notes that having prior experience with Groovy made writing the script straightforward, and promises future posts on custom Gradle tasks and deeper Jenkins integration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Build Automationdependency managementGradleCIJenkins
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.