Baratine on GitHub

Hello World - Eclipse/Gradle Walkthrough

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

Download and Install Gradle

For this tutorial we will using Eclipse with the Gradle plugin.

If you have not already downloaded & installed the Gradle plugin for eclipse, you can grab it from here: Gradle

After installing, navigate to the following menu:

File > New > Project > Gradle Project

../../_images/example-gradle.png

The Gradle Project Wizard will open and walk you through setting up an initial project.

Download and unpack Baratine

Select your generated project and create a new class file within the package. Name the file “HelloMain.java”.

The code for our entire Hello World is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import static io.baratine.web.Web.*;

public class HelloMain
{
  public static void main(String []args)
  {
    get("/hello").to(req->req.ok("Hello, world"));

    start(args);
  }
}

Most of the work is done on line 7 of our Hello World:

  1. We declare the path /hello by passing the string “/hello” to get
  2. We bind the path /hello with .to and pass in a lambda
  3. In our lambda we call .ok pass in our arguments to be returned

Note

Step 3 is an important step to the Baratine model. Baratine

is a reactive web server, which means a request may stil be pending when a method returns. Therefore, we call .ok to signal the completion of the actual request.

Edit your build.gradle script

Gradle will automatically configure a default build.gradle file for you. For this initial Hello World Project using Gradle, we will only alter what is necessary to create an executable jar.

Change the generated build.gradle script to apply ‘baratine’ plugin. The plugin applies ‘java’ and ‘application’ plugins automatically.

Next, configure main class using mainClassName.

The final build.gradle should look as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//let gradle know where to find 'baratine' plugin
buildscript {
  repositories {
    mavenCentral();
  }

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

apply plugin: 'baratine'

repositories {
    mavenCentral();
}

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

mainClassName='test.HelloMain'

That’s it! From here we will build our gradle script and execute the jar.

Execute your Gradle Script

From Terminal, navigate to your working directory for eclipse and execute: gradle build

You will see a print out in the terminal window, displaying relevant information about the gradle build.

../../_images/example-build.png

From here, your jar file will be placed in build > libs > HelloMain.jar

An additional executable jar is created as build > libs > HelloMain-boot.jar

Execute your the executable jar with the following command & navigate to localhost:8080/hello

java -jar HelloMain-boot.jar

../../_images/example-execute.png

In your browser:

../../_images/browser.png

Congratulations, you’ve now deployed your first Baratine Service!

Next Steps

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