如何从 IntelliJ IDEA 启动 Vert.x 服务器?

How to start Vert.x server from IntelliJ IDEA?

如何从 IntelliJ IDEA 中启动一个简单的 Vert.x 服务器?

我的build.gradle如下:

apply plugin: 'java'

version = '3.0.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.vertx:vertx-core:3.0.0'
}

我的 Vertx-server,MyVertex.java 如下:

package com.example;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

public class MyVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> fut) {
        vertx.createHttpServer()
                .requestHandler(r -> r.response().end("<h1>Hello</h1>"))
                .listen(8081);
    }
}

我的 IntelliJ 运行 配置如下,其中 io.vertx.core.Starter 作为主要 class:

但是当我 运行 它与我的 运行 配置时,我收到此错误消息:

Error: Could not find or load main class run

VM 选项(在 运行 配置中)run 是我需要安装并添加到我的路径中的东西吗?或者我如何开始 Vert.x 服务器开发?

啊,我错了:

run com.example.MyVerticle 应该是 程序参数的值: 而不是 IntelliJ IDEA VM 选项 运行 配置.

你必须使用这个:org.vertx.java.platform.impl.cli.Starter 作为你在 IntelliJ IDEA 中的 Main Class;如果您正在使用参数和类似的东西,您可能想要使用类似的东西:runmod <groupId>~<artifactId>~<version> [-conf src/main/resources/your_config.json -cp]

看看这个project

对于 Vert.x 3.0.0 你必须使用这个:io.vertx.core.Starter 作为你的 Main Class 和 run com.example.other.AnyVerticle 作为你的程序参数。

我正在使用 vertx 3.2.1,它抱怨 io.vertx.core.Starter。现在已弃用。所以,应该使用 io.vertx.core.Launcher.

这是一个通过 intellij 启动的示例,可以选择指定配置 JSON 文件:

  • 主要Class:io.vertx.core.Launcher
  • 虚拟机选项:<up to you, or leave blank>
  • 程序参数:run com.app.verticle.MyVerticle -conf /path/to/my_config.json

当使用日志框架时,它将被添加到 VM 选项中,如下所示。

Log4j 与 log4j 或 slf4j delgate:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=log4j.xml

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=log4j.xml

登录:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlogback.configurationFile=logback.xml

只需将此添加到您的 MyVerticle(或单独的 class):

import io.vertx.core.Launcher;
...
public static void main(final String[] args) {
    Launcher.executeCommand("run", MyVerticle.class.getName());
}

然后只需 Ctrl+Shift+F10 到 运行 它和 IntelliJ 将自动创建 Run Configuration.

您可以简单地添加一个 main 并使用 deployVerticle(),然后在 IntelliJ 中您可以 运行 或轻松调试它。 使用 deployVerticle,您可以传递 main/bootstrap verticle 的新实例,也可以传递 yourMainVerticle.class

public class VertxVerticleMain {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new MyVerticle());
       //vertx.deployVerticle(MyVerticle.class);

    }
}