using springs resource abstraction will save you a lot of hassle, i think.
given a plugin resource src/main/resources/foo/bar.txt, it could be used
in one of these ways (among others ofc):
https://gist.github.com/zyro23/f662d4eef32dcead859e
a few things worth mentioning there:
- @Component/@PostConstruct just used for the example. as long as you
are in a bean that is subject to autowiring, you are set
- when using @Value injection into a Resource, the placeholder key has
to be a config entry (i.e. a resolvable property source entry), in my
example (application.yml):
foobar: classpath:foo/bar.txt
supplying a string directly will not use the required converter.
- when accessing resources inside jar files always use
Resource.getInputStream(). getFile() will not work reliably (if at all)
(because the resource is _inside_ a jar file..)
- if its really not possible for you to extract the resource loading
into a bean, you should be able to statically use
Holders.grailsApplication.mainContext.getResource(...).
hth, zyro
-------- Original Message --------
Subject: Getting resources from Grails 3 plugin
From: Owen Rubel <
oru...@gmail.com>
To:
grails-de...@googlegroups.com
Date: Mon, 15 Jun 2015 11:39:45 -0700 (pdt)
> Am trying to test a plugin locally and can see the resources for the
> plugin are generating fine in the build under
> 'build/resources/main/public' ... but when I include the plugin in a
> project and try to access the files, it says they're not there.
> Obviously it is looking locally but the plugin is trying to access it's
> resources in the project.
>
> How do we write in the plugin so it can access it's resources when it is
> loaded in a project? I have it written as the following:
>
> *if*(grailsApplication.isWarDeployed()){
>
> ioPath=Holders.servletContext.getRealPath('/')
>
> *
> if*(Environment.current==Environment.DEVELOPMENT||Environment.current==Environment.TEST){
>
> ioPath+='WEB-INF/classes/iostate'
>
> }*else*{
>
> // test in Environment.PRODUCTION
>
> ioPath+='WEB-INF/classes/iostate'
>
> }
>
> }*else*{
>
> ioPath=(String)grails.util.BuildSettings.RESOURCES_DIR?.path
>
> *
> if*(Environment.current==Environment.DEVELOPMENT||Environment.current==Environment.TEST){
>
> ioPath+='/public/iostate'
>
> }*else*{
>
> // test in Environment.PRODUCTION
>
> ioPath+='/public/iostate'
>
> }
>
> }
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
grails-dev-disc...@googlegroups.com
> <mailto:
grails-dev-disc...@googlegroups.com>.
> To post to this group, send email to
grails-de...@googlegroups.com
> <mailto:
grails-de...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/grails-dev-discuss/a5606b7a-52b5-4670-a8c8-4e1b5f1019ad%40googlegroups.com
> <
https://groups.google.com/d/msgid/grails-dev-discuss/a5606b7a-52b5-4670-a8c8-4e1b5f1019ad%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
https://groups.google.com/d/optout.