Size: 1983
Comment:
|
← Revision 21 as of 2025-05-10 11:57:58 ⇥
Size: 2430
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
<<TableOfContents(2)>> | |
Line 3: | Line 4: |
* http://groovy-lang.org/groovy-dev-kit.html * http://groovy-lang.org/objectorientation.html |
* https://groovy-lang.org/groovy-dev-kit.html * https://groovy-lang.org/objectorientation.html |
Line 8: | Line 9: |
{{{#!highlight bash wget https://dl.bintray.com/groovy/maven/apache-groovy-binary-3.0.3.zip cp apache-groovy-binary-3.0.3.zip ~/ |
{{{#!highlight sh |
Line 12: | Line 11: |
unzip apache-groovy-binary-3.0.3.zip | wget https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-binary-4.0.26.zip unzip apache-groovy-binary-4.0.26.zip |
Line 14: | Line 15: |
# export GROOVY_HOME=$HOME/groovy-3.0.3 # PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/jdk-11.0.2/bin:/home/user/scripts # PATH=$PATH:/opt/mssql-tools/bin/:$GROOVY_HOME/bin # export PATH |
# export GROOVY_HOME=$HOME/groovy-4.0.26 # export PATH=$PATH:$GROOVY_HOME/bin |
Line 20: | Line 19: |
# print("aaa") groovyConsole |
# a = 1 # print("aaa $a") # :q groovyConsole # opens window that allows to run groovy code |
Line 37: | Line 38: |
groovyc MyClass.groovy java -cp . -jar $GROOVY_HOME/lib/groovy-3.0.3.jar MyClass java -jar $GROOVY_HOME/lib/groovy-3.0.3.jar MyClass |
groovyc MyClass.groovy # compile java -cp . -jar $GROOVY_HOME/lib/groovy-4.0.26.jar MyClass # run code java -jar $GROOVY_HOME/lib/groovy-4.0.26.jar MyClass # run code |
Line 70: | Line 71: |
groovyc swing.groovy java -cp .:$GROOVY_HOME/lib/groovy-3.0.3.jar:$GROOVY_HOME/lib/groovy-swing-3.0.3.jar swing |
groovy swing.groovy # run with groovy groovyc swing.groovy # compile code # run compiled code with java java -cp .:$GROOVY_HOME/lib/groovy-4.0.26.jar:$GROOVY_HOME/lib/groovy-swing-4.0.26.jar swing |
Line 73: | Line 76: |
== Map example - map.groovy == {{{#!highlight groovy /* run: groovy map.groovy compile and run with java groovyc map.groovy java -cp .:$GROOVY_HOME/lib/groovy-4.0.26.jar map */ def map=[:] map.x = 1 map["y"] = "ab" print("Serialized map $map \n") print("Keys size: ${map.keySet().size()} \n") for(key in map.keySet()){ print "$key: ${map["$key"]}\n" } }}} |
Groovy
Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax
Install
1 cd ~/
2 wget https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-binary-4.0.26.zip
3 unzip apache-groovy-binary-4.0.26.zip
4
5 nano ~/.bashrc
6 # export GROOVY_HOME=$HOME/groovy-4.0.26
7 # export PATH=$PATH:$GROOVY_HOME/bin
8 . ~/.bashrc
9 groovysh
10 # a = 1
11 # print("aaa $a")
12 # :q
13 groovyConsole # opens window that allows to run groovy code
14
groovyc - compiler
nano MyClass.groovy
swing - groovy
1 import groovy.swing.SwingBuilder
2 import java.awt.BorderLayout as BL
3
4 count = 0
5
6 def actionPerformedFn(){
7 count++;
8 textlabel.text = "Clicked ${count} time(s)."
9 println "clicked"
10 }
11
12 def quitFn(){
13 System.exit(0)
14 }
15
16 new SwingBuilder().edt {
17 frame(title: 'Frame', size: [300, 300], show: true) {
18 borderLayout()
19 textlabel = label(text: 'Click the button!', constraints: BL.NORTH)
20 button(text:'Click Me', actionPerformed: {actionPerformedFn()}, constraints:BL.WEST)
21 button(text:'Quit', actionPerformed: {quitFn()}, constraints:BL.EAST)
22 }
23 }
Map example - map.groovy
1 /*
2 run: groovy map.groovy
3 compile and run with java
4 groovyc map.groovy
5 java -cp .:$GROOVY_HOME/lib/groovy-4.0.26.jar map
6 */
7 def map=[:]
8 map.x = 1
9 map["y"] = "ab"
10 print("Serialized map $map \n")
11
12 print("Keys size: ${map.keySet().size()} \n")
13 for(key in map.keySet()){
14 print "$key: ${map["$key"]}\n"
15 }