Question

I have some files in a specific folder into grails-app directory. During bootstrap, I would like to copy one of those files (let's say the latest, doesn't matter) and copy it into the web-app folder, to make it accessible to the grails application.

How would you do that? I wrote something like this:

class BootStrap {
    GrailsApplication grailsApplication

    def init = { servletContext ->
        // ...

        def source = new File('grails-app/myFolder/my-file-'+ grailsApplication.metadata.getApplicationVersion() +'.txt')
        def destination = new File('web-app/my-current-file.txt')

        source?.withInputStream { is ->
            destination << is
        }

        // ... 
    }
}

But I have difficulties to identify the right path for source and destination files (getting a FileNotFoundException). I already double checked folder and files names, my problem is the starting point for relative paths.

Is the bootstrap a good place to perform this kind of operation?

As always, thanks in advance.

Was it helpful?

Solution 2

How about hooking onto Grails events. Currently as part of the project compilation step, I am copying my external configuration file from conf folder to class path. So you can do something similar to that.

This is what I have in _Events.groovy file: I guess you can do something similar to this.

eventCompileEnd = {
ant.copy(todir:classesDirPath) {
  fileset(file:"${basedir}/grails-app/conf/override.properties")
}}

OTHER TIPS

I made it with the Bootstrap (please read the entire answer):

class BootStrap {
    GrailsApplication grailsApplication

    def init = { servletContext ->    
        def applicationContext = grailsApplication.mainContext
        String basePath = applicationContext.getResource("/").getFile().toString()

        File source = new File("${basePath}/../grails-app/myFolder/" + grailsApplication.metadata.getApplicationVersion() +'.txt')
        File destination = new File("${basePath}/my-current-file.txt")

        source?.withInputStream {
            destination << it
        }
    }
}

But, as suggested by Muein Muzamil, the best approach is with events. Here's his solution applied to my example:

eventCompileEnd = {
    metadata = grails.util.Metadata.getCurrent()
    appVersion = metadata."app.version"

    ant.copy(file: "${basedir}/grails-app/myFolder/${appVersion}.txt", tofile: "${basedir}/web-app/my-current-file.txt")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top