Saturday, 23 May 2015

Knowing gradle dependency jars download location and copying them to custom location

Background

Whenever you need any dependency in your gradle script, gradle will download it it's cache and use it. By default you should see this cache in .gradle folder under your user directory.

For me it is
  • C:\Users\athakur\.gradle\caches
In this post I will show
  1. how to pinpoint exact location of the downloaded dependency jars and
  2. how to copy these jars to some custom directory you might want the dependencies in
using simple gradle scripts.


Gradle script to show downloaded jars location

Note : Whenever you execute some gradle task using gradle taskName then gradle will by default try finding the task in a file named build.gradle in current directory. 

Put the following code in build.gradle file - 


apply plugin: 'java'

repositories{
  mavenCentral()
}

dependencies{
  compile 'com.google.guava:guava:12.0'
}

task showMeCache << {
  configurations.compile.each { println it }
}

and run the following command
  • gradle showMeCache
You should see the downloaded jar location printed on console. For me it shows as in following screenshot


 Now let's see how we can copy these jar's into some custom directory of your choice.

Gradle script to copy downloaded jars to custom location

 Again append the following code to build.gradle file we created while printing the downloaded jar files location.

task copyDepJars(type: Copy) {
  from configurations.compile
  into 'C:\\Users\\athakur\\Desktop\\lib'
}


Now run following command
  • gradle copyDepJars
All dependent jars should be downloaded to the directory you have specified in "into" attribute of task copyDepJars .


To know more about copy task in gradle refer - Copy - Gradle DSL Version 2.4


Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top