Project

General

Profile

Groovy Add-on » History » Version 16

Henning Blohm, 10.04.2013 13:50

1 1 Udo Offermann
h1. Groovy Add-on
2 2 Udo Offermann
3 12 Udo Offermann
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 16 Henning Blohm
It is provided via the repository "z2-addons-groovy":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-groovy
6
7 3 Udo Offermann
The Groovy Add-on comes with the Groovy version 2.1.2. 
8
9
h2. How does it work?
10
11
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. 
12
13
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. 
14
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! 
15
16
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. 
17
18
h2. How do I use it?
19
20 10 Udo Offermann
h3. 1) Include the Groovy Add-on
21
22 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]]).
23 6 Udo Offermann
24
@environment/groovyAddon.properties:@
25
<pre>
26
com.zfabrik.systemStates.participation=com.zfabrik.boot.main/sysrepo_up
27
28
# git stored component repository
29
com.zfabrik.component.type=com.zfabrik.gitcr
30
31
# true <=> optional repository. If gitcr.uri is invalid, then this gitcr will be ignore silently  
32
gitcr.optional=true
33
34
# this can also point to a remote repository like 
35
# ssh://myserver/some/git/repo
36
gitcr.uri=http://git.z2-environment.net/z2-addons.groovy
37
38
# the git branch to use (e.g. 'master')
39
gitcr.branch=master
40
</pre>
41
42 10 Udo Offermann
h3. 2) Write code
43
44 15 Udo Offermann
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]]).
45 3 Udo Offermann
46 11 Udo Offermann
h3. 3) Define compile order
47 10 Udo Offermann
48 3 Udo Offermann
Edit the Java component descriptor and change/add the @java.compile.order@ property:
49
50
@<module-name>/java/z.properties:@
51
<pre><code class="python">
52
com.zfabrik.component.type=com.zfabrik.java
53
java.privateReferences=...
54
java.publicReferences=...
55
56
java.compile.order = groovy
57
</code></pre>
58 5 Udo Offermann
59
When using Spring with AspectJ the compile order will be:
60 1 Udo Offermann
 
61 8 Udo Offermann
@java.compile.order = groovy, spring_aspectj@
62 5 Udo Offermann
 
63 10 Udo Offermann
h3. 4) Adjust memory settings
64
65 9 Udo Offermann
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.
66 3 Udo Offermann
67
@environment/webWorker.properties:@
68
<pre><code class="python">
69
...
70
worker.process.vmOptions=\
71
 -Xmx128m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=256m \
72
...
73
</code></pre>