Modified: 01/26/2008, Created: 11/13/2007
A Groovy based Maven project can come in at least two different packaging/artifact types: groovy-jar and war . For more information about groovy-jar packaging, see http://mojo.codehaus.org/groovy/groovy-maven-plugin/mixed-compilation.html This document contains two sets of quick start instructions depending on the packaging/archive type the Maven project is to build:
Option A describes the steps to get a Maven built Groovy-based non-web project created.
mvn archetype:create \
-DarchetypeGroupId=org.codehaus.mojo.groovy \
-DarchetypeArtifactId=groovy-maven-archetype \
-DgroupId=com.mycompany.app \
-DpackageName=com.mycompany.app.mygroovymodule \
-DartifactId=my-groovy-module
mvn archetype:create -DarchetypeGroupId=org.codehaus.mojo.groovy -DarchetypeArtifactId=groovy-maven-archetype -DgroupId=com.mycompany.app -DpackageName=com.mycompany.app.mygroovymodule -DartifactId=my-groovy-module
cd my-groovy-module
Also notice that the pom.xml file generated contains the line "<packaging>groovy-jar</packaging>"
<groupId>com.mycompany.app</groupId>
<artifactId>my-groovy-module</artifactId>
<name>Example Groovy Module - my-groovy-module</name>
<packaging>groovy-jar</packaging>
<version>1.0-SNAPSHOT</version>
Also notice that there is not a <dependency> tag with groovy-all .
...
<build>
<!--
<defaultGoal>install</defaultGoal>
-->
...
mvn package
as it is now, you will get an error message similar to the following:
[INFO] FileSet <directory> does not exist: C:\_dev\projects\my-groovy-module\src\main\groovy
mkdir src\main\groovy mkdir src\test\groovy
mvn package
mvn eclipse:clean eclipse:eclipse
move the file
to
Also move the file
to
static void main(String[] args)
{
...
, change the function to be
static void main(args)
{
...
to become
And copy, rename and edit the following file
to become
mvn package
mvn clean package
<build>
...
<plugins>
...
<!-- this plugin will copy the dependencies to
a "lib" folder during the package phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
<excludeScope>provided</excludeScope>
<includeTypes>jar</includeTypes>
<excludeTypes>war</excludeTypes>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
Now, you will get the dependency jar files deposited in target/lib when you issue the command:
mvn package
<properties>
<spring.version>2.5.1</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<!--<version>[1.1,)</version>-->
<version>1.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>logkit</groupId>
<artifactId>logkit</artifactId>
</exclusion>
<exclusion>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
...
mvn eclipse:clean eclipse:eclipse
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
mvn eclipse:clean eclipse:eclipse
java -cp "target/classes;target/lib/*" com.mycompany.app.MyExampleClass
java -cp "target/classes;target/lib/nameoflib1.jar;target/lib/nameoflib2.jar" com.mycompany.app.MyExampleClass
Replace nameoflib# with names of jar files your are dependent upon.
groovy -cp "target/classes;target/lib/nameoflib1.jar;target/lib/nameoflib2.jar" target/classes/path/to/script/GroovyScriptFileName
Option B describes the steps to get a Maven built Groovy-based web project created.
The above steps are satisfactory if the project artifact is a jar file, but we may want to code with Groovy in a Web Application resulting in a war artifact.
I will assume you will run the web application with Tomcat 6 and already have it installed and working correctly.
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
mvn archetype:create -DgroupId=com.mycompany.webapp -DpackageName=com.mycompany.webapp.mygroovywebmodule -DartifactId=my-groovy-web-module -DarchetypeArtifactId=maven-archetype-webapp
cd my-groovy-web-module
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<packaging>war</packaging>
...
<dependencies>
...
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.5.1</version>
</dependency>
...
</dependencies>
<build>
...
<plugins>
...
<!-- maven groovy plugin,
any *.groovy files in src/main/groovy or src/test/groovy
will be compiled to *.class files
-->
<plugin>
<groupId>org.codehaus.mojo.groovy</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>1.0-beta-2</version>
<!--<extensions>true</extensions>-->
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Jetty plugin to run app from Maven -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
<!-- Configure surefire to ignore integration tests on "test" and
to ignore plain unit-tests on "integration-test" -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<!--<exclude>**/*ITest.java</exclude>-->
<exclude>**/*ITest.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration tests</id>
<phase>integration-test</phase>
<goals><goal>test</goal></goals>
<configuration>
<includes>
<!--<include>**/itest/*ITest.java</include>-->
<include>**/itest/*ITest.*</include>
</includes>
<excludes>
<exclude>none</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<!-- Start Jetty on pre-integration-test and shut it down on post-integration-test -->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<wait>false</wait>
</configuration>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<systemProperties>
<org.apache.commons.logging.Log>org.apache.commons.logging.impl.SimpleLog</org.apache.commons.logging.Log>
</systemProperties>
</container>
</configuration>
</plugin>
...
</plugins>
<resources>
...
<resource>
<!-- maven default
When sources is specified
the default location is not automatically included
-->
<directory>src/main/resources</directory>
</resource>
<!-- needed to copy over the *.groovy files into the WEB-INF/classes dir -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</resource>
...
</resources>
</build>
<reporting>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo.groovy</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<!--
<version>1.0-beta-2</version>
-->
</plugin>
...
</plugins>
</reporting>
...
</project>
src/test/groovy src/main/groovy
mkdir src\main\groovy mkdir src\test\groovy
mvn eclipse:clean eclipse:eclipse
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
mvn eclipse:clean eclipse:eclipse
| Test Type | File Name Pattern | folder |
|---|---|---|
| Unit Tests coded in Java | **/*Test.java | src/test/java |
| Unit Tests coded in Groovy | **/*Test.groovy | src/test/groovy |
| Integration Tests coded in Java | **/itest/*ITest.java | src/test/java |
| Integration Tests coded in Groovy | **/itest/*ITest.groovy | src/test/groovy |
You can change this pattern by configuring the naming pattern in the maven-surefire-plugin configuration in the pom.xml file.
mvn package
mvn integration-test
<Context path="/my-groovy-web-module"
debug="5"
reloadable="true"
docBase="C:\dev\projects\my-groovy-web-module\target\my-groovy-web-module"/>
Change the value of docBase to be the location of your project.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<description>
A Groovy Based Web Application
</description>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>GroovyServlet</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GroovyTemplateServlet</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
<init-param>
<param-name>generated.by</param-name>
<param-value>false</param-value>
</init-param>
<!--
<init-param>
<param-name>encoding</param-name>
<param-value>ISO-8859-1</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
-->
</servlet>
<servlet-mapping>
<servlet-name>GroovyServlet</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GroovyTemplateServlet</servlet-name>
<url-pattern>*.gsp</url-pattern>
</servlet-mapping>
</web-app>
<!-- Generated by Groovy TemplateServlet [create/get=0 ms, make=2 ms] -->
The inspiration for some of the sample code is from the following links
| file | add to project location | Note |
|---|---|---|
| diagnostic.groovy
example1.groovy helloworld.gsp helloworld.gsp randcolor.gsp simple.gsp AGroovyScript.groovy AGroovyScript2.groovy |
src/main/webapp src/main/webapp src/main/webapp src/main/webapp/gsp/dir src/main/webapp src/main/webapp src/main/groovy src/main/resources |
mvn package
mvn clean package
startup
http://localhost:8080/my-groovy-web-module/ http://localhost:8080/my-groovy-web-module/helloworld.gsp http://localhost:8080/my-groovy-web-module/simple.gsp http://localhost:8080/my-groovy-web-module/randcolor.gsp http://localhost:8080/my-groovy-web-module/diagnostic.groovy http://localhost:8080/my-groovy-web-module/example1.groovy
shutdown
mvn archetype:create -DgroupId=com.mycompany.webapp -DpackageName=com.mycompany.webapp.groovyajax -DartifactId=groovyajax -DarchetypeArtifactId=maven-archetype-webapp
and then followed the above "Option B: Creating a new war Package Groovy-based Module Maven Project" steps to get to here.
<Context path="/groovyajax"
debug="5"
reloadable="true"
docBase="C:\dev\projects\groovyajax\target\groovyajax"/>
Change the value of docBase to be the location of your project.
To get the "Head Rush Ajax" examples to work:
| file | add to project location | Note |
|---|---|---|
| boards.html
getUpdatedBoardSales-ajax.gsp boards.html getUpdatedBoardSales-ajax.gsp lookupCustomer.gsp pizza.html placeOrder.gsp lookupCustomer.gsp pizza.html placeOrder.gsp coffee-sync.js coffee.js coffeemaker.gsp lookupCustomer.gsp pizza.js placeOrder.gsp lookupCustomer.gsp pizza.js placeOrder.gsp boards.js getUpdatedSales.gsp boards.js getUpdatedSales.gsp |
src/main/webapp/chapter01/boards-complete/ src/main/webapp/chapter01/boards-complete/ src/main/webapp/chapter02/boards-complete/ src/main/webapp/chapter02/boards-complete/ src/main/webapp/chapter02/breakneck-complete/ src/main/webapp/chapter02/breakneck-complete/ src/main/webapp/chapter02/breakneck-complete/ src/main/webapp/chapter02-interlude/breakneck-complete/ src/main/webapp/chapter02-interlude/breakneck-complete/ src/main/webapp/chapter02-interlude/breakneck-complete/ src/main/webapp/chapter03/coffee-complete/ src/main/webapp/chapter03/coffee-complete/ src/main/webapp/chapter03/coffee-complete/ src/main/webapp/chapter05/breakneck-complete/ src/main/webapp/chapter05/breakneck-complete/ src/main/webapp/chapter05/breakneck-complete/ src/main/webapp/chapter05-interlude/breakneck-complete/ src/main/webapp/chapter05-interlude/breakneck-complete/ src/main/webapp/chapter05-interlude/breakneck-complete/ src/main/webapp/chapter06/boards-complete/ src/main/webapp/chapter06/boards-complete/ src/main/webapp/chapter07/boards/ src/main/webapp/chapter07/boards/ |
mvn package
mvn clean package
startup
http://localhost:8080/groovyajax/chapter01/boards-complete/getUpdatedBoardSales-ajax.gsp http://localhost:8080/groovyajax/chapter01/boards-complete/boards.html http://localhost:8080/groovyajax/chapter02/boards-complete/getUpdatedBoardSales-ajax.gsp http://localhost:8080/groovyajax/chapter02/boards-complete/boards.html http://localhost:8080/groovyajax/chapter02/breakneck-complete/lookupCustomer.gsp http://localhost:8080/groovyajax/chapter02/breakneck-complete/pizza.html http://localhost:8080/groovyajax/chapter02-interlude/breakneck-complete/lookupCustomer.gsp http://localhost:8080/groovyajax/chapter02-interlude/breakneck-complete/pizza.html http://localhost:8080/groovyajax/chapter03/coffee-complete/coffee-sync.html http://localhost:8080/groovyajax/chapter03/coffee-complete/coffee.html http://localhost:8080/groovyajax/chapter03/coffee-complete/coffeemaker.gsp http://localhost:8080/groovyajax/chapter04/simple.html http://localhost:8080/groovyajax/chapter04/whatAmI.html http://localhost:8080/groovyajax/chapter04/tree-test.html http://localhost:8080/groovyajax/chapter04/top5-complete/top5.html http://localhost:8080/groovyajax/chapter05/breakneck-complete/pizza.html http://localhost:8080/groovyajax/chapter05-interlude/breakneck-complete/pizza.html http://localhost:8080/groovyajax/chapter06/boards-complete/getUpdatedSales.gsp http://localhost:8080/groovyajax/chapter06/boards-complete/boards.html http://localhost:8080/groovyajax/chapter07/boards/boards.html
shutdown
References:
See URLs:
http://trac.geekisp.com/SiA/browser/trunk/Chapter09/Poker-WS/README.txt
and
http://trac.geekisp.com/SiA/browser/trunk/Chapter09/Poker-WS/pom.xml