Gradle / Grails application - grails

I have been trying to configure Gradle to manage a Grails project for a couple of hours with no success. The suggestions I've found here on Stack Overflow and elsewhere on the Internet didn't work for me.
Could you please give me up-to-date directions on how to configure a Gradle+Grails project? Ideally it should relate to the current versions of Grails (2.1.0) and Gradle (1.1).

BuildScript
import org.grails.gradle.plugin.GrailsTask
buildscript {
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
dependencies {
classpath "org.grails:grails-gradle-plugin:1.1.1-SNAPSHOT"
}
}
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
version = "1.0"
grailsVersion = "2.1.0"
apply plugin: "grails"
dependencies {
['dependencies', 'resources', 'core', 'hibernate', 'plugin-datasource', 'plugin-domain-class', 'plugin-tomcat', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.1.0"
}
bootstrap "org.codehaus.groovy:groovy-all:1.8.6"
}
GRAILS_TASK_PREFIX = "grails-"
if (name.startsWith(GRAILS_TASK_PREFIX)) {
project.task(name, type: GrailsTask) {
command "${name - GRAILS_TASK_PREFIX}"
}
}
Initialize
Then you can do gradle init to initialize the project structure
Commands
Use gradle grails-[grails script] to execute your grails commands. For example: gradle grails-run-app is equivalent to grails run-app
Hope this helps!
Update
This seems to work for Grails 2.3.2:
buildscript {
repositories {
mavenCentral()
maven { url 'http://repo.grails.org/grails/repo' }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0-SNAPSHOT"
}
}
repositories {
mavenCentral()
maven { url 'http://repo.grails.org/grails/repo' }
}
version = "1.0"
apply plugin: "grails"
apply plugin: 'idea'
grails {
grailsVersion = "2.3.2"
}
dependencies {
['dependencies', 'core', 'spring', 'web', 'plugin-datasource', 'plugin-domain-class', 'plugin-controllers', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.3.2"
}
compile 'org.grails.plugins:tomcat:7.0.42'
compile 'org.grails.plugins:hibernate:3.6.10.3'
compile 'com.h2database:h2:1.3.173'
bootstrap "org.codehaus.groovy:groovy-all:2.1.9"
}
A note, throws Exception when generating Grails wrapper, but gradle init seems to initialize the project structure properly otherwise. And, gradle grails-run-app seems to work fine too.

Related

Grails 3 : console plugin not coming up in production mode

Grails console plugin page is not coming up nothing gets rendered on the UI only when application is run on production environment either via run-app or as a war file deployed on embedded tomcat.
Grails version - 3.2.4
build.gradle looks like this:
buildscript {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
....
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.13.1"
...
}
}
version "0.1"
apply plugin: "asset-pipeline"
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
maven { url "http://repo1.maven.org/maven2" }
}
dependencies {
....
runtime 'org.grails.plugins:grails-console:2.0.9'
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.13.1"
....
}
Any thoughts whats going wrong here?
By default the plugin is disabled in production for obvious reasons (an attacker could do anything to your app) - but if you understand the risk and secure it you can re-enable it. Checkout the documentation here https://github.com/sheehan/grails-console#security

Spring security core framework getting configured twice in Grails Spring security core plugin

I am using Grails spring security core plugin version 3.0.3.
The debug statements when configuring the spring security core framework are printed twice and the filter chain is also initialized twice
WARN grails.plugin.springsecurity.SpringSecurityCoreGrailsPlugin -
Configuring Spring Security Core ...
Configuring Spring Security Core ...
WARN grails.plugin.springsecurity.SpringSecurityCoreGrailsPlugin - ... finished
configuring Spring Security Core
... finished configuring Spring Security Core
Build gradle file
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
}
version "0.1"
group "restservicesapp"
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
provided "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
runtime "mysql:mysql-connector-java:5.1.38"
compile 'org.grails.plugins:spring-security-core:3.0.3'
compile ('org.grails.plugins:spring-security-rest-gorm:2.0.0.M2') {
exclude group: 'org.grails.plugins', module: 'spring-security-core'
}
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
//console "org.grails:grails-console"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
Do you have org.grails.plugins:cxf into your build.gradle ? Possibly two context are being created. One for your main app and other for your /services/*. Move the cfx dependency in gradle file above spring security plugin and then you should see spring security being configured once only. I have been struggled with this more then 2 weeks now. But this solved this issue for me. For me it actually was an issue as the spring security being configured second time it was giving my NPE at times. See this question from myself only.
Update
My above assessment proved wrong. The real solution is, add below snippet to your build.gradle
configurations.runtime {
exclude module: "cxf"
}
I believe Spring Security is not being configured twice. One line of output is from logging, the other is a println.
Below is some code from grails.plugin.springsecurity.SpringSecurityCoreGrailsPlugin:
Closure doWithSpring() {{ ->
ReflectionUtils.application = SpringSecurityUtils.application = grailsApplication
SpringSecurityUtils.resetSecurityConfig()
def conf = SpringSecurityUtils.securityConfig
boolean printStatusMessages = (conf.printStatusMessages instanceof Boolean) ? conf.printStatusMessages : true
if (!conf || !conf.active) {
if (printStatusMessages) {
String message = '\n\nSpring Security is disabled, not loading\n\n'
log.warn message
println message
}
return
}
log.trace 'doWithSpring'
if (printStatusMessages) {
String message = '\nConfiguring Spring Security Core ...'
log.warn message
println message
}

Grails 3.0 and Spring Security Error

In my build.gradle I added this line of code
dependencies {
compile "org.grails.plugins:spring-security-core:3.0.3"
}
Then when I try to use it
import grails.plugin.springsecurity.annotation.Secured
#Secured('ROLE_ADMIN')
class SecureController {
def index() {
render 'Secure access only'
}
It "cannot resolve the symbol springsecurity"
I get the error
Error:(5, 1) Groovyc: unable to resolve class Secured , unable to find class for annotation
Any help would be much appreciated.
Build.gradle **** EDIT
This is the whole build.gradle file
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.4.RELEASE"
}
version "0.1"
group "securityrolesspring"
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
assets {
minifyJs = true
minifyCss = true
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.grails.plugins:spring-security-core:3.0.3"
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:scaffolding"
runtime "org.grails.plugins:asset-pipeline"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'
console "org.grails:grails-console"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
after 'apply plugin: "war"' add a line that says:
apply plugin: "maven"
in your repositories section, after "mavenLocal()" add a line that says:
mavenCentral()
this last recommendation is just cosmetic, but I would move your line:
compile "org.grails.plugins:spring-security-core:3.0.3"
and place it after the
compile "org.grails.plugins:scaffolding" line. it's good form to keep the initial set of grails dependencies pretty much as they are from the initial 'grails create-app' command. the exception to this rule is if you want to replace the logback logging with something else, but that's another story.
finally, you could also change your spring-security-core dependency to version 3.0.4 which is out now.
Spring security core has been updated the package location for #Secured Annotations. I use Grails 3.2.3 and Spring Security Core 3.1.1 int my project and works well. The following is the configuration the I used and is working as was in previos version of spring security core and Grails (2.X).
For installation, you have to add the entry in the dependencies block if your build.gradle file. Details in the oficial documentation for installation section for Spring Security Core
dependencies {
...
compile 'org.grails.plugins:spring-security-core:3.1.1'
...
To use the secured annotations in your controller, there were updates on the import (org.springframework.security.access.annotation.Secured) package and the #Secure annotation you have to assign to "value" the security statement you want to do. For example the annotation for #Secured('ROLE_ADMIN'), have to change like in the following example.
import org.springframework.security.access.annotation.Secured
#Secured(value=["hasRole('ROLE_ADMIN')"])
class SecureController {
def index() {
render 'Secure access only'
}
Detailed configuration are in the oficial documentation page, updates for secured annotation in version 3, and how to define secured annotations.
By importing
import grails.plugin.springsecurity.annotation.Secured
you will be able to resolve your problem.
It will again say that it cannot resolve symbol "Secured" but you need to ignore it because the code will run completely according to your expectation.

Grails gradle: How to set System Property

May I know why I'm unable to set the System Property with the systemProperty method when using the grails-gradle-plugin?
My build.gradle as follows:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0"
}
}
version "0.1"
group "example"
apply plugin: "grails"
repositories {
grails.central() //creates a maven repo for the Grails Central repository (Core libraries and plugins)
}
grails {
grailsVersion = '2.3.5'
groovyVersion = '2.1.9'
springLoadedVersion '1.1.3'
}
dependencies {
bootstrap "org.grails.plugins:tomcat:7.0.50" // No container is deployed by default, so add this
compile 'org.grails.plugins:resources:1.2' // Just an example of adding a Grails plugin
}
test {
println "I'm in the test"
//Could not find method systemProperty() for arguments [geb.env, sauce] on root project
systemProperty 'geb.env', 'sauce'//Fails
}
In the test task, I get the following error when i run $gradle grails-test:
Could not find method systemProperty() for arguments [geb.env, sauce] on root project..."
Is this a problem with the grails-gradle plugin since other plugins like "java" allows me to use setProperty? Thanks.
The error message is correct: there's no systemProperty() method.
As Joshua Moore comments, this should work:
test {
println "I'm in the test"
System.setProperty 'geb.env', 'sauce'
}

How to tell Grails to use Gradle for dependencies resolution

I'm trying to setup a grails project with gradle but i came a problem, i can't make grails to use gradle for dependencies resolution. If i config the dependencies in gradle build file and run gradle grails-run-app, it always report can't find classes in dependencies jars.
When i cut and paste the dependencies into grails BuildConfig.groovy and everything is ok.
How to tell Grails to use Gradle for dependencies resolution?
I paste my build.gradle file here, Any suggestion?
apply plugin: 'grails'
apply plugin: 'java'
apply plugin: 'jetty'
version "1.0-SNAPSHOT"
buildscript {
repositories {
mavenCentral()
mavenRepo urls: 'http://repository.jboss.org/nexus/content/groups/public/'
}
dependencies {
classpath 'com.connorgarvey.gradle:gradle-grails-wrapper:1.0'
}
}
grails {
version '2.2.3'
}
repositories {
mavenLocal()
mavenCentral()
mavenRepo urls: 'http://repository.jboss.org/nexus/content/groups/public/'
}
dependencies {
compile 'org.modeshape.bom:modeshape-bom-embedded:3.3.0.Final'
compile 'postgresql:postgresql:9.1-901.jdbc4'
compile 'javax.jcr:jcr:2.0'
compile 'org.modeshape:modeshape-jcr:3.3.0.Final'
}
I would recommend using the grails-gradle-plugin instead.
UPDATED ANSWER, cleanup and usage of bootstrap scope to exclude Tomcat jars from war.
General info
I followed a presentation from Luke Daley (aka alkemist) on Youtube at gr8conf 2013. I was able to create a small POC and Gradle seems to work fine with Grails 2.2.3.
Gradle build file
buildscript {
repositories {
mavenCentral()
maven { url 'http://repository.jboss.org/maven2/' }
maven { url 'http://repo.grails.org/grails/repo' }
maven { url 'http://repo.grails.org/grails/plugins' }
maven { url 'http://repository.springsource.com/maven/bundles/release' }
maven { url 'http://repository.springsource.com/maven/bundles/external' }
maven { url 'http://repository.springsource.com/maven/libraries/release' }
maven { url 'http://repository.springsource.com/maven/libraries/external' }
}
dependencies {
classpath 'org.grails:grails-gradle-plugin:2.0.0-SNAPSHOT',
'org.grails:grails-bootstrap:2.2.3'
}
}
version='0.0.1'
apply plugin: 'grails'
repositories {
mavenCentral()
maven { url 'http://repository.jboss.org/maven2/' }
maven { url 'http://repo.grails.org/grails/repo' }
maven { url 'http://repo.grails.org/grails/plugins' }
maven { url 'http://repository.springsource.com/maven/bundles/release' }
maven { url 'http://repository.springsource.com/maven/bundles/external' }
maven { url 'http://repository.springsource.com/maven/libraries/release' }
maven { url 'http://repository.springsource.com/maven/libraries/external' }
}
grails {
grailsVersion '2.2.3'
version '2.2.3'
}
configurations {
all {
exclude module: 'commons-logging'
exclude module: 'xml-apis'
}
test {
exclude module: 'groovy-all'
}
compile {
exclude module: 'hibernate'
}
}
dependencies {
compile( "org.grails:grails-crud:$grails.grailsVersion",
'org.grails:grails-gorm:1.3.7')
bootstrap "org.grails:grails-plugin-tomcat:$grails.grailsVersion"
}
Send a text.... ;) Kidding.
You must include a version of the 'grails-bootstrap' artifact in the
'classpath' configuration. You should also add whichever Grails
artifacts you need. 'grails-crud' and 'grails-gorm' will give you
everything you need for a standard Grails web application.
Have a look at the plugin docs.

Resources