Grails Dynamic Modules Plugin(GDMP) offer new ways of creating modular and maintainable Grails applications.
This project Grails Dynamic Modules Demo show you how to Develop dynamic modules in your Grails plugin,
I create three plugin module types: LanguageModuleDescriptor
, MenuModuleDescriptor
, MenuItemModuleDescriptor
, and use these modules to rewrite the Grails main page.
- Grails 4.1.2
Grails Dynamic Modules Plugin has been released to Maven Central, please check and update the latest version.
Since the plugin is for building multiple modules, I highly recommend that you read this guide Grails Multi-Project Build first.
In this project, I create a Menu
plugin in the plugins/menu
.
.
├── gradle
│ └── wrapper
├── grails-app
│ ├── assets
│ ├── conf
│ ├── controllers
│ ├── domain
│ ├── i18n
│ ├── init
│ ├── services
│ ├── taglib
│ ├── utils
│ └── views
├── plugins
│ └── menu
├── src
│ ├── integration-test
│ ├── main
│ └── test
├── build.gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── grails-wrapper.jar
├── grailsw
├── grailsw.bat
└── settings.gradle
Add dependency to the build.gradle
,
repositories {
mavenCentral()
}
dependencies {
// in Grails 4
compile "org.rainboyan.plugins:grails-plugin-dynamic-modules:0.1.0"
// in Grails 5/6
implementation "org.rainboyan.plugins:grails-plugin-dynamic-modules:0.1.0"
}
In the Grails plugin project: plugins/menu
,
create a Module descriptor: MenuModuleDescriptor
,
@ModuleType('menu')
class MenuModuleDescriptor extends AbstractModuleDescriptor {
String i18n
String title
String link
String location
int order
MenuModuleDescriptor() {
}
@Override
void init(GrailsPlugin plugin, Map args) throws PluginException {
super.init(plugin, args)
this.i18n = args.i18n
this.title = args.title
this.link = args.link
this.location = args.location
}
}
and then update the MenuGrailsPlugin
to extend grails.plugins.DynamicPlugin
,
class MenuGrailsPlugin extends DynamicPlugin {
// 1. add your new module types
def providedModules = [
MenuModuleDescriptor
]
// 2. define 'menu' modules in doWithDynamicModules
void doWithDynamicModules() {
menu(key: 'about', i18n: 'menu.about', title: 'About US', link: '/about', location: 'topnav')
menu(key: 'product', i18n: 'menu.product', title: 'Products', link: '/product', location: 'topnav', enabled: "${Environment.isDevelopmentMode()}") {
description = "This menu enabled: ${Environment.isDevelopmentMode()}"
order = 2
}
menu(key: 'contact', i18n: 'menu.contact', title: 'Contact', link: '/contact', location: 'topnav', enabled: false)
menu(key: 'help', i18n: 'menu.help', title: 'Help', link: '/help', location: 'footer')
}
}
now, you can get all the module descriptors in your Grails application throug the extended API of GrailsPluginManager
,
// Get all the ModuleDescriptors
Collection<ModuleDescriptor<?>> allDescriptors = pluginManager.getModuleDescriptors()
// Get all the enabled MenuModuleDescriptor
List<MenuModuleDescriptor> menuDescriptors = pluginManager.getEnabledModuleDescriptorsByClass(MenuModuleDescriptor)
Now you can run it,
This plugin is available as open source under the terms of the APACHE LICENSE, VERSION 2.0