Rick

Rick
Rick

Sunday, February 21, 2016

Step 1 Initially setting up gradle and create Vert.x hello world (Microservices with Vertx)

Initially setting up gradle and vert.x.
We will expand in this script, but it is easier to understand in its infancy.
We are using John Rengelman's Shadow Jar Plugin which is similar to the Maven which is similar to Maven Shade plugin but for Gradle and is faster than the Gradle built-in fat jar.
I am not sure how we will do the final deploy. This is a step down the road. The direction might change.
At this point the gradle build file is pretty basic.

Gradle build file

plugins {
    id 'java'
    id 'application'
    id 'com.github.johnrengelman.shadow' version '1.2.2'
    id 'idea'
}

group 'rickhigh'
version '1.0-SNAPSHOT'


idea {
    project {
        languageLevel = '1.8'
    }
}
repositories {
    mavenCentral()
    maven {
        url = 'http://oss.sonatype.org/content/repositories/snapshots/'
    }
    mavenLocal() /* Just in case we want to use local artifacts that we build locally. */
}


sourceCompatibility = '1.8'
mainClassName = 'io.vertx.core.Launcher'

dependencies {
    compile "io.vertx:vertx-core:3.2.0"
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

/* used to create the fat jar files. */
shadowJar {
    classifier = 'fat'
    manifest {
        attributes 'Main-Verticle': 'com.github.vertx.node.example.HelloWorldVerticle'
    }
    mergeServiceFiles {
        include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}
You can see this in this branch.
Like I said, I am not sure if we will stick with gradle or use maven. It all depends. Also I am not sure we will use fatjars or dist. Or run with vertx command line.
For now, we are doing this.
Notice that the main app is io.vertx.core.Launcher. Also notice that our Verticle is calledcom.github.vertx.node.example.HelloWorldVerticle. This will change in a later branch.

HelloWorldVerticle

package com.github.vertx.node.example;

import io.vertx.core.AbstractVerticle;

public class HelloWorldVerticle extends AbstractVerticle {

    @Override
    public void start() {
        vertx.createHttpServer().requestHandler(req -> req.response().end("Hello World!")).listen(8080);
    }
}

To build the shadow jar / fat jar do this.

Build the fat jar

$ ./gradlew shadowJar
To run the verticle do this:

Run the verticle

$ #FIND The JAR
$ find . -name "*fat.jar"
./build/libs/vertx-node-ec2-eventbus-example-1.0-SNAPSHOT-fat.jar

$ # Run the jar
$ java -jar ./build/libs/vertx-node-ec2-eventbus-example-1.0-SNAPSHOT-fat.jar

Files so far.


├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── settings.gradle
├── src
    ├── main
    │   └── java
    │       └── com
    │           └── github
    │               └── vertx
    │                   └── node
    │                       └── example
    │                           └── HelloWorldVerticle.java
    └── test
        └── java

You can see this in this branch.
After you run this example, you can see it in action as follows:

Curl it

$ curl http://localhost:8080
Hello World!
Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training