2

I need to get the path of a static resource located in assets/schemas/resource.json in a Grails 3 service.

At the moment it is defined as

private final String SCHEMA = 'grails-app/assets/schemas/resource.json',

which is fine for development environment, but of course not for production (as it would be located in <app_root>/assets/resource.json.

I tried to search how to exploit the Asset Pipeline in my case, but up to now I really have no idea :P

Thanks in advance!

ilPittiz
  • 734
  • 1
  • 10
  • 23

3 Answers3

7

It works locally but not when deployed to a server. Using Grails 3.1.0, Java 1.8.0_91 and Tomcat 8.0.33.

assetResourceLocator?.findAssetForURI('myFolder/placeholder.jpg')?.byteArray

returns

groovy.lang.MissingPropertyException: No such property: byteArray for class: org.springframework.web.context.support.ServletContextResource

EDIT: Solved it:

assetResourceLocator?.findAssetForURI('myFolder/placeholder.jpg')?.getInputStream()?.bytes
Mexx
  • 359
  • 3
  • 17
2

It is covered in the docs. http://bertramdev.github.io/grails-asset-pipeline/guide/usage.html

In a controller or service, inject the assetResourceLocator and use assetResourceLocator.findAssetForURI()

npskirk
  • 1,188
  • 1
  • 8
  • 21
  • 1
    You're right :) The solution for my problem would have been `assetResourceLocator.findAssetForURI('schemas/resource.json').getURI()` (or alternatively `.getURL()`), but I later decided to put my json file in `src/main/resources` as I really didn't need it to be managed as an asset but as a simple resource. – ilPittiz Sep 06 '16 at 16:00
1

Complete example:

class ExampleService {
  def assetResourceLocator

  def someMethod() {
    Resource res = assetResourceLocator.findAssetForURI('test.css')
    String url = res.getURL()
    String uri = res.getURI()
  }
}

Source: http://bertramdev.github.io/grails-asset-pipeline/guide/usage.html

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94