最近是真的忙,好久没写了,再来分享一点新东西!!!
一、 新建gradle项目
①
②选择gradle(如果没有安装gradle,自己下载一个)
③
④选择gradle
下一步,然后输入项目名称和磁盘路径,点击finish。
二、配置vertx依赖
项目打开之后,在build.gradle文件中dependencies里面加入vertx的核心依赖
1 |
compile 'io.vertx:vertx-core:3.4.2' |
在build.gradle最下面加入任务
1 2 3 4 |
task copyjars(type: copy) { from configurations.runtime into 'lib' // 目标位置 } |
build.gradle内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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方法启动
1 2 3 4 5 6 7 8 |
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方法
1 2 3 4 5 6 7 8 9 10 11 |
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方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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方法启动
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/tao_ssh/article/details/78318858
查看更多关于使用IDEA和Gradle构建Vertx项目(图文步骤)的详细内容...