how to download external files in gradle?

前端 未结 7 527
你的背包
你的背包 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/).

提交回复
热议问题