第 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。
流程
创建新目录
getting-started
并浏览到其中。$ mkdir getting-started $ cd getting-started
这是应用的根目录。
在根目录中创建目录结构
src/main/java/com/example/
,然后导航到.$ mkdir -p src/main/java/com/example/ $ cd src/main/java/com/example/
创建包含应用代码的 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 服务器。向您发送不动时,它又会得到 回报! 我很好。
在应用程序根目录中创建一个
pom.xml
文件,其内容如下:-
在 &
lt;dependencyManagement&
gt; 部分中,添加io.vertx:vertx-dependencies
构件。 -
将
类型指定为
pom
,范围
指定为导入
。 在 &
lt;project&
gt; 部分的<properties
> 下,指定 Eclipse Vert.x 和 Eclipse Vert.x Maven 插件的版本。注意属性可用于设置在每个版本中更改的值。例如,产品或插件的版本。
-
在 <
;project>
; 部分中,在 <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>
-
在 &
从应用的根目录使用 Maven 构建应用。
mvn vertx:run
验证应用是否正在运行。
使用
curl
或浏览器验证您的应用程序是否在http://localhost:8080
中运行,并返回 "Greetings!" 作为回答。$ curl http://localhost:8080 Greetings!