How to reuse Grails application's domain classes? - grails

I have already create a Grails application. For some reasons I need to create another console application for someone to modify database data.
Is it possible to package Grails application as a JAR library, so that the console application can reuse those domain classes?
Or, I add/create some classes in Grails application and package it as a JAR and run as console application?

If no better answer, probably I will use the batch-launcher plugin to do that.

You can put the domain classes in a JAR and tell Grails that these are your domain classes by adding a Hibernate XML file (in grails-app/conf/hibernate) that refers to the classes in this JAR. You can use this JAR in any other Java/Groovy application, but obviously they'll only have the persistence methods (dynamic finders, save(), etc.) when used in a Grails app.

Related

How to share Domain Codebase between Grails 2 and Grails 3

At the moment, we have multiple grails applications built on grails 2.5.0
Our Model is in a grails plugin (2.5.0), published to a local maven repository. So far everything works great.
Now we want to create a new application with Grails 3, which relies on some of the basic Domains located in a grails2 plugin (User, Group, etc).
What would be the best way, to share those ? I dont want to maintain two code-bases for our model, one for v2 and one for v3 ...
The only thing which came into my mind is, building everything in plain groovy Classes / Interfaces, and then extending / implementing the Model in both grails2 and grails3 plugins
core-model (contains Interfaces, abstract classes)
grails-model-v2
grails-model-v3
You're on the right track. The domain classes will probably work unchanged, but the problem is the grails plugin projects are incompatible. So you can use a plain groovy project to house your domains and then have each plugin depend on the domain project. The tricky part is telling grails that those plain groovy classes are domains. In grails 3 you can probably just apply the groovy traits that grails automatic applies to domains using doWithSpring(). I think grails 2 uses metaClass to accomplish the same thing, so the approach might be similar.

Modify Grail plugin Domain Class data-source in dependent application

I have multiple plugins with Grails domain classes that are stored in separate databases. I want to be able to configure within the dependent project what sources each class comes from. It seems like a similar question is here:
Grails changing datasource at runtime
Is it still not possible to add additionaly sources to a class at runtime? And, I don't necessarily need to do it at runtime either. Just configure the class in the dependent Grails application. The method for setting this up (http://www.grails.org/doc/2.2.1/guide/conf.html#multipleDatasources) requires direct access to the class definitions, and I'd rather avoid having to do that.

How to use Grails Domain objects in a Java project?

I have a Grails application with outsourced (in a separate Grails Plugin) domain objects and would like to use the domain objects in a Java application.
If this is possible, how to do that?
Thanks
In order to use them as domain classes, you need GORM-plugin to be installed somehow in your java code. Without the plugin the domain classes lose their "active record" aspect and can be used only as POJOs.
I can barely imagine how to do that w/o lot of pain
You need to define your custom hbm.xml hibernate mapping files. See hibernate doc

How to use groovy domain classes in my Java Project?

I have some domain classes in grails application. My middle ware is called as servic
The domain classes would be shared between grails-app and services.
If we use annotation – would services be able to detect them?
How do you share the classes with services ?. Can you do a jar on them?.
Can any one please help me out
Thanks Siva.
You may access your grails domain classes with hql query. See How to execute HQL query in Java for reference.

Is it possible to add Grails MVC classes at deployment time?

I'm writing a Grails app which I'd like 3rd parties to augment at runtime. Ideally they would be able to add a JAR/WAR to the webapp directory which contains new domain, controller and service classes, new views, and other content.
Is there a simple way to do this within grails? Would it be simplest to create a startup script which copies the new classes etc. into the relevant directories and then updates grails.xml and web.xml?
You will be able to do this in version 2 of grails in which plugins will be also OSGI plugins http://jira.codehaus.org/browse/GRAILS/fixforversion/15421
It seems that the Grails plugins will actually fit quite well for this: http://www.grails.org/Understanding+Plugins
A plugin can do just about anything... One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions

Resources