Menu Close
Settings Close

Language and Page Formatting Options

Chapter 3. Creating an Eclipse Vert.x project with a POM file

When you develop a basic Eclipse Vert.x application, you should create the following artifacts. We will create these artifacts in our first getting-started Eclipse Vert.x project.

  • A Java class containing Eclipse Vert.x methods.
  • A pom.xml file containing information required by Maven to build the application.

The following procedure creates a simple Greeting application that returns Greetings! as response.

Note

Eclipse Vert.x supports builder images based on OpenJDK 8 and OpenJDK 11 for building and deploying your applications to OpenShift. Oracle JDK and OpenJDK 9 builder images are not supported.

Prerequisites

  • OpenJDK 8 or OpenJDK 11 is installed.
  • Maven is installed.

Procedure

  1. Create a new directory getting-started, and navigate to it.

    $ mkdir getting-started
    $ cd getting-started

    This is the root directory for the application.

  2. Create a directory structure src/main/java/com/example/ in the root directory, and navigate to it.

    $ mkdir -p src/main/java/com/example/
    $ cd src/main/java/com/example/
  3. Create a Java class file MyApp.java containing the application code.

    package com.example;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Promise;
    
    public class MyApp extends AbstractVerticle {
    
        @Override
        public void start(Promise<Void> promise) {
            vertx
                .createHttpServer()
                .requestHandler(r ->
                    r.response().end("Greetings!"))
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        promise.complete();
                    } else {
                        promise.fail(result.cause());
                    }
                });
        }
    }

    The application starts an HTTP Server on port 8080. When you send a re­quest, it re­turns Greetings! mes­sage.

  4. Create a pom.xml file in the application root directory getting-started with the following content:

    • In the <dependencyManagement> section, add the io.vertx:vertx-dependencies artifact.
    • Specify the type as pom and scope as import.
    • In the <project> section, under <properties>, specify the versions of Eclipse Vert.x and the Eclipse Vert.x Maven Plugin.

      Note

      Properties can be used to set values that change in every release. For example, versions of product or plugins.

    • In the <project> section, under <plugin>, specify vertx-maven-plugin. The Eclipse Vert.x Maven Plugin is used to package your application.
    • Include repositories and pluginRepositories to specify the repositories that contain the artifacts and plugins to build your application.

      The pom.xml contains the following artifacts:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>My Application</name>
      <description>Example application using Vert.x</description>
    
      <properties>
        <vertx.version>4.1.8.redhat-00003</vertx.version>
        <vertx-maven-plugin.version>1.0.24</vertx-maven-plugin.version>
        <vertx.verticle>com.example.MyApp</vertx.verticle>
    
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      </properties>
    
      <!-- Import dependencies from the Vert.x BOM. -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-dependencies</artifactId>
            <version>${vertx.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <!-- Specify the Vert.x artifacts that your application depends on. -->
      <dependencies>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-core</artifactId>
        </dependency>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-web</artifactId>
        </dependency>
    
        <!-- Test dependencies -->
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-junit5</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.4.0</version>
          <scope>test</scope>
        </dependency>
    
      </dependencies>
    
      <!-- Specify the repositories containing Vert.x artifacts. -->
      <repositories>
        <repository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </repository>
      </repositories>
    
    <!-- Specify the version of the Maven Surefire plugin. -->
        <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
          </plugin>
          <plugin>
    
    <!-- Configure your application to be packaged using the Vert.x Maven Plugin. -->
            <groupId>io.reactiverse</groupId>
            <artifactId>vertx-maven-plugin</artifactId>
            <version>${vertx-maven-plugin.version}</version>
            <executions>
              <execution>
                <id>vmp</id>
                <goals>
                  <goal>initialize</goal>
                  <goal>package</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
  5. Build the application using Maven from the root directory of the application.

    mvn vertx:run
  6. Verify that the application is running.

    Use curl or your browser to verify if your application is running at http://localhost:8080 and returns "Greetings!" as response.

    $ curl http://localhost:8080
    Greetings!