Date post: | 02-Sep-2018 |
Category: |
Documents |
Upload: | truongkhanh |
View: | 223 times |
Download: | 0 times |
Scripting for the JVM using Groovy
Adil Khan
Sr. Application Developer /Java Group
Biomedical Informatics
Outline
• What is Groovy?
Outline
• What is Groovy?
• Why would we want to use it?
Outline
• What is Groovy?
• Why would we want to use it?
• A taste of the features.
Outline
• What is Groovy?
• Why would we want to use it?
• A taste of the features.
• Some examples in the BMI world.
Outline
• What is Groovy?
• Why would we want to use it?
• A taste of the features.
• Some examples in the BMI world.
• Other Features
Outline
What is Groovy?
• Released in 2003 ~ Java 1.4
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
• Great for Designing Domain Specific Languages (DSL).
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
• Great for Designing Domain Specific Languages (DSL).
• Seamless Integration with Java. (Groovy is Java)
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
• Great for Designing Domain Specific Languages (DSL).
• Seamless Integration with Java. (Groovy is Java)
• Natural for Java Developers /Access to all java libraries.
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
• Great for Designing Domain Specific Languages (DSL).
• Seamless Integration with Java. (Groovy is Java)
• Natural for Java Developers /Access to all java libraries.
• Natural for Scripters who want to learn/use Java.
What is Groovy?
• Released in 2003 ~ Java 1.4
• Scripting language that is a Superset of Java.
• Great for Designing Domain Specific Languages (DSL).
• Seamless Integration with Java. (Groovy is Java)
• Natural for Java Developers /Access to all java libraries.
• Natural for Scripters who want to learn/use Java.
• Potential avenue for non-java developers who need access to java api/libraries
What is Groovy?
• Numerous ways to run groovy.
• Interactive console like Ruby, Python
• Shell Scripts: #!/usr/bin/env groovy
• GroovyShell Helper class
• java groovy.lang.GroovyShell path_to/MyScript.groovy
• Plugins for Eclipse/Netbeans etc.
• The designers goals is to have a language that gets out
of the way and allows you to be productive.
What is Groovy (Ctd.)
• Hello World Java:
• Hello World Groovy:
Note lack of parenthesis, semicolons, modifiers and
Typing.
A Taste
• Java (6) Print contents of a file:
• With Groovy:
More useful Example
• Java (6) Print contents of a file:
• With Groovy:
More useful Example
• java.io.File class with a Groovy Nature.
• Groovy ‘enhances’ existing java Objects.
• Use of closures /Functional programming.
• A more java like example:
More useful Example (Ctd)
Baked in Features
A quick overview of some basic built in features.
Baked in Features
A quick overview of some basic built in features.
• Collections
Baked in Features
A quick overview of some basic built in features.
• Collections
• {G}Strings
Baked in Features
A quick overview of some basic built in features.
• Collections
• {G}Strings
• Templating
Baked in Features
A quick overview of some basic built in features.
• Collections
• {G}Strings
• Templating
• Switch
Baked in Features
A quick overview of some basic built in features.
• Collections
• {G}Strings
• Templating
• Switch
• Builders / Slurpers
Baked in Features
• Lists :
Collections
• Lists :
Collections
• Maps :
Collections (Ctd)
• Maps :
Collections (Ctd)
• Range – Using the ‘..’ operator
Collections (Ctd)
• Range – Using the ‘..’ operator
Collections (Ctd)
• Powerful extensions to the java.lang.String class.
• Evaluate expressions and here documents (“””).
{G}Strings
• Powerful extensions to the java.lang.String class.
• Evaluate expressions and here documents (“””).
{G}Strings
• Templating engine allows for quick building of text. Good use case
of building sql statements, or any dynamic text.
Templating
• Templating engine allows for quick building of text. Good use case
of building sql statements, or any dynamic text.
Templating
• Templating engine allows for quick building of text. Good use case
of building sql statements, or any dynamic text.
Templating
• Can use any object in the condition
Switch
• Can use any object in the condition
Switch
• Can use any object in the condition
Switch
• Built in ability to generate/parse JSON/XML
• Declarative Nature of groovy simplifies the task.
Builders
• Built in ability to generate/parse JSON/XML
• Declarative Nature of groovy simplifies the task.
Builders
• Built in ability to generate/parse JSON/XML
• Declarative Nature of groovy simplifies the task.
Builders
Using MarkupBuilder to generate XML
Builders (Ctd)
Using MarkupBuilder to generate XML
Builders (Ctd)
Using MarkupBuilder to generate XML
Builders (Ctd)
• Using the ‘Slurpers’ to build an object graph from
Json/XML
Slurpers
• Xml example.
Slurpers (Ctd)
Potential Uses for Java Group
• Testing : unit/integration
• Ease of parsing/generating xml/json is very useful in
integration tests.
Potential Uses for Java Group
• Testing : unit/integration
• Ease of parsing/generating xml/json is very useful in
integration tests.
• Simplifying ‘Utils’ classes.
Potential Uses for Java Group
• Testing : unit/integration
• Ease of parsing/generating xml/json is very useful in
integration tests.
• Simplifying ‘Utils’ classes.
• Build/Deployment scripts.
Potential Uses for Java Group
• Testing : unit/integration
• Ease of parsing/generating xml/json is very useful in
integration tests.
• Simplifying ‘Utils’ classes.
• Build/Deployment scripts.
• System Administration
Potential Uses for Java Group
• Testing : unit/integration
• Ease of parsing/generating xml/json is very useful in
integration tests.
• Simplifying ‘Utils’ classes.
• Build/Deployment scripts.
• System Administration
• DSLs
Potential Uses for Java Group
• Creating Mock/Stub objects that simulate
request/response for unit testing.
• Requirements specific testing using frameworks such
as Spock (BDD)
• Unit Testing baked in groovy.util.GroovyTestCase
• Use case of using groovy constructs to simplify
loops/parameters to java code.
Testing Java with Groovy
Simplifying ‘Utils’ Classes
Simplifying ‘Utils’ Classes
Simplifying ‘Utils’ Classes
Simplifying ‘Utils’ Classes
Simplifying ‘Utils’ Classes
• Invoke Groovy Scripts in Ant using the <groovy> tag.
• Use Groovy to monitor tomcat/application status *.
Build/Monitor Scripts
• Invoke Groovy Scripts in Ant using the <groovy> tag.
• Use Groovy to monitor tomcat/application status *.
Build/Monitor Scripts
• Invoke Groovy Scripts in Ant using the <groovy> tag.
• Use Groovy to monitor tomcat/application status *.
Build/Monitor Scripts
* http://blog.techstacks.com/2009/05/tomcat-management-use-groovy-to.html
• DSLs-Powerful feature of Groovy to build specific
languages for specific tasks.
• Web Application Frameworks like Grails could be useful
for rapid prototyping of ideas.
• Developers with scripting/non-java background can get
easier access to existing java code base.
Other Topics to Research
Q/A