Getting resources from Grails 3 plugin

346 views
Skip to first unread message

Owen Rubel

unread,
Jun 15, 2015, 2:39:45 PM6/15/15
to grails-de...@googlegroups.com
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'

    }

}


zyro

unread,
Jun 15, 2015, 3:18:38 PM6/15/15
to grails-de...@googlegroups.com
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.

Owen Rubel

unread,
Jun 15, 2015, 3:49:22 PM6/15/15
to grails-de...@googlegroups.com
Two issues:

1) its a JSON file
2) Annotations create a binding and I need everything to be abstracted for theory and practice

Thanks for advice though. I'll give holders a shot. 

Owen Rubel

unread,
Jun 16, 2015, 12:01:05 PM6/16/15
to grails-de...@googlegroups.com
No thats not it. I was getting better results prior. First the method needs a directory path to the walk through the files contained within

But when I go to the plugins resource dir in 3.0, I am getting this error...

Caused by: java.io.FileNotFoundException: /home/owenrubel/workspace/api-framework-example/./build/resources/main/iostate
at org.codehaus.groovy.runtime.ResourceGroovyMethods.checkDir(ResourceGroovyMethods.java:1011)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.eachFile(ResourceGroovyMethods.java:1030)
at org.codehaus.groovy.runtime.ResourceGroovyMethods.eachFile(ResourceGroovyMethods.java:1056)
at org.codehaus.groovy.runtime.dgm$936.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
at net.nosegrind.apiframework.ApiObjectService.parseFiles(ApiObjectService.groovy:94)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInte.java:1270)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:207)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:151)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:163)
at net.nosegrind.apiframework.ApiObjectService.initialize(ApiObjectService.groovy:74)

The file is being pulled in in the applications build and IS at that location. But am wondering if I should file a JIRA for this.

Owen Rubel

unread,
Jun 16, 2015, 1:28:10 PM6/16/15
to grails-de...@googlegroups.com
So the issue is that previously, the plugin resources (like files in a plugins 'conf' dir) were loaded under an application 'build' directory during run-app.

Hence:

ioPath = (String) BuildSettings.TARGET_DIR?.path


This would allow me to find the resource in the plugin project when running the including application.


Now it seems that in 3.0, the files are kept in a build dir in the plugin, so the question is how do we access them iwthin the plugin from a script within the plugin when the application runs? Because the previous way doesn't seem to work anymore.

Owen Rubel

unread,
Jun 16, 2015, 2:20:20 PM6/16/15
to grails-de...@googlegroups.com
GrailsPluginUtils is also under codehaus and is deprecated (and I can't find a replacement class for it).

Everywhere I look where I would be able to reference classes in the plugin build has been removed, deprecated, etc and the application doesn't bring over those files.

How is the plugin supposed to reference its own BUILD files in the application when the application doesn't contain them??

Tyler Rafferty

unread,
Jul 26, 2016, 11:08:58 AM7/26/16
to Grails Dev Discuss
@Owen were you able to find a package that replaced 
org.codehaus.groovy.grails.plugins.GrailsPluginUtils
Reply all
Reply to author
Forward
0 new messages