grails-core icon indicating copy to clipboard operation
grails-core copied to clipboard

Grails 5.0.2 - Cannot exclude views from Plugins - causes PluginException "Failed to initialize view [viewName] from plugin [pluginName]" in client application

Open boardbloke opened this issue 2 years ago • 5 comments

I have a plugin that uses some GSP views for testing, and I do not want the views to appear in the plugin jar file.

I have constructed the build.gradle to exclude the compiled gsp classes, and added them to the pluginExcludes as documented in https://docs.grails.org/latest/guide/plugins.html.

The result is compiled classes are not in the jar as expected, BUT the views are still represented in the views.properties files in the gsp folder of the plugin jar.

That file seems to be getting used by BinaryGrailsPlugin.initializeViewMap in the grails-core plugin to initialise the plugin, and that throws the PluginException error because the class files do not exist for the views specified.

I think the problem is either view.properties should not contain the excluded gsp, or BinaryGrailsPlugin.initializeViewMap should be using the specified pluginExcludes to filter out the excluded gsp when it initialises the binary plugin

boardbloke avatar Nov 30 '21 19:11 boardbloke

Attached an example Grail 5.02 web-plugin profile project that recreates. The build.gradle has been modified to exclude error.gsp, and the pluginExcludes excludes error.gsp

However, if you run gradlew jar and look at the jar file - view properties includes a line for error.gsp.

If you look at https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/grails/plugins/BinaryGrailsPlugin.java you see that when the plugin is loaded an error is thrown if there is an entry in views.properties that is not accompanied by the corresponding compiled gsp files

boardbloke avatar Nov 30 '21 19:11 boardbloke

Note it does appear to be possible to workaround this issue if you want to exclude all views - by having a excludes in the jar task in build.gradle that looks like:

jar {
   excludes gsp**
}

as that excludes the gsp folder containing view.properties. But if you want (as I do) to include some but not all gsps, there doesn't appear to be a way to do that

boardbloke avatar Dec 01 '21 10:12 boardbloke

If anybody ends up look at this issue - I was able to get a better workaround - that allows you to selectively exclude gsp files by using gradle to rewrite the view.properties file as part of the Plugin gradle jar task as follows:

jar {
    // We wish to exclude all gsps except unavailable.gsp
    List includes = [
            'unavailable.gsp'
    ]

    eachFile { copyDetails ->
        if (copyDetails.path.contains('views.properties')) {
            filter { line -> includes.find{line.contains(it)} ? line : null}
        }
    }
}

boardbloke avatar Dec 14 '21 16:12 boardbloke

For what it's worth, I've noticed that a lot of plugins put optional views in src/main/templates which are then expanded on request with something like grails create-my-test-templates or some such. That way you can have them in source control without deployed. The other option is you don't worry about it, and put them behind some security.

xpusostomos avatar Jan 16 '22 12:01 xpusostomos