4

On my project, I'm updating my Gradle version from 2.3 to 4.1 and I ran into an issue when getting absolute path on a resource inside a custom task.

In version 2.3, the getResource function returned the absolute path to the resource:

/Users/user/work/rootProject/buildSrc/src/main/resources/resource-name

In Gradle version 4.1 getResource function returned the absolute path to the jar:

file:/Users/user/.gradle/caches/jars-3/d6ba6bacd01b220f9dc681b05b86d37c/buildSrc-1.0.jar!/resource-name

After retrieving the path I copy the resource to a different location, and therefore the build fails with the new behavior.

What is the best way to get the path of a resource under a custom task?
example: buildSrc/src/main/resources

Holger Just
  • 52,918
  • 14
  • 115
  • 123
gilkal1979
  • 61
  • 3
  • Do you really need the `java.io.File`? If you are just copying the file, can't you use `ClassLoader.getResourceAsStream("resource-name")` to get the `InputStream` – lance-java Aug 23 '17 at 15:40
  • Thanks for the suggestion. You're right, I can do that, but that would require some other modifications to my code. I was hoping for something more compact. – gilkal1979 Aug 24 '17 at 12:15
  • Ideally, your code should operate on `InputStream` with possibly "helper" methods to use `java.io.File`. Once you stop dealinig with `File` and start using `InputStream` (or the concept of a `Resouce` interface with `getInputStream()` and `getName()`) you'll find testing becomes easier too. Eg I usually have `FileResource`, `ClasspathResource`, `StringResource` and `ByteArrayResource`. – lance-java Aug 24 '17 at 12:43

0 Answers0