IDEA和Gradle构建Vertx 使用IDEA和Gradle构建Vertx项目(图文步骤)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

IDEA和Gradle构建Vertx 使用IDEA和Gradle构建Vertx项目(图文步骤)

yhdw   2021-03-30 我要评论

最近是真的忙,好久没写了,再来分享一点新东西!!!

一、 新建Gradle项目


 

②选择Gradle(如果没有安装gradle,自己下载一个)

 


 

④选择gradle

 

下一步,然后输入项目名称和磁盘路径,点击Finish。

二、配置vertx依赖

项目打开之后,在build.gradle文件中dependencies里面加入vertx的核心依赖

compile 'io.vertx:vertx-core:3.4.2'

在build.gradle最下面加入任务

task copyJars(type: Copy) {
  from configurations.runtime
  into 'lib' // 目标位置
}

build.gradle内容

group 'test'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
  mavenCentral()
}

dependencies {
  compile 'io.vertx:vertx-core:3.4.2'
  testCompile group: 'junit', name: 'junit', version: '4.11'
}

task copyJars(type: Copy) {
  from configurations.runtime
  into 'lib' // 目标位置
}

执行这个任务(命令行 gradle copyJars或者在右侧找copyJars双击),会将依赖jar下载到项目根目录下的lib目录

然后右击lib –> Add as Library…

如果没有依赖就会报错

三、 创建Java项目

①创建Module

②创建Class

创建web服务的方式

1、直接main方法启动

import io.vertx.core.Vertx;

public class App1 {
  public static void main(String[] args) {
    Vertx.vertx().createHttpServer().requestHandler(req -> req.response().
        end("Hello Vertx!")).listen(8989);
  }
}

在地址栏输入 localhost:8989就可以看到Hello Vertx!

2、继承Application重写start方法

import io.vertx.core.Vertx;
import javafx.application.Application;
import javafx.stage.Stage;

public class App2 extends Application {
  @Override
  public void start(Stage primaryStage) throws Exception {
    Vertx.vertx().createHttpServer().requestHandler(req -> req.response().
        end("Hello My Application!")).listen(8888);
  }
}

3、继承AbstractVerticle重写start方法

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;

public class App3 extends AbstractVerticle {

  @Override
  public void start() {
    Vertx.vertx()
        .createHttpServer()
        .requestHandler(r -> {
          r.response().end("Hello Verticle !!!");
        })
        .listen(8787);
  }

  public static void main(String[] args) {
    App3 app = new App3();
    app.start();
  }
}

通过main方法启动

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们