[JAVA] Try "Introduction to Vert.x" on Gradle

There is "Introduction to Vert.x" in the Vert.x 3 series document. It describes how to build a simple CRUD screen by assembling the source code from scratch, and it is an excellent tutorial to do at the beginning. But unfortunately it's written based on Maven, so Gradle doesn't say how to do it. So I tried this tutorial on Gradle. The source is in the following repository.

https://github.com/nakaken0629/my-first-app

For the time being, please do your best on your own until the Gradle 3 system works. I'm also trying it on a Mac. I think you can do the same with Linux, but I think Windows has more fixes. If you can run Gradle like this, Gradle installation is complete.

$ gradle --version

------------------------------------------------------------
Gradle 3.4.1
------------------------------------------------------------

Build time:   2017-03-03 19:45:41 UTC
Revision:     9eb76efdd3d034dc506c719dac2955efb5ff9a93

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_101 (Oracle Corporation 25.101-b13)
OS:           Mac OS X 10.11.6 x86_64

My first Vert.x 3 Application

Let’s start !

First, create a new project.

$ mkdir my-first-app
$ cd my-first-app
$ gradle init --type java-application

Set the project information and the setting to refer to vertx-core in build.gradle.

diff --git a/build.gradle b/build.gradle
index c63225d..2576243 100644
--- a/build.gradle
+++ b/build.gradle
@@ -6,6 +6,12 @@
  * user guide available at https://docs.gradle.org/3.4.1/userguide/tutorial_java_projects.html
  */

+group 'io.vertx.blog'
+version '1.0-SNAPSHOT'
+project.ext {
+      artifactId = 'my-first-app'
+}
+
 // Apply the java plugin to add support for Java
 apply plugin: 'java'

@@ -21,7 +27,7 @@ repositories {

 dependencies {
     // This dependency is found on compile classpath of this component and consumers.
-    compile 'com.google.guava:guava:20.0'
+    compile 'io.vertx:vertx-core:3.0.0'

     // Use JUnit test framework
     testCompile 'junit:junit:4.12'

Let’s code !

Create the MyFirstVerticle class in "src / main / java / io / vertx / blog / first / MyFirstVerticle.java".

Now the compilation will pass.

$ ./gradlew compileJava
:compileJava UP-TO-DATE

BUILD SUCCESSFUL

Total time: 2.633 secs

Let’s test

Set to refer to vertx-unit in build.gradle.

diff --git a/build.gradle b/build.gradle
index 2576243..404ca01 100644
--- a/build.gradle
+++ b/build.gradle
@@ -31,6 +31,7 @@ dependencies {

     // Use JUnit test framework
     testCompile 'junit:junit:4.12'
+    testCompile 'io.vertx:vertx-unit:3.0.0'
 }

Create the MyFirstVerticleTest class in "src / test / java / io / vertx / blog / first / MyFirstVerticleTest.java".

The test will now pass.

$ ./gradlew test
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 1.665 secs

Packaging

Since shade is a plugin for Maven, we will introduce shadow with similar function in Gradle. All you have to do is set it in build.gradle. The following contents have been added.

$ git diff
diff --git a/build.gradle b/build.gradle
index 404ca01..c7198bb 100644
--- a/build.gradle
+++ b/build.gradle
@@ -6,6 +6,10 @@
  * user guide available at https://docs.gradle.org/3.4.1/userguide/tutorial_java_projects.html
  */

+plugins {
+    id 'com.github.johnrengelman.shadow' version '1.2.3'
+}
+
 group 'io.vertx.blog'
 version '1.0-SNAPSHOT'
 project.ext {
@@ -35,5 +39,14 @@ dependencies {
 }

 // Define the main class for the application
-mainClassName = 'App'
-
+mainClassName = 'io.vertx.core.Starter'
+
+shadowJar {
+    classifier = 'fat'
+    manifest {
+        attributes 'Main-Verticle': 'io.vertx.blog.first.MyFirstVerticle'
+    }
+    mergeServiceFiles {
+        include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
+    }
+}

Run the shadowJar task to create a fat jar file. The files will be created under the "build / libs /" directory.

$ ./gradlew shadowJar
:compileJava
:processResources NO-SOURCE
:classes
:shadowJar

BUILD SUCCESSFUL

Total time: 2.74 secs

Executing our application

You can start it with the following command. Press "CTRL + C" to stop.

$ java -jar build/libs/my-first-app-1.0-SNAPSHOT-fat.jar
3 26, 2017 9:59:36 am io.vertx.core.Starter
information: Succeeded in deploying verticle

Vert.x Application Configuration

This chapter doesn't have any notable Gradle-only customizations.

Some Rest with Vert.x

Vert.x Web

Add vertx-web to your classpath.

$ git diff head^ head
diff --git a/build.gradle b/build.gradle
index c7198bb..31f115e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -32,6 +32,7 @@ repositories {
 dependencies {
     // This dependency is found on compile classpath of this component and consumers.
     compile 'io.vertx:vertx-core:3.0.0'
+    compile 'io.vertx:vertx-web:3.0.0'

     // Use JUnit test framework
     testCompile 'junit:junit:4.12'

Some Rest with Vert.x

This chapter doesn't have any notable Gradle-only customizations.

Unit and Integration Tests

Implement the plan

TODO: Find a free port for testing and rewrite src / test / resources / my-it-config.json at build time

Add a setting to make the existing test task dependent on shadowJar, start the vertx process before executing the test, and end the vertx process after the test is completed.

diff --git a/build.gradle b/build.gradle
index 31f115e..fc8c656 100644
--- a/build.gradle
+++ b/build.gradle
@@ -51,3 +51,13 @@ shadowJar {
         include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
     }
 }
+
+test.dependsOn.add(shadowJar)
+test.doFirst {
+    ant.java(jar: "build/libs/${project.name}-${version}-fat.jar",
+            fork: true,
+            spawn: true)
+}
+test.doLast {
+    ['sh', '-c', "ps -ax | grep ${project.name}-${version}-fat.jar | grep -v grep | awk 'NR==1{print \$1}' | xargs kill -SIGTERM"].execute()
+}

Hey, we don’t have integration tests !

Set to add "AssertJ" and "Rest-Assured" to the classpath in build.gradle.

diff --git a/build.gradle b/build.gradle
index fc8c656..a5ea94a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -37,6 +37,8 @@ dependencies {
     // Use JUnit test framework
     testCompile 'junit:junit:4.12'
     testCompile 'io.vertx:vertx-unit:3.0.0'
+    testCompile "com.jayway.restassured:rest-assured:2.4.0"
+    testCompile "org.assertj:assertj-core:2.0.0"
 }

You should now be able to pass the Integration Test as well.

$ ./gradlew test
:compileJava
:processResources
:classes
:shadowJar
:compileTestJava
:processTestResources NO-SOURCE
:testClasses
:test

BUILD SUCCESSFUL

Total time: 9.685 secs

Using the asynchronous SQL client

This chapter doesn't have any notable Gradle-only customizations.

Combine vert.x and mongo to build a giant

TODO: Give it a try (I'm not interested in mongo so far, so I'm passing it :-p)

Recommended Posts

Try "Introduction to Vert.x" on Gradle
[Introduction] Try to create a Ruby on Rails application
Introduction to Ruby 2
Introduction to SWING
Introduction to Micronaut 1 ~ Introduction ~
Introduction to migration
Introduction to java
Introduction to Doma
Try to build Java8 environment on Amazon Linux2
Introduction to kotlin for iOS developers ③-About gradle
Try to display hello world with spring + gradle
Introduction to JAR files
Try to release gem
Introduction to Ratpack (8)-Session
Try DisplayLink on Ubuntu 20.04
Introduction to RSpec 1. Test, RSpec
Try OpenLiteSpeed on CentOS8
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Let's try Zoomdata (Introduction)
Introduction to RSpec 2. RSpec setup
How to use Gradle
Introduction to Keycloak development
Introduction to javac command
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
Try RabbitMQ + PHP on CentOS
Introduction to Design Patterns (Builder)
Try Spring Boot from 0 to 100.
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to RSpec 3. Model specs
Gradle to touch and remember
Introduction to Ratpack (5) --Json & Registry
Try Docker on Windows 10 Home
How to deploy on heroku
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1