An Eclipse Vert.x Gradle Plugin

Eclipse Vert.x is a ver­sa­tile toolkit, and as such it does not have any strong opin­ion on the tools that you should be using.

Gra­dle is a pop­u­lar build tool in the JVM ecosys­tem, and it is quite easy to use for build­ing Vert.x project as show in one of the vertx-examples sam­ples where a so-​called fat Jar is being pro­duced.

The new Vert.x Gra­dle plugin of­fers an opin­ion­ated plug­in for build­ing Vert.x ap­pli­ca­tions with Gra­dle.

It au­to­mat­i­cally ap­plies the fol­low­ing plu­g­ins:

  • java (and sets the source com­pat­i­bil­ity to Java 8),
  • application + shadow to gen­er­ate fat Jars with all de­pen­den­cies bun­dled,
  • nebula-dependency-recommender-plugin so that you can omit ver­sions from mod­ules from the the Vert.x stack.

The plug­in au­to­mat­i­cally adds io.vertx:vertx-core as a compile de­pen­dency, so you don’t need to do it.

The plug­in pro­vides a vertxRun task that can take ad­van­tage of the Vert.x auto-​reloading ca­pa­bil­i­ties, so you can just run it then have your code being au­to­mat­i­cally com­piled and re­loaded as you make changes.

Getting started

A min­i­mal build.gradle looks like:

plugins {
  id 'io.vertx.vertx-plugin' version '0.0.4'
}

repositories {
  jcenter()
}

vertx {
  mainVerticle = 'sample.App'
}

Pro­vided sample.App is a Vert.x ver­ti­cle, then:

  1. gradle shadowJar builds an ex­e­cutable Jar with all de­pen­den­cies: java -jar build/libs/simple-project-fat.jar, and
  2. gradle vertxRun starts the ap­pli­ca­tion and au­to­mat­i­cally re­com­piles (gradle classes) and re­loads the code when any file under src/ is being added, mod­i­fied or deleted.

Using with Kotlin (or Groovy, or…)

The plug­in in­te­grates well with plu­g­ins that add con­fig­u­ra­tions and tasks trig­gered by the classes task.

Here is how to use the plug­in with Kotlin (re­place the ver­sion num­bers with the lat­est ones…):

plugins {
  id 'io.vertx.vertx-plugin' version 'x.y.z'
  id 'org.jetbrains.kotlin.jvm' version 'a.b.c'
}

repositories {
  jcenter()
}

dependencies {
  compile 'io.vertx:vertx-lang-kotlin'
  compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8'
}

vertx {
  mainVerticle = "sample.MainVerticle"
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
  kotlinOptions {
    jvmTarget = "1.8"
  }
}

Using with WebPack (or any other custom task)

Web­Pack is pop­u­lar to bun­dle web as­sets, and there is even a guide for its in­te­gra­tion with Gra­dle.

Mix­ing the Vert.x Gra­dle plug­in with Web­Pack is very sim­ple, es­pe­cially in com­bi­na­tion with the com.moowork.node plug­in that in­te­grates Node into Gra­dle.

Sup­pose we want to mix Vert.x code and JavaScript with Gra­dle and Web­Pack. We as­sume a package.json as:

{
  "name": "webpack-sample",
  "version": "0.0.1",
  "description": "A sample with Vert.x, Gradle and Webpack",
  "main": "src/main/webapp/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^2.7.0"
  },
  "dependencies": {
    "axios": "^0.16.2"
  }
}

and webpack.config.js as:

module.exports = {
  entry: './src/main/webapp/index.js',
  output: {
    filename: './build/resources/main/webroot/bundle.js'
  }
}

The build.gradle file is the fol­low­ing:

plugins {
  id 'io.vertx.vertx-plugin' version '0.0.4'
  id 'com.moowork.node' version '1.2.0'
}

repositories {
  jcenter()
}

dependencies {
  compile "io.vertx:vertx-web"
}

vertx {
  mainVerticle = "sample.MainVerticle"
  watch = ["src/**/*", "build.gradle", "yarn.lock"]
  onRedeploy = ["classes", "webpack"]
}

task webpack(type: Exec) {
  inputs.file("$projectDir/yarn.lock")
  inputs.file("$projectDir/webpack.config.js")
  inputs.dir("$projectDir/src/main/webapp")
  outputs.dir("$buildDir/resources/main/webroot")
  commandLine "$projectDir/node_modules/.bin/webpack"
}

This cus­tom build ex­poses a webpack task that in­vokes Web­Pack, with proper file track­ing so that Gra­dle knows when the task is up-​to-date or not.

The Node plug­in adds many tasks, and in­te­grates fine with npm or yarn, so fetch­ing all NPM de­pen­den­cies is done by call­ing ./gradlew yarn.

The vertxRun task now re­de­ploys on mod­i­fi­ca­tions to files in src/ (and sub-​folders), build.gradle and yarn.lock, call­ing both the classes and webpack tasks:

Summary

The Vert.x Gra­dle plug­in pro­vides lots of de­faults to con­fig­ure a Gra­dle project for Vert.x ap­pli­ca­tions, pro­duc­ing fat Jars and of­fer­ing a run­ning task with au­to­matic re­de­ploy­ment. The plug­in also in­te­grates well with other plu­g­ins and ex­ter­nal tools for which a Gra­dle task is avail­able.

The project is still in its early stages and we are look­ing for­ward to hear­ing from you!

Next post

Eclipse Vert.x 3.5.0 released!

The Vert.x team is pleased to announce the release of Eclipse Vert.x 3.5.0.

Read more
Previous post

Introducing Vert.x MQTT client

In this article, we will see how to set up the new Vert.x MQTT client. An example project is available on GitHub.

Read more
Related posts

Unit and Integration Tests

Let’s refresh our mind about what we developed so far in the introduction to vert.x series. We forgot an important task. We didn’t test the API.

Read more

TCP Client using Eclipse Vert.x, Kotlin and Gradle build

In this blog post, I demonstrate how to write a very simple TCP client that keeps a connection open to a custom-written server in cloud.

Read more

Checklist for Migrating from Vert.x 2.1.x to Vert.x 3 - Part One

So while upgrading our application, I thought I should note down all the changes that we had to do in the process. Since Vert.x 3 is a major upgrade from the previous version, with so many changes.

Read more