quarkus实战之五:细说maven插件

  • quarkus的maven插件非常重要,管理和构建工程时都离不开,本篇就来一起了解和掌握它

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

本篇概览

  • 本文是《quarkus实战》系列的第五篇,一起去熟悉quarkus的maven插件(就是下图红框中的那个plugin),用好它可以使我们更加得心应手的在项目中配置和控制quarkus

quarkus实战之五:细说maven插件

  • 插件quarkus-maven-plugin提供了丰富的功能,它们都有对应的命令,执行mvn quarkus:xxx即可执行,其中xxx就是具体的命令,例如mvn quarkus:help,接下来一起用实际操作来熟悉这些命令

帮助(help)

  • mvn quarkus:help是首先要掌握的命令,它告诉了大家quarkus的maven插件到底有哪些能力,作为工具书,今后随时可能用到,如下所示,篇幅所限,仅列出部分内容:
% mvn quarkus:help [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:help (default-cli) @ hello-quarkus --- [INFO] Quarkus Platform - Quarkus Maven Plugin 2.7.3.Final   Quarkus Universe platform aggregates extensions from Quarkus Core and those   developed by the community into a single compatible and versioned set that   application developers can reference from their applications to align the   dependency versions  This plugin has 25 goals:  quarkus:add-extension   Allow adding an extension to an existing pom.xml file. Because you can add one   or several extension in one go, there are 2 mojos: add-extensions and   add-extension. Both supports the extension and extensions parameters.  quarkus:add-extensions   Allow adding extensions to an existing pom.xml file. Because you can add one   or several extension in one go, there are 2 mojos: add-extensions and   add-extension. Both supports the extension and extensions parameters.  quarkus:analyze-call-tree   Analyze call tree of a method or a class based on an existing report produced   by Substrate when using -H:+PrintAnalysisCallTree, and does a more meaningful   analysis of what is causing a type to be retained.  quarkus:build   Builds the Quarkus application. ...   

创建工程(create)

  • 首先要掌握的mvn命令是创建工程,回顾前文中创建工程的命令:
mvn "io.quarkus:quarkus-maven-plugin:create"    -DprojectGroupId="com.bolingcavalry"    -DprojectArtifactId="hello-quarkus"    -DprojectVersion="1.0-SNAPSHOT"    -DclassName="HobbyResource"    -Dpath="actions" 
  • 创建工程时有很多参数可以设置,它们的默认值的作用如下表
参数 默认值 说明
projectGroupId org.acme.sample group id,GAV坐标中的G
projectArtifactId mandatory artifact id, GAV坐标中的A
projectVersion 1.0.0-SNAPSHOT version, GAV坐标中的V
platformGroupId io.quarkus.platform 在依赖quarkus的BOM,以及quarkus插件时,都要指定GAV,这是G
platformArtifactId quarkus-bom 在依赖quarkus的BOM,以及quarkus插件时,都要指定GAV,这是A
platformVersion 2.7.1.Final 在依赖quarkus的BOM,以及quarkus插件时,都要指定GAV,这是V,默认值会自动更新到最新稳定版
className 无默认值 指定后,会自动创建一个类
path 无默认值 如果指定了className,此时再指定path的话,path会作为类的web接口的path
extensions 所需的quarkus插件列表
quarkusRegistryClient true 是否联网获取最新的quarkus插件列表,如果设置为false,列表只能根据BOM获取,得不到最新的在线数据

查看工程信息(info)

  • 查看工程信息的命令是mvn quarkus:info,使用频率很高,如下,可以观察当前的BOM和扩展插件情况:
(base) willdeMBP:hello-quarkus will$ mvn quarkus:info [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.1.Final:info (default-cli) @ hello-quarkus --- [WARNING] quarkus:info goal is experimental, its options and output may change in future versions [INFO] Looking for the newly published extensions in registry.quarkus.io [INFO] Quarkus platform BOMs: [INFO]   io.quarkus:quarkus-bom:pom:2.7.1.Final [INFO]  [INFO] Extensions from io.quarkus:quarkus-bom: [INFO]   io.quarkus:quarkus-arc [INFO]   io.quarkus:quarkus-resteasy [INFO]  [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time:  4.828 s [INFO] Finished at: 2022-03-04T08:40:44+08:00 [INFO] ------------------------------------------------------------------------ 

paltform列表(list-platforms)

  • 回顾项目的pom.xml文件,如下图,红框中是依赖的平台信息,所有quarkus依赖库都由这个平台统一管理

quarkus实战之五:细说maven插件

  • 上图红框中的平台,在quarkus中有多个,我们可以按照自己的实际情况选择,quarkus插件的list-platforms命令可以查看一共有哪些平台类型,完整命令是
mvn quarkus:list-platforms 
  • 控制台输出所有平台信息
% mvn quarkus:list-platforms [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:list-platforms (default-cli) @ hello-quarkus --- [INFO] io.quarkus.platform:quarkus-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-optaplanner-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-kogito-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-qpid-jms-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-cassandra-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-amazon-services-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-camel-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-hazelcast-client::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-debezium-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-blaze-persistence-bom::pom:2.7.3.Final [INFO] io.quarkus.platform:quarkus-google-cloud-services-bom::pom:2.7.3.Final [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 

扩展组件:查看(list-extensions)

  • 扩展组件(extensions)是quarkus的一大特色,web、序列化、数据库等丰富的功能,都有对应的组件提供
  • 命令mvn quarkus:list-extensions可以查看支持的扩展组件列表:
(base) willdeMBP:hello-quarkus will$ mvn quarkus:list-extensions [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.1.Final:list-extensions (default-cli) @ hello-quarkus --- [INFO] Looking for the newly published extensions in registry.quarkus.io [INFO] Current Quarkus extensions available:  [INFO]  [INFO] ✬ ArtifactId                                         Extension Name [INFO] ✬ quarkus-agroal                                     Agroal - Database connection pool [INFO] ✬ quarkus-amazon-lambda                              AWS Lambda [INFO] ✬ quarkus-amazon-lambda-http                         AWS Lambda HTTP [INFO] ✬ quarkus-amazon-lambda-rest                         AWS Lambda Gateway REST API [INFO] ✬ quarkus-amazon-lambda-xray                         AWS Lambda X-Ray [INFO] ✬ quarkus-apicurio-registry-avro                     Apicurio Registry - Avro [INFO] ✬ quarkus-avro                                       Apache Avro [INFO] ✬ quarkus-awt                                        AWT [INFO] ✬ quarkus-azure-functions-http                       Azure Functions HTTP [INFO] ✬ quarkus-cache                                      Cache [INFO] ✬ quarkus-config-yaml                                YAML Configuration ... 
  • 如果想看更丰富的信息,可以增加参数-Dformat=full,如下图红框所示,新增了两个字段:
    quarkus实战之五:细说maven插件

扩展组件:查看分类(list-categories)

  • 直接用list-extensions命令可以看到所有扩展组件,我们当然不会全部都用,所以最好能有个分类的功能,然后我们在合适的类目中找寻自己需要的组件
  • quarkus已经为我们准备好了分类功能,首先,执行以下命令查看有哪些分类(-Dformat参数可选,有了它会展示更详细的信息)
mvn quarkus:list-categories -Dformat=full 
  • 控制台输出如下,列出了所有分类和说明
% mvn quarkus:list-categories -Dformat=full [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:list-categories (default-cli) @ hello-quarkus --- [INFO] Available Quarkus extension categories:  [INFO]  [INFO] Category                       CategoryId           Description [INFO] Alternative languages          alt-languages        Support for other JVM based languages [INFO] Alternative languages          alternative-languages Support for other JVM based languages [INFO] Business Automation            business-automation  Rules engine, BPM, etc [INFO] Cloud                          cloud                Useful for Cloud Native deployments platforms like Kubernetes and cloud providers [INFO] Compatibility                  compatibility        Support for alternative programming models on Quarkus [INFO] Core                           core                 Core Quarkus components: engine, logging, etc. [INFO] Data                           data                 Accessing and managing your data (RDBMS, NoSQL, caching, transaction management, etc) [INFO] Integration                    integration          Connectors to read to write from a skew of systems (file, S#, Twitter, etc) [INFO] Messaging                      messaging            Send and receives message to various messaging systems (AMQP, KAfka etc) [INFO] Miscellaneous                  miscellaneous        Mixed bag of good stuff [INFO] Observability                  observability        Metrics, tracing, etc [INFO] Reactive                       reactive             Non blocking stack and connectors [INFO] Security                       security             Everything you need to secure your application [INFO] Serialization                  serialization        Serializing and deserializing various formats [INFO] Web                            web                  Everything you need for REST endpoints, HTTP and web formats like JSON [INFO] gRPC                           grpc                 gRPC integration [INFO]  [INFO] To list extensions in given category, use: `./mvnw quarkus:list-extensions -Dcategory="categoryId"` [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 
  • 如果我对消息相关的组件感兴趣,可以看messaging这个类目,命令是
mvn quarkus:list-extensions -Dcategory="messaging" 
  • 此时控制台会列出messaging这个类目下的所有组件
% mvn quarkus:list-extensions -Dcategory="messaging"               [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:list-extensions (default-cli) @ hello-quarkus --- [INFO] Current Quarkus extensions available:  [INFO]  [INFO] ✬ ArtifactId                                         Extension Name [INFO]   quarkus-artemis-jms                                Artemis JMS [INFO] ✬ quarkus-google-cloud-pubsub                        Google Cloud Pubsub [INFO] ✬ quarkus-kafka-client                               Apache Kafka Client [INFO] ✬ quarkus-kafka-streams                              Apache Kafka Streams [INFO] ✬ quarkus-qpid-jms                                   AMQP 1.0 JMS client - Apache Qpid JMS [INFO]   quarkus-rabbitmq-client                            RabbitMQ Client [INFO]   quarkus-reactive-messaging-http                    Reactive HTTP and WebSocket Connector [INFO] ✬ quarkus-smallrye-reactive-messaging                SmallRye Reactive Messaging [INFO] ✬ quarkus-smallrye-reactive-messaging-amqp           SmallRye Reactive Messaging - AMQP Connector [INFO] ✬ quarkus-smallrye-reactive-messaging-kafka          SmallRye Reactive Messaging - Kafka Connector [INFO] ✬ quarkus-smallrye-reactive-messaging-mqtt           SmallRye Reactive Messaging - MQTT Connector [INFO] ✬ quarkus-smallrye-reactive-messaging-rabbitmq       SmallRye Reactive Messaging - RabbitMQ Connector [INFO]  [INFO] To get more information, append `-Dformat=full` to your command line. [INFO]  [INFO] Add an extension to your project by adding the dependency to your pom.xml or use `./mvnw quarkus:add-extension -Dextensions="artifactId"` [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 

扩展组件:添加(add-extension、add-extensions)

  • mvn quarkus:list-extensions列出所有扩展组件后,可根据实际需要将列出的组件加入自己项目,例如我需要quarkus-jackson这个组件,执行以下命令即可
mvn quarkus:add-extension -Dextensions="quarkus-jackson" 
  • 控制台输出如下,提示添加成功
% mvn quarkus:add-extension -Dextensions="quarkus-jackson" [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:add-extension (default-cli) @ hello-quarkus --- [INFO] [SUCCESS] ✅  Extension io.quarkus:quarkus-jackson has been installed [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time:  3.756 s [INFO] Finished at: 2022-03-05T15:36:40+08:00 [INFO] ------------------------------------------------------------------------ 
  • 再去看pom.xml,如下图红框,文件已被改动,新增依赖quarkus-jackson

quarkus实战之五:细说maven插件

  • 还可以同时添加多个,命令是add-extensions,注意多了个s,多个组件之间用逗号分隔
mvn quarkus:add-extensions -Dextensions="quarkus-jdbc-mysql,quarkus-config-yaml" 
  • 为了方便用户,add-extensions除了精确匹配,还会模糊查找,输入下面的命令试试
quarkus:add-extensions -Dextensions="agroal" 
  • 控制台输出如下,quarkus-agroal会被找到并添加
% mvn quarkus:add-extensions -Dextensions="agroal"    [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:add-extensions (default-cli) @ hello-quarkus --- [INFO] [SUCCESS] ✅  Extension io.quarkus:quarkus-agroal has been installed [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time:  0.832 s [INFO] Finished at: 2022-03-05T16:16:53+08:00 [INFO] ------------------------------------------------------------------------ 
  • 当匹配结果有多个的时候,quarkus插件会提示失败,以jdbc为例,如下所示,有多个匹配结果,因此quarkus插件不知道该选哪个,就报错了:
% mvn quarkus:add-extension -Dextensions="jdbc"    [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:add-extension (default-cli) @ hello-quarkus --- [INFO] [ERROR] ❗  Multiple extensions matching 'jdbc'      - io.quarkus:quarkus-agroal      - io.quarkus:quarkus-elytron-security-jdbc      - io.quarkus:quarkus-jdbc-db2      - io.quarkus:quarkus-jdbc-derby      - io.quarkus:quarkus-jdbc-h2      - io.quarkus:quarkus-jdbc-mariadb      - io.quarkus:quarkus-jdbc-mssql      - io.quarkus:quarkus-jdbc-mysql      - io.quarkus:quarkus-jdbc-oracle      - io.quarkus:quarkus-jdbc-postgresql      - org.kie.kogito:kogito-addons-quarkus-persistence-jdbc      - org.apache.camel.quarkus:camel-quarkus-jdbc      Be more specific e.g using the exact name or the full GAV. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time:  3.500 s [INFO] Finished at: 2022-03-05T16:18:34+08:00 [INFO] ------------------------------------------------------------------------ 
  • 再用info命令查看当前工程,可以看到刚才添加的组件
[INFO] Quarkus platform BOMs: [INFO]   io.quarkus.platform:quarkus-bom:pom:2.7.3.Final [INFO]  [INFO] Extensions from io.quarkus.platform:quarkus-bom: [INFO]   io.quarkus:quarkus-jackson [INFO]   io.quarkus:quarkus-config-yaml [INFO]   io.quarkus:quarkus-arc [INFO]   io.quarkus:quarkus-jdbc-mysql [INFO]   io.quarkus:quarkus-resteasy [INFO]   io.quarkus:quarkus-agroal [INFO]  [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 

扩展组件:删除(remove-extension、remove-extensions)

  • 如果要删除已添加的quarkus组件,例如quarkus-jackson,执行以下命令即可
mvn quarkus:remove-extension -Dextensions="quarkus-jackson" 
  • 控制台输入如下,提示删除成功,再去看pom.xml发现quarkus-jackson的依赖也已经被删除了
% mvn quarkus:remove-extension -Dextensions="quarkus-jackson"     [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:remove-extension (default-cli) @ hello-quarkus --- [INFO] [SUCCESS] ✅  Extension io.quarkus:quarkus-jackson has been uninstalled [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 
  • 也有批量删除功能,命令是remove-extensions,注意多了个s,多个组件之间用逗号分隔
mvn quarkus:remove-extensions -Dextensions="quarkus-config-yaml,quarkus-jdbc-mysql" 
  • 控制台输出
% mvn quarkus:remove-extensions -Dextensions="quarkus-config-yaml,quarkus-jdbc-mysql" [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:remove-extensions (default-cli) @ hello-quarkus --- [INFO] [SUCCESS] ✅  Extension io.quarkus:quarkus-config-yaml has been uninstalled [INFO] [SUCCESS] ✅  Extension io.quarkus:quarkus-jdbc-mysql has been uninstalled [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 
  • 再用info命令查看当前工程,被删除的组件已不在列表
% mvn quarkus:info [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.3.Final:info (default-cli) @ hello-quarkus --- [WARNING] quarkus:info goal is experimental, its options and output may change in future versions [INFO] Looking for the newly published extensions in registry.quarkus.io [INFO] Quarkus platform BOMs: [INFO]   io.quarkus.platform:quarkus-bom:pom:2.7.3.Final [INFO]  [INFO] Extensions from io.quarkus.platform:quarkus-bom: [INFO]   io.quarkus:quarkus-arc [INFO]   io.quarkus:quarkus-resteasy [INFO]   io.quarkus:quarkus-agroal [INFO]  [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ 

查看依赖树(dependency-tree)

  • 我们常用mvn dependency:tree命令查看maven工程的依赖树,此命令是根据pom.xml的依赖信息分析出所有直接和间接依赖关系,然而,在quarkus应用中存在一些特殊情况:quarkus的maven插件参与了构建,它会根据当前的quarkus扩展组件(extensions)去添加对应的依赖,这是maven本身不知道的,因此,推荐使用mvn quarkus:dependency-tree命令查看quarkus应用的依赖关系:
(base) willdeMBP:hello-quarkus will$ mvn quarkus:dependency-tree [INFO] Scanning for projects... [INFO]  [INFO] ------------------< com.bolingcavalry:hello-quarkus >------------------- [INFO] Building hello-quarkus 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO]  [INFO] --- quarkus-maven-plugin:2.7.1.Final:dependency-tree (default-cli) @ hello-quarkus --- [INFO] Quarkus application PROD mode build dependency tree: [INFO] com.bolingcavalry:hello-quarkus:pom:1.0-SNAPSHOT [INFO] ├─ io.quarkus:quarkus-arc-deployment:jar:2.7.1.Final (compile) [INFO] │  ├─ io.quarkus:quarkus-core-deployment:jar:2.7.1.Final (compile) [INFO] │  │  ├─ org.aesh:readline:jar:2.2 (compile) [INFO] │  │  │  └─ org.fusesource.jansi:jansi:jar:1.18 (compile) [INFO] │  │  ├─ org.aesh:aesh:jar:2.6 (compile) [INFO] │  │  ├─ org.apache.commons:commons-lang3:jar:3.12.0 (compile) [INFO] │  │  ├─ org.wildfly.common:wildfly-common:jar:1.5.4.Final-format-001 (compile) ... 

离线构建(go-offline)

  • 先回顾一个maven命令mvn dependency:go-offline,其作用是预先下载所有依赖库到本地,这样构建的时候就算没有网络,也能成功构建
  • 但是对于quarkus应用,因为quarkus扩展组件(extensions)不能被maven识别和处理,因此无法分析出这些quarkus扩展组件的依赖库,导致mvn dependency:go-offline命令无法下载这些依赖库
  • 为了解决上述问题,建议使用mvn quarkus:go-offline命令,此命令会分析扩展组件的依赖项,然后下载缓存到本地

更多信息

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

发表评论

评论已关闭。

相关文章