首页 > 编程 > Java > 正文

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

2019-11-26 09:39:32
字体:
来源:转载
供稿:网友

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

一、 新建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.5repositories {  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方法启动

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表