Grails behavior difference between run-app and run-war - grails

I'm relatively new to Groovy and Grails and am trying them out in my spare time. I've got a small test Grails application that I'm able to run fine using grails run-app, but grails run-war results in an error.
In the grails-app/conf/BootStrip.init method, I'm adding some property getters onto the DefaultGrailsControllerClass and DefaultGrailsApplication:
DefaultGrailsControllerClass.metaClass.getMenuText = { ->
getPropertyOrStaticPropertyOrFieldValue('menuText', String.class)
}
DefaultGrailsControllerClass.metaClass.getMenuOrder = { ->
getPropertyOrStaticPropertyOrFieldValue('menuOrder', Integer.class)
}
DefaultGrailsApplication.metaClass.getMenuControllerClasses = { ->
controllerClasses.findAll { it.menuText != null }.sort { it.menuOrder }
}
In my grails-app/views/layouts/main.gsp, I'm using this:
<g:each var="c" in="${ grailsApplication.menuControllerClasses }">
<li><g:link controller="${c.logicalPropertyName}">${c.menuText}</g:link></li>
</g:each>
This works fine under run-app, but running it under run-war, I get the following:
groovy.lang.MissingPropertyException: No such property: menuControllerClasses for class: org.codehaus.groovy.grails.commons.DefaultGrailsApplication
I've tried this under Grails 1.1.1 and 1.2-M1 and get the same result. I've verified that the BootStrap.init method is being called (via a println), but the changes made to the metaClass don't appear to take under run-war.
Any idea what I'm missing?

grails run-war -- Run's a Grails application's WAR in Jetty
grails run-app -- Runs a Grails application in Jetty
The difference seems to be that run-war does not support reloading as run-app does.
Not sure what you are missing.

Related

Register custom constraints

I'm trying to upgrade Grails 2.3.7 project to Grails 3.2.3. In 2.3.7, I used custom constraints and register them in /conf/Config.groovy using:
org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)
Then I can use something like this in domain:
static constraints = {
approvedDate(description: '>= applyDate')
}
However, in Grails 3.2.3, When I put above command (and remove org.codehaus.groovy from package name) in /conf/application.groovy I got following error:
Error occurred running Grails CLI: No signature of method: groovy.util.ConfigObject.registerNewConstraint() is applicable for argument types: (groovy.util.ConfigObject, groovy.util.ConfigObject) values: [[:], [DESCRIPTION_CONSTRAINT:[:]]]
I've notice that validation class is somewhat changed in Grails 3. However using constraint class from Grails-validation still got the same error.
All validation plugins I found were long abandoned before Grails 3. And I can't find any document for register new constraint in Grails 3.2.
Calling ConstrainedProperty.registerNewConstraint in /grails-app/init/BootStrap.groovy works. (tested with Grails 3.2.4)
class BootStrap {
def init = { servletContext ->
grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)
// The rest of bootstrap code
}
}
Note. Previously, I call it from main() in /grails-app/init/Application.groovy. It works for running application. However, it does not work with integration test.
Another way you can create the runtime.groovy under config and register your constraints in the runtime.groovy as in grails 2.x.x:
org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)

Grails access variable in build.gradle from Service class

I am sitting in front of a Grails 3.1.4 Application. The build.gradle file looks like this:
buildscript {
...
}
version "01.01.12.99"
...
ext {
...
}
repositories {
...
}
dependencyManagement {
...
}
dependencies {
...
}
From one of my Service Classes I want to access the version variable. Is this somehow built in into Grails or am I trying to do something impossible? Are there ways to load this variable from a Service class?
I searched a bit and found this, but it accesses the Grails version not the project version.
Well, I recently started using Grails 3. Here I got the solution:
println System.properties.getProperty("info.app.version")
And you will get "01.01.12.99". This is working on Grails 3.1.8 for me.
Another way to achieve this (in addition to the solution provided by Shashank Agrawal) is relying on grailsApplication bean, with the following code:
grailsApplication.config.getProperty('info.app.version')
I tested this with Grails 3.1.9 and 3.2.7.

doWithSpring in Grails 3 plugin as a subproject

I have a project built with Gradle with 3 subprojects: trawler, adaptrice, frontage. Trawler is Groovy/Java, adaptrice is a Grails 3.1.7 app, frontage is a Grails 3 plugin used by adaptrice.
The top settings.gradle says include 'trawler', 'adaptrice', 'frontage', adaptrice/build.gradle contains,
grails {
plugins {
compile project(':frontage')
}
}
All this as per the Grails documentation. Everything works -- almost. The doWithSpring method in the plugin descriptor (class FrontageGrailsPlugin) looks like this,
Closure doWithSpring() { {->
entityPropGrabberFactory(EntityPropGrabberFactory) {
constraintsEvaluator = ref(ConstraintsEvaluator.BEAN_NAME)
proxyHandler = ref('proxyHandler')
}
}}
It is never executed when I do run-app for the adaptrice Grails app. There is a NPE when a supposedly injected bean is used. If I move the body of doWithSpring to the .../spring/resources.groovy of the adaptrice Grails app then it really works.
So my question is, what causes the doWithSpring in a Grails 3 plugin descriptor to be executed? Do I have to do something special when the plugin also is a subproject?

Grails 3.0.1 and Hibernate Session error

I am following a book example (Grails in Action 2nd edition) which is based on Grails 2.* but I am using the new Grails 3.0.1.
When I create a domain class that looks like:
package qotd
class Quote {
String content
String author
Date created = new Date()
}
I get an Exception thrown whenever I try to interact with the DB through the groovy console.
org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session;
Caused by: org.hibernate.HibernateException: No Session found for current thread
I have tried to add #Transactional to the domain class and also swith to a lower JDK version(7) but none of them works.
I have also tested with Grails 3.0 and results are same.
If I downgrade to Grails 2.5.0 it works so it is a Grails 3.* issue. Gradle could be the issue.
I wrapped everything inside a transaction for Grails 3
qotd.Quote.withTransaction {
new qotd.Quote(author: 'Larry Wall',
content: 'There is more than one method to our madness.').save()
}

Grails 2.2 not recognizing homepage view

I'm upgrading a grails 1.3.6 app to 2.2.4. In 1.3.6, the app is accessed by hitting localhost:8080/APPNAME. This then renders the file in grails-app/views/index.gsp.
After upgrading the app to 2.2.4, hitting localhost:8080/APPNAME' returns an error of :
HTTP Status 404 - "/index.gsp" not found..
My URLMappings file is as follows:
class UrlMappings
{
static mappings =
{
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
//"500"(view:'/error')
"500"(controller: 'pacErrors', action: 'handleErrors')
}
}
I am able to access controllers directly via URL calls, so the server does appear to be up and running.
It all seems correct, sometimes when confronted with weird behaviours after an upgrade, I find the best fix is a "grails clean", and make sure that the app is indeed migrated to 2.2.4 by running "grails upgrade".
If this does not work, try creating a new application directly with grails 2.2.4 to test it out. If you cannot spot the problem after that, you could copy your existing app into the new one.

Resources