2

I am beginner in grails.
I would like to add some static resources to grails app and exclude them from url mappings.
I’ve added the following line to UrlMappings groovy:

class UrlMappings {
    static excludes = ['/resources/*']
...

But I don’t know how add resources to app and to the final war. Probably there is settings in build.gradle.
I want to open static html which can use static js/css/images.
Like this: localhost:8080/resources/index.html

Thanks in advance

U.DOG
  • 47
  • 1
  • 8
  • Thank you! @emmanuel-rosa Well, I would like to use npm package manager in my project, and there could be hundreds of js/css/other files, in resources folder. Probably there is robust solution, which just automatically add folder with all content to war, and process resources in it statically. Appreciate any help – U.DOG Apr 17 '16 at 20:43

2 Answers2

6

Since Grails 2.4, static assets are managed by the Asset Pipeline plugin. It's well documented, but it amounts to placing static content in either:

  1. grails-app/assets/javascripts
  2. grails-app/assets/stylesheets
  3. grails-app/assets/images

Then pull them into your GSP view with the <asset> tag:

<asset:javascript src="something.js"/>
<asset:stylesheet src="something.css"/>
<asset:image src="something.png" width="200" height="200"/>

It's quite easy, just read the documentation.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • Thank you! @emmanuel-rosa Well, I would like to use npm package manager in my project, and there could be hundreds of js/css/other files, in resources folder. Probably there is robust solution, which just automatically add folder with all content to war, and process resources in it statically. Appreciate any help – U.DOG Apr 17 '16 at 20:46
  • Check this out: https://objectpartners.com/2015/04/29/using-gradle-and-bower-to-manage-jscss-dependencies/ – Emmanuel Rosa Apr 17 '16 at 20:53
  • Thanks! This link is useful too, http://svene.github.io/adocblog/html/2015_05_09_webjars_in_webapplications.html – U.DOG Apr 18 '16 at 20:33
2

See the grails documentation on static resources. https://gsp.grails.org/latest/guide/resources.html

Note the following information on alternatives to the asset pipeline.

If you do not want to use the Asset-Pipeline plugin, you can serve the static assets from directories src/main/resources/public or src/main/webapp, but the latter one only gets included in WAR packaging but not in JAR packaging.

Duncan
  • 176
  • 1
  • 4