how to download external files in gradle?

前端 未结 7 502
你的背包
你的背包 2020-12-02 09:50

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

相关标签:
7条回答
  • 2020-12-02 10:17

    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/).

    0 讨论(0)
  • 2020-12-02 10:26

    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..

    0 讨论(0)
  • 2020-12-02 10:32

    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 :)

    0 讨论(0)
  • 2020-12-02 10:38

    How about just:

    def f = new File('the file path')
    if (!f.exists()) {
        new URL('the url').withInputStream{ i -> f.withOutputStream{ it << i }}
    }
    
    0 讨论(0)
  • 2020-12-02 10:40

    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

    0 讨论(0)
  • 2020-12-02 10:41

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题