欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Vert.x Maven插件快速创建项目的方法

程序员文章站 2024-02-24 23:24:28
本文介绍了使用vert.x maven插件快速创建项目的方法,分享给大家,具体如下: 文档地址: 已有项目添加该插件 在项目 pom.xml 目录,执行下面的命...

本文介绍了使用vert.x maven插件快速创建项目的方法,分享给大家,具体如下:

文档地址:

已有项目添加该插件

在项目 pom.xml 目录,执行下面的命令即可添加:

mvn io.reactiverse:vertx-maven-plugin:1.0.17:setup

执行该命令后,在 pom.xml 中会增加下面的配置:

<properties>
 <vertx.version>3.5.3</vertx.version>
 <vertx-maven-plugin.version>1.0.17</vertx-maven-plugin.version>
</properties>
<dependencymanagement>
 <dependencies>
  <dependency>
   <groupid>io.vertx</groupid>
   <artifactid>vertx-stack-depchain</artifactid>
   <version>${vertx.version}</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
 </dependencies>
</dependencymanagement>

还有下面的插件:

<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>
 <configuration>
  <redeploy>true</redeploy>
 </configuration>
</plugin>

自动引入的 vert.x 版本为 3.5.3,你可以通过在 mvn 命令增加 -dvertxversion=3.4.0 这个参数来指定需要的版本。

从头创建空项目

首先你 必须创建一个目录 ,vert.x 插件不会自动给你创建目录,只会创建目录内的 src 和 pom.xml 等文件。

创建一个目录,进入该目录内,在该目录内执行下面的命令:

mvn io.reactiverse:vertx-maven-plugin:1.0.17:setup ^
  -dprojectgroupid=org.acme ^
  -dprojectartifactid=acme-project ^
  -dprojectversion=1.0-snapshot ^
  -dverticle=org.acme.foo ^
  -dverticle=io.vertx.sample.myfirstverticle ^
  -ddependencies=web

如果你使用的 linux 系统,将上面的 ^ 改为 \ 。

这里和上面相比增加了项目 gav 的配置。

通过 -dverticle=io.vertx.sample.myfirstverticle ,可以生成一个默认的 verticle 类。

通过 -ddependencies=web 可以指定你想加入的 vert.x 的依赖,这里写的名字都是缩写,具体对应关系看下面的介绍。

使用上面命令后,就创建了一个基础 vert.x 项目,在开始学习 vert.x 的时候,通过这种方式可以更快的创建基础项目。

-ddependencies 对照表

源码:

下面 json 中的 labels 就是缩写名,groupid 和 artifactid 是对应的依赖。

[
  {
    "name": "vert.x web",
    "labels": [
      "web"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web"
  },
  {
    "name": "vert.x web client",
    "labels": [
      "web-client"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-client"
  },
  {
    "name": "vert.x mongo client",
    "labels": [
      "mongo"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-mongo-client"
  },
  {
    "name": "vert.x kafka client",
    "labels": [
      "kafka"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-kafka-client"
  },
  {
    "name": "vert.x consul client",
    "labels": [
      "consul"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-consul-client"
  },
  {
    "name": "vert.x grpc",
    "labels": [
      "grpc"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-grpc"
  },
  {
    "name": "vert.x (async) jdbc client",
    "labels": [
      "jdbc"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-jdbc-client"
  },
  {
    "name": "vert.x redis client",
    "labels": [
      "redis"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-redis-client"
  },
  {
    "name": "vert.x mail client",
    "labels": [
      "mail",
      "smtp"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-mail-client"
  },
  {
    "name": "vert.x stomp",
    "labels": [
      "stomp"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-stomp"
  },
  {
    "name": "vert.x eventbus bridge using tcp",
    "labels": [
      "tcp-bridge"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-tcp-eventbus-bridge"
  },
  {
    "name": "vert.x - apache camel bridge",
    "labels": [
      "camel"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-camel-bridge"
  },
  {
    "name": "vert.x bridge with amqp",
    "labels": [
      "amqp"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-amqp-bridge"
  },
  {
    "name": "vert.x client for rabbitmq",
    "labels": [
      "rabbitmq"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-rabbitmq-client"
  },
  {
    "name": "vert.x authentication support using jdbc",
    "labels": [
      "jdbc-auth"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-auth-jdbc"
  },
  {
    "name": "vert.x authentication support using jwt",
    "labels": [
      "jwt-auth"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-auth-jwt"
  },
  {
    "name": "vert.x authentication support using mongo",
    "labels": [
      "mongo-auth"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-auth-mongo"
  },
  {
    "name": "vert.x authentication support using shiro",
    "labels": [
      "shiro-auth"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-auth-shiro"
  },
  {
    "name": "vert.x authentication support using oauth 2",
    "labels": [
      "oauth2",
      "oauth2-auth",
      "oauth"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-auth-oauth2"
  },
  {
    "name": "vert.x support for rx java",
    "labels": [
      "rx",
      "rxjava"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-rx-java"
  },
  {
    "name": "vert.x support for javascript (nashorn)",
    "labels": [
      "js",
      "javascript"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-lang-js"
  },
  {
    "name": "vert.x support for kotlin",
    "labels": [
      "kotlin",
      "vertx-kotlin"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-lang-kotlin-compiler"
  },
  {
    "name": "vert.x support for ruby (jruby)",
    "labels": [
      "rb",
      "ruby",
      "jruby"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-lang-ruby"
  },
  {
    "name": "vert.x support for apache groovy",
    "labels": [
      "groovy"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-lang-groovy"
  },
  {
    "name": "vert.x service discovery",
    "labels": [
      "discovery",
      "service-discovery"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-service-discovery"
  },
  {
    "name": "vert.x circuit breaker",
    "labels": [
      "circuit-breaker",
      "circuit"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-circuit-breaker"
  },
  {
    "name": "vert.x service discovery for kubernetes",
    "labels": [
      "discovery-kubernetes",
      "service-discovery-kubernetes"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-service-discovery-bridge-kubernetes"
  },
  {
    "name": "vert.x service discovery for consul",
    "labels": [
      "discovery-consul",
      "service-discovery-consul"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-service-discovery-bridge-consul"
  },
  {
    "name": "vert.x metrics using dropwizard",
    "labels": [
      "jmx",
      "dropwizard"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-dropwizard-metrics"
  },
  {
    "name": "vert.x metrics using hawkular",
    "labels": [
      "hawkular"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-hawkular-metrics"
  },
  {
    "name": "vert.x shell",
    "labels": [
      "shell"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-shell"
  },
  {
    "name": "vert.x unit",
    "labels": [
      "test"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-unit",
    "scope": "test"
  },
  {
    "name": "vert.x cluster manager based on hazelcast",
    "labels": [
      "hazelcast",
      "hazelcast-cluster-manager"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-hazelcast"
  },
  {
    "name": "vert.x cluster manager based on infinipan",
    "labels": [
      "infinispan",
      "infinispan-cluster-manager"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-infinispan"
  },
  {
    "name": "vert.x cluster manager based on zookeeper",
    "labels": [
      "zookeeper-cluster-manager"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-zookeeper"
  },
  {
    "name": "vert.x cluster manager based on apache ignite",
    "labels": [
      "ignite"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-ignite"
  },
  {
    "name": "vert.x cluster manager based on apache zookeeper",
    "labels": [
      "zookeeper"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-zookeeper"
  },
  {
    "name": "vert.x web template engine based on pebble",
    "labels": [
      "pebble",
      "pebble-template",
      "pebble-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-pebble",
    "classifier": "shaded"
  },
  {
    "name": "vert.x web template engine based on apache freemarker",
    "labels": [
      "freemarker",
      "freemarker-template",
      "freemarker-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-freemarker",
    "classifier": "shaded"
  },
  {
    "name": "vert.x web template engine based on thymeleaf",
    "labels": [
      "thymeleaf",
      "thymeleaf-template",
      "thymeleaf-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-thymeleaf",
    "classifier": "shaded"
  },
  {
    "name": "vert.x web template engine based on handlebars",
    "labels": [
      "handlebars",
      "handlebars-template",
      "handlebars-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-handlebars",
    "classifier": "shaded"
  },
  {
    "name": "vert.x web template engine based on jade",
    "labels": [
      "jade",
      "jade-template",
      "jade-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-jade",
    "classifier": "shaded"
  },
  {
    "name": "vert.x web template engine based on mvel",
    "labels": [
      "mvel",
      "mvel-template",
      "mvel-template-engine"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-web-templ-mvel",
    "classifier": "shaded"
  },
  {
    "name": "vert.x (async) rpc service proxies",
    "labels": [
      "service-proxies",
      "rpc-services"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-service-proxy"
  },
  {
    "name": "vert.x service factory using apache maven",
    "labels": [
      "maven-service-factory"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-maven-service-factory"
  },
  {
    "name": "vert.x service factory",
    "labels": [
      "service-factory"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-service-factory"
  },
  {
    "name": "vert.x service factory using http",
    "labels": [
      "http-service-factory"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-http-service-factory"
  },
  {
    "name": "vert.x configuration",
    "labels": [
      "config"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config"
  },
  {
    "name": "vert.x configuration with kubernetes configmap",
    "labels": [
      "config-kubernetes",
      "config-config-map",
      "config-configmap"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-kubernetes-configmap"
  },
  {
    "name": "vert.x configuration with a git repository",
    "labels": [
      "config-git"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-git"
  },
  {
    "name": "vert.x configuration - hocon format",
    "labels": [
      "config-hocon"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-hocon"
  },
  {
    "name": "vert.x configuration - yaml format",
    "labels": [
      "config-yaml"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-yaml"
  },
  {
    "name": "vert.x configuration with a zookeeper backend",
    "labels": [
      "config-zookeeper"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-zookeeper"
  },
  {
    "name": "vert.x configuration with a redis backend",
    "labels": [
      "config-redis"
    ],
    "groupid": "io.vertx",
    "artifactid": "vertx-config-redis"
  }
]

打包项目

集成了 vert.x 插件后,打包变的极其容易,只需要下面的命令:

mvn clean package

通过这种方式就会打出一个 fat jar 包,可以直接通过 java -jar xxxx.jar 运行的包。

其他命令

除了上面这些,还有 vertx:run, vertx:debug, vertx:start, vertx:stop 命令,这些命令可以在不打 jar 包的情况下运行或者关闭项目。

常见问题

通过插件方式运行时,你经常会遇到下面的问题:

[info] 严重: java.net.bindexception: address already in use: bind

这种情况是因为使用类似 idea maven 插件中的命令直接双击运行时,你无法关闭这个应用。如果你使用的纯命令行,直接 ctrl+c 就能关闭。

使用 vertx:run 在命令行运行时可以直接关闭。

使用 vertx:start 运行时,需要通过 vertx:stop 关闭。

万一遇上无法关闭的情况,在 windows 中,可以用下面方式解决。

在命令行中,输入下面的命令:

jps -m

这个命令会列出所有运行的 jvm:

15248 jps -m
2384 launcher run io.vertx.sample.myfirstverticle redeploy-termination-period=1000 -dvertx.id=5fd656fa-55a9-46b4-8d23-caa95f2e5032-redeploy
8208
11844 launcher run io.vertx.sample.myfirstverticle redeploy-termination-period=1000 -dvertx.id=bec46d01-d441-4949-a2d9-f8ffbe85f965-redeploy
14200 launcher run io.vertx.sample.myfirstverticle --redeploy=f:\git\my-first-vertx-app\target\classes/**/* --redeploy-scan-period=1000 redeploy-termi
nation-period=1000 --launcher-class=io.vertx.core.launcher
7580 launcher clean compile vertx:run

根据后面的信息找到你想关闭的 jvm。输入下面的命令(假设关闭 2384):

taskkill /f /pid 2384

当相同端口的 jvm 关闭后,你就可以再次运行了。

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