What classspath is used for executing Grails' application.groovy - grails

What classspath is used for compiling/executing Grails' application.groovy?
In my application.groovy, I instantiate a custom class (contained in a dependency's jar) and assign it to one of the config properties, like so:
environments {
production {
configProperty = new com.example.CustomClass()
I recently upgraded my application from Grails 3.1.5 to 3.2.2, and now this no longer works.
I receive an error like the following when I try to run grails run-app:
Error occurred running Grails CLI: startup failed:
script14788250424471597489853.groovy: 43: unable to resolve class com.example.CustomClass
# line 43, column 33.
configProperty = new com.example.CustomClass()
(Notice that the code is in the production block, but I'm running in development (run-app). That makes me think it's the compilation of this script that is failing.)
So I'm guessing I just need to add my dependency (that contains the CustomClass) to the appropriate classpath, but I'm not sure which one.
I'm using gradle, and have the following in my build.gradle file, to pull in the dependency containing CustomClass:
buildscript {
dependencies {
classpath "com.example:custom-module:1.1"
// ...
dependencies {
compile group: 'com.example', name: 'custom-module', version:'1.1'
}

The grails-app/conf/application.groovy file shouldn't reference application classes because it is read before compilation. If you wish to reference application classes in configuration please use grails-app/conf/runtime.groovy

Related

getting grails 2.5.4 asset pipeline excludes to work

I am unable to get either the sass asset pipeline or the less asset pipeline plugin to work, if I am using sass or less imports.
If I only have one sass or less file, grail war works fine.
In my scenario however, file1.scss imports file2.scss, and in the end I only want to end up with a file1.css created from both. This should be possible according to this
https://grails.org/plugin/sass-asset-pipeline
But I am getting this output
| Minifying File 345 of 398 - file2.
| Error WAR packaging error: Stream closed
from grails war --stacktrace --verbose
Both files are in the same directory and have teh following contents:
file1.scss
#import "file2";
.brand {background-color:$mainColor;}
file2.scss
$mainColor: #277D85;
My build config has this:
plugins = {
...
compile ':asset-pipeline:2.5.7'
provided ":sass-asset-pipeline:2.9.1"
}
grails.assets.minifyJs = false
grails.assets.minifyCss = false
grails.assets.minifyOptions = [
optimizationLevel: 'WHITESPACE_ONLY'
]
grails.assets.excludes = ["**/file2.scss"]
What am I doing wrong?
How can I get more insights into what's going on here.
Your plugin block should remain in BuildConfig.groovy. However, the rest should be placed in Config.groovy

Angular - Error: [$injector:unpr] Unknown provider: IdleProvider

Getting the error Error: [$injector:unpr] Unknown provider: IdleProvider in my application when it is deployed to our staging server using dokku but I am not getting it when running it on my local machine. I'm using ng-idle 1.2.1
I've found this question asked a number of times but the cause was always related to the changes made in version 1.0.0 where the service names were changed. The only thing I can think of is that the minification of the code is the problem but as far as I can see the code should be ok but I am not an expert. Any help would be greatly appreciated.
It's written in Coffeescript
configuration = (RestangularProvider, $logProvider, growlProvider, IdleProvider, KeepaliveProvider) ->
.
.
.
return
configuration.$inject = [
'RestangularProvider'
'$logProvider'
'growlProvider'
'IdleProvider'
'KeepaliveProvider'
]
angular
.module 'vssApp.config', [
'restangular'
]
.config configuration
EDIT
While trying to replicate the problem on my local machine I removed the 'ngIdle' module in the modules array below. This resulted in the same behavior so I am assuming that the problem stems from the ngIdle module not being loaded correctly here. I still feel that minification could be causing the problem but, again, I'm not sure why or how to fix it.
modules = [
'ui.router'
'ui.bootstrap'
'ui.select'
'ngAnimate'
'ngMessages'
'ngSanitize'
'ngCookies'
'smart-table'
'angularMoment'
'templates'
'angular-storage'
'angular-growl'
'vssApp.core.auth'
'vssApp.core.loading'
'ngIdle'
'cgPrompt'
'vssApp.filters'
]
runBlock.$inject = [
'$templateCache'
]
angular
.module 'vssApp.core', modules
.run runBlock
EDIT 2
Here's the full output from the error message I'm getting
Error: [$injector:modulerr] Failed to instantiate module vssApp due to:
Error: [$injector:modulerr] Failed to instantiate module vssApp.config due to:
Error: [$injector:unpr] Unknown provider: IdleProvider
http://errors.angularjs.org/1.3.16/$injector/unpr?p0=IdleProvider
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:18814
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:16489
at getService (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:14903)
at Object.invoke (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:15466)
at runInvokeQueue (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13793)
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:14062
at forEach (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:19482)
at loadModules (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13587)
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13964
at forEach (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:19482)
http://errors.angularjs.org/1.3.16/$injector/modulerr?p0=vssApp.config&p1=E…net%2Fassets%2Fapplication-85a5fd382c73380bf2a71b66e581c941.js%3A3%3A19482)
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:18814
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:14406
at forEach (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:19482)
at loadModules (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13587)
at https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13964
at forEach (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:19482)
at loadModules (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:13587)
at createInjector (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:4:16844)
at doBootstrap (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:28466)
at bootstrap (https://SERVER/assets/application-85a5fd382c73380bf2a71b66e581c941.js:3:28995)
http://errors.angularjs.org/1.3.16/$injector/modulerr?p0=vssApp&p1=Error%3A…net%2Fassets%2Fapplication-85a5fd382c73380bf2a71b66e581c941.js%3A3%3A28995)
A rule of thumb is to load modules dependencies in each place where they are used. This allows to decouple them. And this eliminates race condition with service provider injection.
If the app looks like this
angular.module('vssApp', ['vssApp.config', 'ngIdle', ...])..
angular.module('vssApp.config', ['restangular'])...
service provider for Idle service is not defined at the time when vssApp.config module is loaded.
While this
angular.module('vssApp', ['ngIdle', 'vssApp.config', ...])
angular.module('vssApp.config', ['restangular'])...
avoids race condition but still indicates code smell.
It should be
angular.module('vssApp.config', ['restangular', 'ngIdle'])...
This issue applies to service providers only and config phase. Service instances can be injected for any module order.
Finally found the cause and solution to this, it seems to have been a bower issue.
It's a Rails app, so I specified ng-idle 1.2.1 in the bower.json file but for some reason the bower file was ignored when the app was being deployed using Dokku and the last installed version 0.3.5 remained, which meant that the pre-1.0.0 ng-idle services naming convention was still being used where all service names were preceded with a $. This resulted in the Unknown provider: IdleProvider error because $IdleProvider was the actual service name.
In the end I had to connect to the docker container and remove and reinstall all bower components. Running bower update as part of the deployment was not enough for some reason. When I have more time I will investigate what caused this behavior and I will report here.

External properties file in grails 3

I need read configuration from a external file properties in grails 3. In grails 2.x I link the file with:
grails.config.locations = ["classpath:config.properties"]
In the config.groovy, but this file do not exists in grails 3.
Have you any idea for solve?
Because Grails 3 is built on Spring Boot, you can use the Spring Boot mechanisms for externalized properties. Namely, using the spring.config.location command line parameter, or the SPRING_BOOT_LOCATION environment variable. Here's the Spring documentation page on it.
The example the documentation provides for the command line parameter is this:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
The way I have been using it is by setting an environment variable, like this:
export SPRING_CONFIG_LOCATION="/home/user/application-name/application.yml"
One of the nice features is that you can leave some properties in the properties file that is bundled in the app, but if there are some properties you do not want to include (such as passwords), those can be set specifically in the external config file.
Take a look at https://gist.github.com/reduardo7/d14ea1cd09108425e0f5ecc8d3d7fca0
External configuration in Grails 3
Working on Grails 3 I realized that I no longer can specify external configuration using the standard grails.config.locations property in Config.groovy file.
Reason is obvious! There is no Config.groovy now in Grails 3. Instead we now use application.yml to configure the properties. However, you can't specify the external configuration using this file too!
What the hack?
Now Grails 3 uses Spring's property source concept. To enable external config file to work we need to do something extra now.
Suppose I want to override some properties in application.yml file with my external configuration file.
E.g., from this:
dataSource:
username: sa
password:
driverClassName: "org.h2.Driver"
To this:
dataSource:
username: root
password: mysql
driverClassName: "com.mysql.jdbc.Driver"
First I need to place this file in application's root. E.g., I've following structure of my Grails 3 application with external configuration file app-config.yml in place:
[human#machine tmp]$ tree -L 1 myapp
myapp
├── app-config.yml // <---- external configuration file!
├── build.gradle
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── grails-app
└── src
Now, to enable reading this file just add following:
To your build.gradle file
bootRun {
// local.config.location is just a random name. You can use yours.
jvmArgs = ['-Dlocal.config.location=app-config.yml']
}
To your Application.groovy file
package com.mycompany
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.PropertiesPropertySource
import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.Resource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
public final static String LOCAL_CONFIG_LOCATION = "local.config.location"
static void main(String[] args) {
GrailsApp.run(Application, args)
}
#Override
void setEnvironment(Environment environment) {
String configPath = System.properties[LOCAL_CONFIG_LOCATION]
if (!configPath) {
throw new RuntimeException("Local configuration location variable is required: " + LOCAL_CONFIG_LOCATION)
}
File configFile = new File(configPath)
if (!configFile.exists()) {
throw new RuntimeException("Configuration file is required: " + configPath)
}
Resource resourceConfig = new FileSystemResource(configPath)
YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
ypfb.setResources([resourceConfig] as Resource[])
ypfb.afterPropertiesSet()
Properties properties = ypfb.getObject()
environment.propertySources.addFirst(new PropertiesPropertySource(LOCAL_CONFIG_LOCATION, properties))
}
}
Notice that Application class implements EnvironmentAware Interface and overrides its setEnvironment(Environment environment):void method.
Now this is all what you need to re-enable external config file in Grails 3 application.
Code and guidance is taken from following links with little modification:
http://grails.1312388.n4.nabble.com/Grails-3-External-config-td4658823.html
https://groups.google.com/forum/#!topic/grails-dev-discuss/_5VtFz4SpDY
Source: https://gist.github.com/ManvendraSK/8b166b47514ca817d36e
I am having the same problem to read the properties file from external location in Grails 3. I found this plugin which helpme to read the properties from external location. It has feature to read .yml, .groovy files as well.
Follow the steps mentioned in the documentation and it will work.
The steps are like:
Add dependency in build.gradle:
dependencies {compile 'org.grails.plugins:external-config:1.2.2'}
Add this in application.yml grails:
config:
locations:
- file:///opt/abc/application.yml
Create file at external location. In my case /opt/abc/application.yml.
Build the application and run.
You can use
def cfg = new ConfigSlurper.parse(getClass().classLoader.getResource('path/myExternalConfigfile.groovy'))
When running from a .jar file, I found that Spring Boot looks at the current directory for an application.yml file.
java -jar app-0.3.jar
In the current directory, I created an application.yml file with one line:
org.xyz.app.title: 'XYZZY'
I also used this file to specify the database and other such info.
Seems like there is no externalised config out of the box: http://grails.1312388.n4.nabble.com/Grails-3-External-config-td4658823.html

Grails and release plugin

I'm trying to use the release 2.0.4 plugin to deploy my war through grails 2.1.1 to artifactory server.
My BuildConfig.groovy has:
grails.project.repos.snap.url = "http://server:8080/artifactory/apps-snapshot-local"
grails.project.repos.snap.username = "user"
grails.project.repos.snap.password = "password"
grails.project.repos.rel.url = "http://server:8080/artifactory/apps-release-local"
grails.project.repos.rel.username = "user"
grails.project.repos.rel.password = "password"
grails.project.repos.default = "rel"
When I just do the "grails maven-deploy" it works and deploys to my rel server as expected. When I try to override the default target through the command line I get failures.
grails maven-deploy --repository=snap
I get this:
| Done creating WAR snap
| POM generated: C:\dev-git\DBUpdateWeb\target/pom.xml.
| Error Error deploying artifact: C:\dev-git\DBUpdateWeb\target\DBUpdateWeb.war (The system cannot find the file specified)
| Error Have you specified a configured repository to deploy to (--repository argument) or specified distributionManagement in your POM?
When I do specify the --repository tag it doesn't generate a war even though it says it did. Any help is appreciated. Thanks in advance.
Try grails maven-deploy "--repository=snap".
Also, specify app.version in application.properties so that the WAR will be standard compliant (1.0-SNAPSHOT for publishing to the snapshots repository and 1.0 for releases), and comment out the grails.project.war.file line in BuildConfig.groovy.

doWithDynamicMethods not being Called

When I create a production war (grails war --nojars) my doWithDynamicMethods is not being called in a production environment. The code works in the dev env (ie grails run-app)
Here is some of my code:
PDFFormsGrailsPlugin.groovy (in PDFForms (plugin) directory) :-
def doWithDynamicMethods = { ctx ->
println "Adding renderPDFForm to controller";
for (controllerClass in application.controllerClasses) {
updateControllers controllerClass.metaClass
}
}
BuildConfig.groovy (in Application using Plugin) :-
grails.plugin.location.'pdff-orms' = "../PDFForms"
We are using Grails 1.3.5 on Windows XP, The Build is called from STS 2.3.3.CI-R5462-B20
Thanks in advance.
Check if the plugin is referenced in your grails.xml.
Read this Nabble thread
And this jira
P.S. Why not you upgrade to grails 1.3.7 at least?!

Resources