Introduction
The template project, that you can download from the download page,
follows the project structure outlined in the setup section.
You will notice it features a build.gradle file for letting you build, test, run and deploy you project.
This build file is using the Gradle build system,
which is using the Groovy language for its build description DSL.
This Gradle build file leverages key plugins that we'll describe below,
and offers integration with the Spock testing framework for testing your groovlets.
Note: The template project conveniently provides the Gradle wrapper to run your code without having to install the Gradle runtime. You do not have to provide a Google App Engine SDK in your environment. It will automatically be downloaded by the build script.
Gradle build file
The Gradle build file uses the following Gradle plugins:
-
java: for compiling the Java sources in the
src/main/javafolder -
groovy: for compiling the Groovy sources in the
src/main/groovyfolder - eclipse: for creating/updating the proper project files for Eclipse
- idea: for creating/updating the proper project files for IntelliJ IDEA
- gae: for interacting with the Google App Engine SDK for running, deploying apps, instead of using the SDK command-line directly
- gaelyk: for creating views and controllers, taking care of plugins
You'll find more information on the gae plugin and gaelyk plugin on their respective project pages. The pages describe all available configuration options and tasks in detail.
In a nutshell
You will find the following Gradle tasks handy:
- gradlew tasks: to list all the possible tasks which are available
-
gradlew classes: to compile your Java/Groovy sources in the folders
src/main/javaandsrc/main/groovyand have them placed inWEB-INF/classes -
gradlew test: to compile and run your tests from
src/test/javaandsrc/test/groovy - gradlew gaeRun: to run your application locally
- gradlew gaeStop: to stop your locally running application
- gradlew gaeUpload: to upload your application to production
-
gradlew gaelykInstallPlugin: to install a plugin provided by the
command line property (
-P)plugin -
gradlew gaelykUninstallPlugin: to uninstall a plugin provided by the
command line property (
-P)plugin - gradlew gaelykListInstalledPlugins: to show the installed plugins
- gradlew gaelykCreateController<ControllerName>: to create a Groovlet with the specified name
- gradlew gaelykCreateView<ViewName>: to create a Groovy template with the specified name
- gradlew cleanEclipse eclipse: to generate Eclipse project files
- gradlew cleanIdea idea: to generate IntelliJ project files
Testing with Spock
You can test your groovlets with the Spock testing framework.
As an example, lets imagine we have the following groovlet, in WEB-INF/groovy/dataStoreGroovlet.groovy,
which inserts some data in the datastore:
import com.google.appengine.api.datastore.*
def e = new Entity("person")
e.firstname = 'Bart'
e.lastname = 'Simpson'
e.save()
You could test such a Groovlet with the following Spock test, in src/test/groovy/DatastoreServiceSpec.groovy:
import groovyx.gaelyk.spock.*
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*
class DatastoreServiceSpec extends GaelykUnitSpec {
def setup() {
groovlet 'dataStoreGroovlet.groovy'
}
def "the datastore is used from within the groovlet"() {
given: "the initialised groovlet is invoked and data is persisted"
dataStoreGroovlet.get()
when: "the datastore is queried for data"
def query = new Query("person")
query.addFilter("firstname", Query.FilterOperator.EQUAL, "Marco")
def preparedQuery = datastore.prepare(query)
def entities = preparedQuery.asList(withLimit(1))
then: "the persisted data is found in the datastore"
def person = entities[0]
person.firstname == 'Bart'
person.lastname == 'Simpson'
}
}
Then, you could run the test by executing the gradle command: gradlew test.
For further information, please have a look at the Spock support for Gaelyk.
