Skip to content

Getting Started

Alex Vigdor edited this page Mar 1, 2019 · 10 revisions

Getting Started with Groovity

There are two approaches to getting your first Groovity project off the ground; either a starter maven project, or a standalone groovity executable jar file.

Getting Started with Maven

To get started with Maven, you can download groovity-hello-world.zip, which offers a pre-packaged starter web application. To run this project you will need to have both JDK 1.8+ and Maven 3+ installed already.

See Installing Maven for help with first-time setup of Maven.

You can execute this application locally by unzipping this file, entering a command console, changing to the groovity-hello-world directory, and executing the command

mvn groovity:run

which starts an embedded Jetty server on the default groovity port 9880. You can access the sample home page in a browser at

http://localhost:9880

And you can modify the message it generated by adding a name query string parameter

http://localhost:9880/?name=goodbye

You can also execute groovity scripts directly from the command line prompt while groovity is running. To execute the hello world script from the command line, enter the script path and hit enter

/hello

You will be prompted and given a chance to change the default value of the name parameter; the output of the script is printed in the command window rather than in a browser.

Any changes you make to source or test scripts while groovity is running will be automatically compiled, so you can immediately try your changes without restart.

To exit the groovity runtime, enter q or CTRL-C.

You can execute groovity unit tests from the command line by executing the standard maven test command

mvn test

You can package a standard java war file of the application for deployment to a separate container (e.g. tomcat or jetty) by issuing a standard maven package or install command

mvn package

which produces a war file in the target/ directory.

For more information on the maven build lifeycle, see here

This starter project includes just 3 files in standard maven layout:

/pom.xml

A standard maven project definition file configures the groovity plugins that can be used to build and run the project, and declares a dependency on groovity servlet. The complete contents of the hello-world pom are shown here for convenient reference:

<?xml version="1.0" encoding="utf-8"?>
<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.sample</groupId>
	<artifactId>groovity-hello-world</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Groovity hello world app</name>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.disney.groovity</groupId>
				<artifactId>groovity-maven-plugin</artifactId>
				<version>2.0.4</version>
				<executions>
					<execution>
						<id>groovityTest</id>
						<goals>
							<goal>test</goal>
						</goals>
					</execution>
					<execution>
						<id>groovityPackage</id>
						<goals>
							<goal>package</goal>
						</goals>
					</execution>
					<execution>
						<id>default-cli</id>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>com.disney.groovity</groupId>
			<artifactId>groovity-servlet</artifactId>
			<version>2.0.4</version>
		</dependency>
	</dependencies>
</project>

/src/main/groovity/hello.grvt

The hello-world home page is just 11 lines of code and shows how to declare a web endpoint, expected query string parameter with default value, and how to declare a template for the generated output that references the query string parameter.

static web=[
	path:'/',
	output:'text/html',
	method:'GET'
]

static args=[
	name: 'world'
]

<~
Hello ${name}
~>

See Groovity Servlet for more documentation on how to configure web endpoints.

See Core#args for more information on declaring Arguments

See Core#templates for more information on declaring templates

/src/test/groovity/testHello.grvt

The hello-world test script is just 5 lines of code and shows how to execute an actual HTTP request in a unit test to validate proper function of the application

http(url:"http://localhost:9880/?name=Peanut+Butter"){
 handler{
   assert httpResponse.entity.content.text.trim() == "Hello Peanut Butter"
 }
}

See Http for more information on executing HTTP requests.

Getting Started with Groovity standalone

If you are not ready to take the dive into Maven, you can also download a standalone groovity runtime in groovity-standalone.zip, which requires only JDK 1.8+ to be installed already.

You can fire up the groovity runtime with embedded jetty server on port 9880 using the java -jar command to execute the jar file contained in the distribution.

java -jar groovity-standalone-2.0.4.jar

See Standalone for information on the directory layout of groovity-standalone

You can access the sample home page in a browser at

http://localhost:9880

And you can modify the message it generated by adding a name query string parameter

http://localhost:9880/?name=goodbye

Groovity standalone also comes with groovity-servlet-admin preinstalled, so you can access live documentation and runtime performance statistics

http://localhost:9880/groovity/servlet/admin/ui/stats

You can also execute groovity scripts directly from the command line prompt while groovity is running. To execute the hello world script from the command line, enter the script path and hit enter

/hello

You will be prompted and given a chance to change the default value of the name parameter; the output of the script is printed in the command window rather than in a browser.

Any changes you make to source scripts while groovity is running will be automatically compiled, so you can immediately try your changes without restart.

To exit the groovity runtime, enter q or CTRL-C.

You can also change the port groovity-standalone runs on by passing the desired port as an argument to the java command:

java -jar groovity-standalone-2.0.4.jar 8080

Out of the box, groovity standalone ships with just one file, the same hello-world server script contained in the maven starter project.

/groovity/hello.grvt

The hello-world home page is just 11 lines of code and shows how to declare a web endpoint, expected query string parameter with default value, and how to declare a template for the generated output that references the query string parameter.

static web=[
	path:'/',
	output:'text/html',
	method:'GET'
]

static args=[
	name: 'world'
]

<~
Hello ${name}
~>

See Groovity Servlet for more documentation on how to configure web endpoints.

See Core#args for more information on declaring Arguments

See Core#templates for more information on declaring templates