Baratine on GitHub

Hello World View - Gradle

This tutorial will walk you through deploying Baratine with Gradle. Gradle is a build and automation tool that can automate building, testing, and deploying tasks. At the end of this tutorial you will have a standalone exectuable jar that will deploy your Hello World Baratine service.

The Hello World has the following features:

  • Uses the Mustache template engine to render the template into HTML
  • Updates the greeting with a HTTP POST.
  • Is persistent. Data is stored in Baratine’s internal document database.
  • Is thread safe without requiring additional locks.

Directory Structure

You’ll create three files:
  • build.gradle is the gradle build file
  • src/main/java/hello/Hello.java is the Java source
  • src/main/resources/templates/hello.mustache is Mustache template
hello/
  build.gradle
  src/main/
       java/
         hello/Hello.java
       resources/
         templates/hello.mustache

build.gradle

build.gradle is the Gradle build file, which includes any dependencies like Baratine itself.

In the gradle build below, we’ve included a Baratine plugin, which adds a boot-jar task to create an executable boot jar.

apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'application'

repositories {
  mavenCentral();
}

dependencies {
  compile 'io.baratine:baratine:1.0.0'
  compile 'com.github.spullara.mustache.java:compiler:0.9.1'
}

mainClassName='hello.Hello'

To build the application use:

$ gradle build

which will create a build/libs/hello.jar.

To run the application directly use:

$ gradle run

Create the Hello Java Source

Java source is in src/main/java. The code for Hello World is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package hello;

import io.baratine.service.*;
import io.baratine.vault.*;
import io.baratine.web.*;

@Asset
@Service
public class Hello
{
  private String _greeting = "Hello, world";

  @Get("/")
  public void hello(RequestWeb result)
  {
    result.ok(View.newView("hello.mustache")
                   .add("greeting", _greeting));
  }

  @Post
  @Modify
  public void update(@Body Form form, RequestWeb result)
  {
    _greeting = form.first("greeting");

    result.redirect("/");
  }

  public static void main(String []args)
  {
    Web.scanAutoConf();

    Web.include(Hello.class);

    Web.go(args);
  }
}

Line notes:

    1. @Asset marks the service as persistent, saved to the internal database.
    1. Field updates are thread-safe because Hello is a Baratine service.
    1. @Get is a HTTP GET method.
    1. The result is a view-model passed to Mustache.
    1. @Post marks a HTTP POST method with a URL defaulting to the method name.
    1. @Modify marks a persistent-update method. Updates are saved.
    1. Forms are parsed from the HTTP body.
    1. Web.scanAutoConf initializes Mustache when it’s in the classpath.
    1. Web.include adds Hello as a HTTP service.
    1. Web.go starts the web server with the command line arguments.

Add a hello.mustache

hello.mustache belongs in src/main/resources/templates

hello.mustache:

Greeting: {{greeting}}

<br />
<form action="/update" method="post">
  <input name="greeting" />
</form>

Run with Gradle

You can run directly with the run task:

$ gradle run

Browse http://localhost:8080/.

Build an executable boot jar

The build.gradle can also build a boot jar that contains all the dependencies needed to run the application. The boot jar requires the baratine gradle plugin, which is included in baratine.jar

build.gradle:

apply plugin: 'maven'

buildscript {
  repositories {
    mavenCentral();
  }

  dependencies {
    classpath 'io.baratine:baratine:1.0.0'
  }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'baratine'

repositories {
  mavenCentral();
}

dependencies {
  compile 'io.baratine:baratine:1.0.0'
  compile 'com.github.spullara.mustache.java:compiler:0.9.1'
}

mainClassName='hello.Hello'

Now the following build will also create a build/libs/hello-boot.jar.

$ gradle build

The hello-boot.jar is an executable jar that includes all the project’s dependencies.

You can run the boot jar as follows:

$ java -jar build/libs/hello-boot.jar

Next Steps

  • Download Baratine or add a maven dependency to start
  • See Web for HTTP/Web documentation.
  • See introduction for an introduction.