how to download external files in gradle? - gradle

I have a gradle project which requires some data files available somewhere on the internet using http. The goal is that this immutable remote file is pulled once upon first build. Subsequent build should not download again.
How can I instruct gradle to fetch the given file to a local directory?
I've tried
task fetch(type:Copy) {
from 'http://<myurl>'
into 'data'
}
but it seems that copy task type cannot deal with http.
Bonus question: is there a way to resume a previously aborted/interrupted download just like wget -c does?

How about just:
def f = new File('the file path')
if (!f.exists()) {
new URL('the url').withInputStream{ i -> f.withOutputStream{ it << i }}
}

You could probably use the Ant task Get for this. I believe this Ant task does not support resuming a download.
In order to do so, you can create a custom task with name MyDownload. That can be any class name basically. This custom task defines inputs and outputs that determine whether the task need to be executed. For example if the file was already downloaded to the specified directory then the task is marked UP-TO-DATE. Internally, this custom task uses the Ant task Get via the built-in AntBuilder.
With this custom task in place, you can create a new enhanced task of type MyDownload (your custom task class). This task set the input and output properties. If you want this task to be executed, hook it up to the task you usually run via task dependencies (dependsOn method). The following code snippet should give you the idea:
task downloadSomething(type: MyDownload) {
sourceUrl = 'http://www.someurl.com/my.zip'
target = new File('data')
}
someOtherTask.dependsOn downloadSomething
class MyDownload extends DefaultTask {
#Input
String sourceUrl
#OutputFile
File target
#TaskAction
void download() {
ant.get(src: sourceUrl, dest: target)
}
}

Try like that:
plugins {
id "de.undercouch.download" version "1.2"
}
apply plugin: 'java'
apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download
task downloadFile(type: Download) {
src 'http://localhost:8081/example/test-jar-test_1.jar'
dest 'localDir'
}
You can check more here: https://github.com/michel-kraemer/gradle-download-task
For me works fine..

The suggestion in Ben Manes's comment has the advantage that it can take advantage of maven coordinates and maven dependency resolution. For example, for downloading a Derby jar:
Define a new configuration:
configurations {
derby
}
In the dependencies section, add a line for the custom configuration
dependencies {
derby "org.apache.derby:derby:10.12.1.1"
}
Then you can add a task which will pull down the right files when needed (while taking advantage of the maven cache):
task deployDependencies() << {
String derbyDir = "${some.dir}/derby"
new File(derbyDir).mkdirs();
configurations.derby.resolve().each { file ->
//Copy the file to the desired location
copy {
from file
into derbyDir
// Strip off version numbers
rename '(.+)-[\\.0-9]+\\.(.+)', '$1.$2'
}
}
}
(I learned this from https://jiraaya.wordpress.com/2014/06/05/download-non-jar-dependency-in-gradle/).

Using following plugin:
plugins {
id "de.undercouch.download" version "3.4.3"
}
For a task which has the purpose of only downloading
task downloadFile(type: Download) {
src DownloadURL
dest destDir
}
For including download option into your task:
download {
src DownloadURL
dest destDir
}
For including download option with multiple downloads into your task:
task downloadFromURLs(){
download {
src ([
DownloadURL1,
DownloadURL2,
DownloadURL3
])
dest destDir
}
}
Hope it helped :)

just now ran into post on upcoming download task on gradle forum.
Looks like the perfect solution to me.. Not (yet) available in an official gradle release though

Kotlin Version of #Benjamin's Answer
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.1")
}
}
tasks.register("downloadPdf"){
val path = "myfile.pdf"
val sourceUrl = "https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf"
download(sourceUrl,path)
}
fun download(url : String, path : String){
val destFile = File(path)
ant.invokeMethod("get", mapOf("src" to url, "dest" to destFile))
}

Related

Can Gradle generate a POM in such a way that it doesn't unnecessarily touch the output file?

We're creating a POM file like so:
task createPom {
doLast {
pom {
project {
groupId project.group
artifactId project.name
version project.ext.pomVersion
}
}.writeTo("${buildDir}/pom.xml")
}
}
This writeTo eagerly writes to the file even if no changes have occurred, which causes the jar to change, so later expensive tasks in the build (indexing, signing, building installers) all have to run as well.
I thought about writing to a temp file and diffing the two files somehow, but it seems like I'd have to write a lot of boilerplate just to do that, so I'm wondering whether there is a proper way to go about it.
What you need to do is to configure task's inputs and outputs which are used to determine whether your task is up-to-date or not. Here, how it should be done:
apply plugin: 'maven'
ext {
pomVersion = '3.1.4.5'
}
group = 'lolgroup'
task createPom {
outputs.file "${buildDir}/pom.xml"
inputs.property('group', project.group)
inputs.property('name', project.name)
inputs.property('pomVersion', project.ext.pomVersion)
doLast {
pom {
project {
groupId inputs.properties['group']
artifactId inputs.properties['name']
version inputs.properties['pomVersion']
}
}.writeTo("${buildDir}/pom.xml")
}
}
Here you can find a demo.

use root path to file instead of subproject path for zipTree

I've got this project structure
journey\
applications\
desktop\
core\
I've got a task that downloads .zip archive.
task download_redis(type: Download) {
src 'https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip'
dest new File("applications/redis/install", 'Redis-x64-3.2.100.zip')
onlyIfNewer true
}
It saves zip file into applications\redis\install.
Then I've got another task that supposed to extract the content of zip file into applications/redis.
task install_redis(dependsOn: download_redis, type: Copy) {
from zipTree(download_redis.dest)
into 'applications/redis'
}
But I get this error
Cannot expand ZIP 'L:\journey\desktop\applications\redis\install\Redis-x64-3.2.100.zip' as it does not exist.
It seems trying to resolve the path to zip from desktop project, despite it being level higher - a root.
How to set for zipTree to use path from root and not from subproject? (Though both these tasks are inside desktop subproject)
You can access various paths like so:
println project(":applications").projectDir
println project(":desktop").projectDir
// root
println project(":").projectDir
Here is an example :desktop:build.gradle file that uses a spoof zip download. Note the applicationsDir variable. (I'm assuming that you are using the same plugin as the one specified.):
plugins {
id "de.undercouch.download" version "1.2"
}
apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download
def applicationsDir = project(":applications").projectDir
task download_redis(type: Download) {
src 'https://github.com/codetojoy/Git_Sandbox/raw/master/tmp/example.zip'
dest new File("${applicationsDir}/redis/install", 'Redis-x64-3.2.100.zip')
onlyIfNewer true
}
task install_redis(dependsOn: download_redis, type: Copy) {
from zipTree(download_redis.dest)
into "${applicationsDir}/redis"
}

Change name of zip when creating archive with distribution plugin in gradle

Is there any way to configure distribution file name when creating archive using distribution plugin in gradle?
Here is my build.gradle (I'm using gradle 2.10) file:
apply plugin: 'distribution'
version '1.2'
distributions {
custom {
baseName = 'someName'
contents {...}
}
}
And calling the task customDistZip creates file: $buildDir/distributions/someName-1.2.zip I would like to have a file name configurd to: someName.zip (without version number).
My workaround for this is to create new task to rename the zip file after creation and use gradle finalizedBy feature:
task renameDist {
doLast {
new File("$buildDir/distributions/someName-${project.version}.zip")
.renameTo("$buildDir/distributions/someName.zip")
}
}
customDistZip.finalizedBy renameDist
But this don't look like elegant and nice solution.
You can either do:
customDistZip.setVersion('')
to set the version string used with baseName to empty or
customDistZip.setArchiveName('someName.zip')
to set the full archive filename, which skips basename alltogether.
Kotlin/Groovy:
distributions {
->distribution<- {
distributionBaseName.set(->name<-)
}
}

Gradle - "apply from" a ZIP dependency

In my Gradle build script I want to import a ZIP dependency that contains static analysis configuration (CheckStyle, PMD etc.) and then "apply from" the files in that ZIP. When anyone runs the "check" task, my custom static analysis configuration should be used then.
I've tried the somewhat convoluted solution below, but I can't get it to work. The files are retrieved and unpacked into the "config" directory, but "apply from" does not work - Gradle complains it cannot find the files; I assume this is due to "apply from" being run during the build configuration phase.
Is there a simpler way to do this?
repositories {
maven { url MY_MAVEN_REPO }
}
configurations {
staticAnalysis {
description = "Static analysis configuration"
}
}
dependencies {
staticAnalysis group:'my-group', name:'gradle-static-analysis-conf', version:'+', ext:'zip'
}
// Unzip static analysis conf files to "config" in root project dir.
// This is the Gradle default location.
task prepareStaticAnalysisConf(type: Copy) {
def confDir = new File(rootProject.projectDir, "config")
if (!confDir.exists()) {
confDir.mkdirs()
}
from {
configurations.staticAnalysis.collect { zipTree(it) }
}
into confDir
apply from: 'config/quality.gradle'
}
check.dependsOn('prepareStaticAnalysisConf')
You are perfectly right: Gradle runs apply during evaluation phase, but the prepareStaticAnalysisConf was not executed yet and the archive is not unpacked.
Instead of a task, just write some top-level code. It should do the trick. Also, you'd better use the buildscript level dependency, so that it is resolved before script is executed.
Here is the full script
buildScript {
repositories {
maven { url MY_MAVEN_REPO }
}
dependencies {
classpath group:'my-group', name:'gradle-static-analysis-conf', version:'+', ext:'zip'
}
}
def zipFile = buildscript.configurations.classpath.singleFile
copy {
from zipTree(it)
into 'config'
}
apply from: 'config/quality.gradle'

Extract specific JARs from dependencies

I am new to gradle but learning quickly. I need to get some specific JARs from logback into a new directory in my release task. The dependencies are resolving OK, but I can't figure out how, in the release task, to extract just logback-core-1.0.6.jar and logback-access-1.0.6.jar into a directory called 'lib/ext'. Here are the relevant excerpts from my build.gradle.
dependencies {
...
compile 'org.slf4j:slf4j-api:1.6.4'
compile 'ch.qos.logback:logback-core:1.0.6'
compile 'ch.qos.logback:logback-classic:1.0.6'
runtime 'ch.qos.logback:logback-access:1.0.6'
...
}
...
task release(type: Tar, dependsOn: war) {
extension = "tar.gz"
classifier = project.classifier
compression = Compression.GZIP
into('lib') {
from configurations.release.files
from configurations.providedCompile.files
}
into('lib/ext') {
// TODO: Right here I want to extract just logback-core-1.0.6.jar and logback-access-1.0.6.jar
}
...
}
How do I iterated over the dependencies to locate those specific files and drop them in the lib/ext directory created by into('lib/ext')?
Configurations are just (lazy) collections. You can iterate over them, filter them, etc. Note that you typically only want to do this in the execution phase of the build, not in the configuration phase. The code below achieves this by using the lazy FileCollection.filter() method. Another approach would have been to pass a closure to the Tar.from() method.
task release(type: Tar, dependsOn: war) {
...
into('lib/ext') {
from findJar('logback-core')
from findJar('logback-access')
}
}
def findJar(prefix) {
configurations.runtime.filter { it.name.startsWith(prefix) }
}
It is worth nothing that the accepted answer filters the Configuration as a FileCollection so within the collection you can only access the attributes of a file. If you want to filter on the dependency itself (on group, name, or version) rather than its filename in the cache then you can use something like:
task copyToLib(type: Copy) {
from findJarsByGroup(configurations.compile, 'org.apache.avro')
into "$buildSrc/lib"
}
def findJarsByGroup(Configuration config, groupName) {
configurations.compile.files { it.group.equals(groupName) }
}
files takes a dependencySpecClosure which is just a filter function on a Dependency, see: https://gradle.org/docs/current/javadoc/org/gradle/api/artifacts/Dependency.html

Resources