Wiki source code of Groovy in Java EE with Maven
Hide last authors
author | version | line-number | content |
---|---|---|---|
1.1 | 1 | It is quite easy to add Groovy support. Once the support is in place you can use Groovy as you would use regular Java. As a matter of fact to test out Groovy we will create a Struts2 action with it! | |
2 | |||
3 | |||
4 | Following assumes we have set up a basic struts2 application using Maven: | ||
5 | |||
6 | 1) In Netbeans, expand the "Project Files" and open pom.xml. | ||
7 | |||
8 | 2a) Locate the maven compiler plugin, which in my poms typically looks like the following: | ||
9 | |||
10 | |||
11 | <plugin> | ||
12 | <groupId>org.apache.maven.plugins</groupId> | ||
13 | <artifactId>maven-compiler-plugin</artifactId> | ||
14 | <version>2.3.2</version> | ||
15 | <configuration> | ||
16 | <source>1.7</source> | ||
17 | <target>1.7</target> | ||
18 | <compilerArguments> | ||
19 | <endorseddirs>${endorsed.dir}</endorseddirs> | ||
20 | </compilerArguments> | ||
21 | </configuration> | ||
22 | </plugin> | ||
23 | |||
24 | |||
25 | 2b) Edit this pom.xml such that the above looks like the following: | ||
26 | |||
27 | <plugin> | ||
28 | <groupId>org.apache.maven.plugins</groupId> | ||
29 | <artifactId>maven-compiler-plugin</artifactId> | ||
30 | <version>2.3.2</version> | ||
31 | <configuration> | ||
32 | **~ <compilerId>groovy-eclipse-compiler</compilerId>** | ||
33 | **~ <verbose>true</verbose>** | ||
34 | <source>1.7</source> | ||
35 | <target>1.7</target> | ||
36 | </configuration> | ||
37 | **~ <dependencies>** | ||
38 | **~ <dependency>** | ||
39 | **~ <groupId>org.codehaus.groovy</groupId>** | ||
40 | **~ <artifactId>groovy-eclipse-compiler</artifactId>** | ||
41 | **~ <version>2.6.0-01</version>** | ||
42 | **~ </dependency>** | ||
43 | **~ </dependencies>** | ||
44 | </plugin> | ||
2.1 | 45 | ||
46 | |||
47 | 3) Add the following dependancy | ||
48 | |||
49 | <dependency> | ||
50 | <groupId>org.codehaus.groovy</groupId> | ||
51 | <artifactId>groovy-all</artifactId> | ||
52 | <version>1.8.4</version> | ||
53 | <scope>provided</scope> | ||
54 | </dependency> | ||
55 | |||
56 | 4) Download a copy of groovy-all-1.8.4.jar and copy it into the glassfish lib folder (partial path on my system is .../glassfish-3.1.2/glassfish/lib/groovy-all-1.8.4.jar). | ||
3.1 | 57 | ||
58 | |||
59 | ---- | ||
60 | |||
61 | With that bit of initial configuration out of the way we can now use Groovy quite seemlessly in our project. | ||
62 | |||
63 | |||
64 | == Create a Struts2 action (will use the struts2 conventions plugin to avoid needless configuration) == | ||
65 | |||
66 | (% style="font-weight: bold;" %)In Netbeans 7.1, create a new package under source packages called com.kenmcwilliams.action.groovy. | ||
67 | |||
68 | (% style="font-weight: bold;" %)Right click that folder and create a new groovy file (New->Other->Category:Groovy->Groovy Class).(% style="font-weight: normal;" %) If the following options does not exist you will need at add Groovy **(Tools(menu)->Plugins->Availible Plugins(tab)->select "Groovy and Grails")**(%%). | ||
69 | |||
70 | //package com.kenmcwilliams.action.groovy// | ||
71 | //import com.opensymphony.xwork2.ActionSupport;// | ||
72 | //import com.opensymphony.xwork2.Action;// | ||
73 | \\// class Gest extends ActionSupport{// | ||
74 | // String greeting = "Hello from Groovy!";// | ||
75 | // public String execute(){// | ||
76 | // println "Grooooovy!"// | ||
77 | // return "success";// | ||
78 | // }// | ||
79 | //}// | ||
80 | |||
81 | Create a view to render the action: | ||
82 | |||
83 | //<%@taglib prefix="s" uri="/struts-tags"%>// | ||
84 | //<%@page contentType="text/html" pageEncoding="UTF-8"%>// | ||
85 | //<!DOCTYPE html>// | ||
86 | //<html>// | ||
87 | // <head>// | ||
88 | // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">// | ||
89 | // <title>Message from Groovy</title>// | ||
90 | // </head>// | ||
91 | // <body>// | ||
92 | // <h1>Message from Groovy</h1>// | ||
93 | // <s:property value="greeting"/>// | ||
94 | // </body>// | ||
95 | //</html>// | ||
96 | |||
97 | |||
98 | That's all there is to it! | ||
99 |