Project

General

Profile

Groovy Add-on » History » Version 22

Henning Blohm, 24.02.2020 17:22

1 1 Udo Offermann
h1. Groovy Add-on
2 2 Udo Offermann
3 21 Henning Blohm
The Groovy Add-on allows to use the "Groovy language":http://groovy.codehaus.org/ within z2 applications. It supports pure Groovy modules as well as modules containing both Java and Groovy source code.
4 3 Udo Offermann
5 17 Henning Blohm
h2. Repository
6 1 Udo Offermann
7 17 Henning Blohm
"z2-addons-groovy":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-groovy
8
9
h2. Version map
10
11
|_. add-on version |_. Groovy version |
12 1 Udo Offermann
| v2.2, v2.3 | 2.0.6 |
13 21 Henning Blohm
| v2.4 | 2.3.11 |
14
| v2.7 | 2.5.4 |
15 22 Henning Blohm
| v2.8, master | 2.5.4 |
16 1 Udo Offermann
17
h2. How does it work?
18
19 21 Henning Blohm
The z2-Groovy compiler checks for each source component (i.e. @java/src.api@ and @java/src.impl@ ) whether it contains pure Groovy source files or Java and Groovy sources and chooses the appropriate compiler.
20 3 Udo Offermann
21 21 Henning Blohm
The Joint-Compiler of Groovy SDK compiles mixed Groovy and Java sources within three passes: In step 1) all Groovy classes are compiled as stubs (i.e. only the signatures w/o any lines of code) so that in step 2) the Java compiler from the JDK can compile all Java classes and resolve all dependencies to Groovy classes using the stubs. In step 3) the Groovy source files are translated into bytecode - since the Java classes are now translated, references from Groovy to Java can be also resolved correctly.
22
If however no Java sources are found step 1 and 2 are skipped and the compilation process is speed-up by leaving only the plain Groovy compiler!
23 3 Udo Offermann
24 21 Henning Blohm
A mixture of Groovy classes and Java classes across z2 modules is supported as well. Since cyclic dependencies are not allowed this does not even require the Groovy Joint-Compiler.
25 3 Udo Offermann
26
h2. How do I use it?
27 1 Udo Offermann
28 10 Udo Offermann
h3. 1) Include the Groovy Add-on
29
30 14 Henning Blohm
At first you need to add the Groovy Add-on repository to your environment with @gitcr.uri=http://git.z2-environment.net/z2-addons.groovy@ (see also the @environment@ module of [[Sample-groovy-in-Z2]]).
31 6 Udo Offermann
32
@environment/groovyAddon.properties:@
33 21 Henning Blohm
34 6 Udo Offermann
<pre>
35
com.zfabrik.systemStates.participation=com.zfabrik.boot.main/sysrepo_up
36
37
# git stored component repository
38 1 Udo Offermann
com.zfabrik.component.type=com.zfabrik.gitcr
39 6 Udo Offermann
40
# true <=> optional repository. If gitcr.uri is invalid, then this gitcr will be ignore silently  
41
gitcr.optional=true
42
43
# this can also point to a remote repository like 
44
# ssh://myserver/some/git/repo
45 21 Henning Blohm
gitcr.uri=https://www.z2-environment.net/git/z2-addons.groovy
46 6 Udo Offermann
47
# the git branch to use (e.g. 'master')
48
gitcr.branch=master
49 10 Udo Offermann
</pre>
50
51 1 Udo Offermann
h3. 2) Write code
52
53 21 Henning Blohm
Put Groovy source files into the same structure as Java source files - i.e. @<module-name>/java/src.api@ , @<module-name>/java/src.impl@ or @<module-name>/java/src.test@ (see the @com.zfabrik.samples.groovy.web@ module of [[Sample-groovy-in-Z2]]).
54 3 Udo Offermann
55 11 Udo Offermann
h3. 3) Define compile order
56 10 Udo Offermann
57 3 Udo Offermann
Edit the Java component descriptor and change/add the @java.compile.order@ property:
58
59
@<module-name>/java/z.properties:@
60 21 Henning Blohm
61
<pre><code>
62 3 Udo Offermann
com.zfabrik.component.type=com.zfabrik.java
63
java.privateReferences=...
64
java.publicReferences=...
65
66 1 Udo Offermann
java.compile.order = groovy
67
</code></pre>
68 5 Udo Offermann
69
When using Spring with AspectJ the compile order will be:
70 21 Henning Blohm
71 8 Udo Offermann
@java.compile.order = groovy, spring_aspectj@
72 21 Henning Blohm
73 10 Udo Offermann
h3. 4) Adjust memory settings
74 3 Udo Offermann
75
You might need to change the heap and the perm size settings of the web worker in your environment since Groovy is usually more memory hungry than Java.
76
77 1 Udo Offermann
@environment/webWorker.properties:@
78 21 Henning Blohm
79
<pre><code>
80 3 Udo Offermann
...
81
worker.process.vmOptions=\
82
 -Xmx128m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=256m \
83 1 Udo Offermann
...
84 20 Henning Blohm
</code></pre>
85
86
h2. Trouble Shooting
87
88
h3. Trouble running code in Eclipse
89
90 21 Henning Blohm
When working with Groovy code in Eclipse and having resolved a module's classpath with the Eclipsoid plugin you will have the Groovy libraries coming with this addon on the Eclipse project classpath. Eclipse's Groovy support plugin brings its own version of the groovy-all library that may (and typically will) differ from the add-on's version.
91 20 Henning Blohm
92 21 Henning Blohm
You will see an error message of the form
93 20 Henning Blohm
94
<pre>
95
groovy-all is loaded in version x.y.z and you are trying to load version a.b.c
96
</pre>
97
98 1 Udo Offermann
*Fix:* Remove the Groovy Libraries classpath container from your project by opening the project properties, choosing "Java build path" and "Libraries".