第 3 章 使用 POM 文件创建 Eclipse Vert.x 项目


当您开发基本的 Eclipse Vert.x 应用程序时,您应该创建以下工件。我们将在我们首先 启动的 Eclipse Vert.x 项目中创建这些工件。

  • 包含 Eclipse Vert.x 方法的 Java 类。
  • 包含 Maven 构建应用程序所需的信息的 pom.xml 文件。

以下流程创建了一个简单的 Greeting 应用程序,它返回 Greetings! 作为响应。

注意

Eclipse Vert.x 支持基于 OpenJDK 8 和 OpenJDK 11 的构建器镜像,用于构建和部署应用程序到 OpenShift。不支持 Oracle JDK 和 OpenJDK 9 构建器镜像。

前提条件

  • 安装了 OpenJDK 8 或 OpenJDK 11。
  • 已安装 Maven。

流程

  1. 创建新目录 getting-started 并浏览到其中。

    $ mkdir getting-started
    $ cd getting-started

    这是应用的根目录。

  2. 在根目录中创建目录结构 src/main/java/com/example/,然后导航到.

    $ mkdir -p src/main/java/com/example/
    $ cd src/main/java/com/example/
  3. 创建包含应用代码的 Java 类文件 MyApp.java

    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());
                    }
                });
        }
    }

    该应用在端口 8080 上启动 HTTP 服务器。向您发送不动时,它又会得到 回报! 我很好。

  4. 在应用程序根目录中创建一个 pom.xml 文件,其内容如下:

    • 在 & lt;dependencyManagement& gt; 部分中,添加 io.vertx:vertx-dependencies 构件。
    • 类型指定为 pom范围 指定为 导入
    • 在 & lt;project& gt; 部分的 <properties > 下,指定 Eclipse Vert.x 和 Eclipse Vert.x Maven 插件的版本。

      注意

      属性可用于设置在每个版本中更改的值。例如,产品或插件的版本。

    • 在 &lt ;project&gt; 部分中,在 & lt;plugin& gt; 下,指定 vertx-maven-plugin。Eclipse Vert.x Maven 插件用于打包您的应用程序。
    • 包含 软件仓库 和插件,以指定 包含要构建应用程序的工件和插件的存储库。

      pom.xml 包含以下工件:

    <?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.3.7.redhat-00002</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. 从应用的根目录使用 Maven 构建应用。

    mvn vertx:run
  6. 验证应用是否正在运行。

    使用 curl 或浏览器验证您的应用程序是否在 http://localhost:8080 中运行,并返回 "Greetings!" 作为回答。

    $ curl http://localhost:8080
    Greetings!
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.