简体   繁体   中英

Gradle or Groovy - how to convert URL to file?

In Gradle I have a file stored on the Internet that I would like to access. But I can't figure out how to convert a URL to a File in Gradle. I need the url to be converted to a file so I can pass it to tasks as a file. Here is what I have so far:

allprojects {
    afterEvaluate { project ->
      String file = new URL('http://techslides.com/demos/samples/sample.json').file
    }
}

this is definetly not what i want. The file variable ends up being: demos/samples/sample.json so it looks like it's just taking the path from the url itself.

I need the actual sample.json to be stored in a File object. I would also prefer if possible to not have to write the file to local storage where it is accessible to other people as its a secure file.

The simplest way to do this:

def fileLocation = 'file://techslides.com/demos/samples/sample.json'
def fileURL = new URL(fileLocation)
def remoteFile = new File(fileURL.toURI())

See: File(URI uri)

Replace fileLocation with valid value for your task.

As of May 4, 2018, Gradle 4.8 RC1 or later, if you are facing this problem using checkstyle, you can use

checkstyle {
    toolVersion = "8.13"
    config project.resources.text.fromUri("${someUrl}/checkstyle.xml")
}

See https://github.com/gradle/gradle/issues/2663

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM