I want to use a plugin to share a bunch of domain classes and controllers between multiple applications. Eac开发者_开发技巧h application will use its own database. I'd like the domain classes in the plugin to store their data in the same database as the application.
How do I do that please?
The plugin basically gets merged into the application that contains it, so it uses the settings from the application. This includes the DataSource. There's actually no way to specify in a domain class what database it uses. Technically this isn't true in 2.0 since it can choose which of multiple datasources to use, but the choice comes from the application's defined datasources.
Your plugin can change the DataSource.groovy file, so you might try reading the application name from the configuration and setting the data source accordingly. Perhaps it would look like this:
production {
dataSource {
jndiName = "java:comp/env/jdbc/${grailsApplication.metadata.'app.name'}"
}
}
In Grails 3 if you want to share domain classes between multiple applications you can put the domain classes in a plugin.
You can then add that plugin as a dependency to your multiple applications.
Your application will then have access to those domain classes.
The datasource is set at the toplevel application that uses the plugins.
So if you only have 1 datasource setup in the top level app the plugins domain classes will be created in the outputed sql when you do a grails schema-export command.
精彩评论