2 Quick Start
This chapter provides instructions on how to get a Ratpack application up and running to play with.
1.2 Using a Groovy script
A Ratpack application can be implemented as a single Groovy script. This is a useful way to experiment with Ratpack and Groovy.
First, install Groovy.
Create the file ratpack.groovy
with the following content:
@Grapes([
@Grab('io.ratpack:ratpack-groovy:2.0.0-rc-1'),
@Grab('org.slf4j:slf4j-simple:1.7.36')
])
import static ratpack.groovy.Groovy.ratpack
ratpack {
handlers {
get {
render "Hello World!"
}
get(":name") {
render "Hello $pathTokens.name!"
}
}
}
You can now start the app by running the following on the command line:
groovy ratpack.groovy
The server will be available via http://localhost:5050/
.
The handlers()
method takes a closure that delegates to a GroovyChain
object. The “Groovy Handler Chain DSL” is used to build the response handling strategy.
Changes to the file are live during development. You can edit the file, and the changes will take effect on the next request.
2.2 Using the Gradle plugin(s)
We recommend the use of the Gradle build system to build Ratpack applications. Ratpack does not require Gradle; any build system can be used.
The following instructions assume you have already installed Gradle. See the Gradle User Guide for installation instructions.
The Ratpack project provides two Gradle plugins:
- io.ratpack.ratpack-java - for Ratpack applications implemented in Java
- io.ratpack.ratpack-groovy - for Ratpack applications implemented in Groovy
For a more detailed explanation of the Gradle build support, please see the dedicated chapter.
1.2.2 Using the Gradle Java plugin
Create a build.gradle
file with the following contents:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.ratpack:ratpack-gradle:2.0.0-rc-1"
}
}
apply plugin: "io.ratpack.ratpack-java"
apply plugin: "idea"
repositories {
mavenCentral()
}
dependencies {
runtimeOnly "org.slf4j:slf4j-simple:1.7.36"
}
mainClassName = "my.app.Main"
Create the file src/main/java/my/app/Main.java
, with the following content:
package my.app;
import ratpack.core.server.RatpackServer;
public class Main {
public static void main(String... args) throws Exception {
RatpackServer.start(server -> server
.handlers(chain -> chain
.get(ctx -> ctx.render("Hello World!"))
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!"))
)
);
}
}
You can now start the application either by executing the run
task with Gradle (i.e. gradle run
on the command line), or by importing the project into your IDE and executing the my.app.Main
class.
When run, the server will be available via http://localhost:5050/
.
The handlers()
method takes a function that receives a Chain
object. The “Handler Chain API” is used to build the response handling strategy.
The Ratpack Gradle plugin supports Gradle’s Continuous Build feature. Use it to have changes to your source code be automatically applied to your running application.
For further information on using Ratpack with Groovy, please see the Gradle chapter.
2.2.2 Using the Gradle Groovy plugin
Create a build.gradle
file with the following contents:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.ratpack:ratpack-gradle:2.0.0-rc-1"
}
}
apply plugin: "io.ratpack.ratpack-groovy"
apply plugin: "idea"
repositories {
mavenCentral()
}
dependencies {
runtimeOnly "org.slf4j:slf4j-simple:1.7.36"
}
Create the file src/ratpack/ratpack.groovy
, with the following content:
import static ratpack.groovy.Groovy.ratpack
ratpack {
handlers {
get {
render "Hello World!"
}
get(":name") {
render "Hello $pathTokens.name!"
}
}
}
You can now start the application either by executing the run
task with Gradle (i.e. gradle run
on the command line), or by importing the project into your IDE and executing the ratpack.groovy.GroovyRatpackMain
class.
When run, the server will be available via http://localhost:5050/
.
The handlers()
method takes a closure that delegates to a GroovyChain
object. The “Groovy Handler Chain DSL” is used to build the response handling strategy.
The Ratpack Gradle plugin supports Gradle’s Continuous Build feature. Use it to have changes to your source code be automatically applied to your running application.
For further information on using Ratpack with Groovy, please see the Groovy chapter.
For further information on using Ratpack with Groovy, please see the Gradle chapter.