Wednesday, February 22, 2017

Simple Gradle file to just download dependencies

If you are managing repositories like Nexus or Artifactory, you often receive complaints from developers that, there dependency is not getting downloaded, though it is present in Nexus/Artifactory. Particularly if you are hosting a local mirror repository (for example for local India developers), then they will doubt on mirror caching as well. But you can always browse the artefacts at there expected paths in Nexus/Artifactory and if it is found there and still developer complains that it is not getting downloaded, then you need to troubleshoot either issue with Gradle or Maven config file or could be network issue as well.

In this blog post, I'm just posting simple Gradle file to just download a dependency, which will help us to debug download issues.

Where Gradle stores artifacts locally on our machine?

Typically at $HOME/.gradle/caches/modules-2/files-2.1/<GROUP-ID>/<ARTIFACT-ID>/<VERSION>

Simple Gradle file to download dependencies

$ cat build.gradle 
apply plugin: 'java'

repositories {
   //The properties nexusMavenRepos, localNexusBaseUrl, etc will be defined gradle.properties file as shown in the end
   nexusMavenRepos.split().each { repo ->
            maven { url(localNexusBaseUrl + repo) }
        }
}

//Below code contacts external maven central repo. comment it, if you don't want to go external
repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.8.2'
    compile 'commons-beanutils:commons-beanutils:1.8.3'
    testCompile group: 'com.typesafe.akka', name: 'akka-http-core_2.11', version: '2.4.4'
}

Then run gradle dependencies task

$ gradle dependencies 

or


$ gradle dependencies --refresh-dependencies    #In case if you want to force gradle to download

This will download the requested artefacts under $HOME/.gradle/caches/modules-2/files-2.1/ 

// typical gradle.properties file for reference
$ cat gradle.properties
nexusBaseUrl = https://my-org-nexus/nexus/content/repositories/
localNexusBaseUrl = https://my-org-blr-nexus/nexus/content/repositories/
nexusMavenRepos = central my-org-third-party-lib