DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > Grails Goodness: Run Groovy Scripts in Grails Context

Grails Goodness: Run Groovy Scripts in Grails Context

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
May. 22, 14 · Performance Zone · Interview
Like (0)
Save
Tweet
8.40K Views

Join the DZone community and get the full member experience.

Join For Free

We can use the run-script command to run Groovy scripts within the context of a Grails application. We can pass one or more Groovy scripts as argument to the run-script command. The Grails environment will be configured and we can access the Spring application context, domain classes, Grails services and more. Basically everything we can do in the Grails console or shell can be saved as a Groovy script and run with the run-script command.

The following Groovy script shows some stats for a Grails application:

// File: src/scripts/appstatus.groovy
import grails.util.Environment
import static grails.util.Metadata.current as metaInfo

header 'Application Status'
row 'App version', metaInfo['app.version']
row 'Grails version', metaInfo['app.grails.version']
row 'Groovy version', GroovySystem.version
row 'JVM version', System.getProperty('java.version')
row 'Reloading active', Environment.reloadingAgentEnabled
row 'Controllers', grailsApplication.controllerClasses.size()
row 'Domains', grailsApplication.domainClasses.size()
row 'Services', grailsApplication.serviceClasses.size()
row 'Tag Libraries', grailsApplication.tagLibClasses.size()
println()

header 'Installed Plugins'
ctx.getBean('pluginManager').allPlugins.each { plugin ->
    row plugin.name, plugin.version
}

void row(final String label, final value) {
    println label.padRight(18) + ' : ' + value.toString().padLeft(8)
}

void header(final String title) {
    final int length = 29
    println '-' * length
    println title.center(length)
    println '-' * length
}

We can invoke the script with the following command:

$ grails run-script src/scripts/appstatus.groovy
| Running script src/scripts/appstatus.groovy ...
-----------------------------
Application Status
-----------------------------
App version        :      0.1
Grails version     :    2.4.0
Groovy version     :    2.3.1
JVM version        : 1.7.0_51
Reloading active   :    false
Controllers        :        2
Domains            :        0
Services           :        3
Tag Libraries      :       15
 
-----------------------------
Installed Plugins
-----------------------------
i18n               :    2.4.0
logging            :    2.4.0
dataBinding        :    2.4.0
restResponder      :    2.4.0
core               :    2.4.0
codecs             :    2.4.0
urlMappings        :    2.4.0
jquery             :   1.11.1
databaseMigration  :    1.4.0
assetPipeline      :    1.8.7
webxml             :    1.4.1
tomcat             :   7.0.53
controllers        :    2.4.0
filters            :    2.4.0
servlets           :    2.4.0
mimeTypes          :    2.4.0
dataSource         :    2.4.0
groovyPages        :    2.4.0
domainClass        :    2.4.0
controllersAsync   :    2.4.0
converters         :    2.4.0
scaffolding        :    2.1.0
hibernate4         :  4.3.5.3
validation         :    2.4.0
services           :    2.4.0
cache              :    1.1.6
| Script src/scripts/appstatus.groovy complete!
$

Code written with Grails 2.4.0.

Grail (web browser) Groovy (programming language)

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Your Old Laptop Is Your New Database Server
  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations
  • Refactoring Java Application: Object-Oriented And Functional Approaches

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo