使用 Quarkus 命令行界面 (CLI) 构建 Quarkus 应用程序
quarkus
命令允许您使用底层的项目构建工具来创建项目、管理扩展以及执行基本的构建和开发任务。
安装 CLI
Quarkus CLI 可用于多种面向开发者的包管理器,例如:
如果您已经在使用(或希望使用)这些工具之一,这是安装 Quarkus CLI 和保持其更新的最简单方法。
除了这些包管理器,Quarkus CLI 还可以通过 JBang 进行安装。选择对您来说最方便的替代方案。
-
JBang - 适用于 Linux、macOS 和 Windows
-
SDKMAN! - 适用于 Linux 和 macOS
-
Homebrew - 适用于 Linux 和 macOS
-
Chocolatey - 适用于 Windows
-
Scoop - 适用于 Windows
使用 CLI
使用 --help
显示帮助信息,其中包含所有可用的命令。
quarkus --help
Usage: quarkus [-ehv] [--refresh] [--verbose] [--config=CONFIG]
[-D=<String=String>]... [COMMAND]
Options:
--refresh Refresh the local Quarkus extension registry cache
--config=CONFIG Configuration file
-h, --help Display this help message.
-v, --version Print CLI version information and exit.
-e, --errors Display error messages.
--verbose Verbose mode.
-D=<String=String> Java properties
Commands:
create Create a new project.
app Create a Quarkus application project.
cli Create a Quarkus command-line project.
extension Create a Quarkus extension project
build Build the current project.
dev Run the current project in dev (live coding) mode.
test Run the current project in continuous testing mode.
extension, ext Configure extensions of an existing project.
list, ls List platforms and extensions.
categories, cat List extension categories.
add Add extension(s) to this project.
remove, rm Remove extension(s) from this project.
image Build or push project container image.
build Build a container image.
docker Build a container image using Docker.
buildpack Build a container image using Buildpack.
jib Build a container image using Jib.
openshift Build a container image using OpenShift.
push Push a container image.
deploy Deploy application.
kubernetes Perform the deploy action on Kubernetes.
openshift Perform the deploy action on OpenShift.
knative Perform the deploy action on Knative.
kind Perform the deploy action on Kind.
minikube Perform the deploy action on minikube.
registry Configure Quarkus registry client
list List enabled Quarkus registries
add Add a Quarkus extension registry
remove Remove a Quarkus extension registry
info Display project information and verify versions
health (platform and extensions).
update, up, upgrade Suggest recommended project updates with the
possibility to apply them.
version Display CLI version information.
plugin, plug Configure plugins of the Quarkus CLI.
list, ls List CLI plugins.
add Add plugin(s) to the Quarkus CLI.
remove Remove plugin(s) to the Quarkus CLI.
sync Sync (discover / purge) CLI Plugins.
completion bash/zsh completion: source <(quarkus completion)
虽然本文档是一个有用的参考,但客户端帮助才是最终来源。 如果您没有看到预期的输出,请使用 |
创建新项目
要创建一个新项目,请使用 create
命令 (当未指定时,app
子命令是隐含的)。
quarkus create
-----------
applying codestarts...
📚 java
🔨 maven
📦 quarkus
📝 config-properties
🔧 dockerfiles
🔧 maven-wrapper
🚀 resteasy-codestart
-----------
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /<output-dir>/code-with-quarkus
这将在您的当前工作目录中创建一个名为 code-with-quarkus
的文件夹,使用默认的 groupId
、artifactId
和 version
值 (groupId='org.acme'
、artifactId='code-with-quarkus'
和 version='1.0.0-SNAPSHOT'
)。
上面显示的 emoji 可能不完全匹配。emoji 的外观可能因使用的字体或终端/环境而异。例如,IntelliJ IDEA 有一些关于 emoji 在终端中的行为/渲染的长期存在的公开问题。 |
您可以通过命令行直接使用 group:artifactId:version
坐标语法来指定 groupId
、artifactId
和 version
。您可以选择性地省略段落以使用默认值。
# Create a project with groupId=org.acme, artifactId=bar, and version=1.0.0-SNAPSHOT
quarkus create app bar
# Create a project with groupId=com.foo, artifactId=bar, and version=1.0.0-SNAPSHOT
quarkus create app com.foo:bar
# Create a project with groupId=com.foo, artifactId=bar, and version=1.0
quarkus create app com.foo:bar:1.0
输出将显示您的项目正在创建。
-----------
applying codestarts...
📚 java
🔨 maven
📦 quarkus
📝 config-properties
🔧 dockerfiles
🔧 maven-wrapper
🚀 resteasy-codestart
-----------
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /<output-dir>/bar
-----------
使用 --help
选项显示创建项目的选项。
quarkus create app --help
quarkus create cli --help
指定 Quarkus 版本
quarkus create
和 quarkus extension list
都允许您通过两种方式之一显式指定 Quarkus 版本。
-
指定特定的平台发布 BOM
一个 Quarkus 平台发布 BOM 由
groupId:artifactId:version
(GAV) 坐标标识。在指定平台发布 BOM 时,您可以使用空段来回退到默认值 (在quarkus create app --help
中显示)。如果您只指定一个段 (没有:
),则假定它是一个版本。io.quarkus.platform
是默认的groupId
。指定-P :quarkus-bom:
等同于-P io.quarkus.platform:quarkus-bom:3.24.4
。请注意,您需要指定groupId
才能使用快照,例如-P io.quarkus::999-SNAPSHOT
等同于-P io.quarkus:quarkus-bom:999-SNAPSHOT
。默认值可能会发生变化。使用
--dry-run
选项查看计算后的值。 -
指定平台流
平台流在远程注册表上运行。每个注册表定义一个或多个平台流,每个流定义一个或多个平台发布 BOM 文件,这些文件定义使用该流的项目应该如何配置。
流使用
platformKey:streamId
语法进行标识。可以使用-S platformKey:streamId
指定特定流。在指定流时,空段将被替换为基于流资源解析规则的发现的默认值。
使用扩展
quarkus ext --help
列出扩展
Quarkus CLI 可用于列出 Quarkus 扩展。
quarkus ext ls
结果的格式可以通过以下四种选项之一控制:
-
--name
仅显示名称 (artifactId) -
--concise
显示名称 (artifactId) 和描述 -
--full
显示简洁格式以及版本/状态相关的列。 -
--origins
显示简洁信息以及扩展的 Quarkus 平台发布来源。
quarkus ext ls
的行为会根据上下文而变化。
列出特定 Quarkus 版本的扩展
如果您在项目之外调用 Quarkus CLI,Quarkus 将列出 CLI 本身使用的 Quarkus 版本的所有可用扩展。
您还可以使用 -P
或 -S
列出特定 Quarkus 版本的扩展,如 指定 Quarkus 版本中所述。
此模式默认使用 --origins
格式。
列出 Quarkus 项目的扩展
在处理 Quarkus 项目时,CLI 将列出当前项目已安装的扩展,默认使用 --name
格式。
使用 --installable
或 -i
选项列出可以从项目使用的 Quarkus 平台安装的扩展。
您可以使用搜索 (--search
或 -s
) 来缩小或过滤列表。
quarkus ext list --concise -i -s jdbc
JDBC Driver - DB2 quarkus-jdbc-db2
JDBC Driver - PostgreSQL quarkus-jdbc-postgresql
JDBC Driver - H2 quarkus-jdbc-h2
JDBC Driver - MariaDB quarkus-jdbc-mariadb
JDBC Driver - Microsoft SQL Server quarkus-jdbc-mssql
JDBC Driver - MySQL quarkus-jdbc-mysql
JDBC Driver - Oracle quarkus-jdbc-oracle
JDBC Driver - Derby quarkus-jdbc-derby
Elytron Security JDBC quarkus-elytron-security-jdbc
Agroal - Database connection pool quarkus-agroal
构建您的项目
要使用 Quarkus CLI 构建您的项目 (在本例中,使用默认配置),请使用 build
命令。
quarkus build
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< org.acme:code-with-quarkus >---------------------
[INFO] Building code-with-quarkus 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.331 s
[INFO] Finished at: 2021-05-27T10:13:28-04:00
[INFO] ------------------------------------------------------------------------
输出将根据您的项目使用的构建工具 (Maven、Gradle 或 JBang) 而有所不同。 |
运行 quarkus build --clean 在构建过程中执行清理。 |
开发模式
要从 Quarkus CLI 启动开发模式,请使用 dev
命令。
quarkus dev
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< org.acme:code-with-quarkus >---------------------
[INFO] Building code-with-quarkus 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...
Listening for transport dt_socket at address: 5005
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-05-27 10:15:56,032 INFO [io.quarkus] (Quarkus Main Thread) code-with-quarkus 1.0.0-SNAPSHOT on JVM (powered by Quarkus 999-SNAPSHOT) started in 1.387s. Listening on: https://:8080
2021-05-27 10:15:56,035 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-05-27 10:15:56,035 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, rest, smallrye-context-propagation]
--
Tests paused, press [r] to resume
输出将根据您的项目使用的构建工具 (Maven、Gradle 或 JBang) 而有所不同。 |
运行 quarkus dev --clean 在构建过程中执行清理。 |
Shell 自动补全和别名
Bash 和 Zsh 支持自动命令补全。
# Setup autocompletion in the current shell
source <(quarkus completion)
如果您选择为 quarkus
命令使用别名,请使用以下命令调整命令补全。
# Add an alias for the quarkus command
alias q=quarkus
# Add q to list of commands included in quarkus autocompletion
complete -F _complete_quarkus q
容器镜像
Quarkus CLI 允许在不修改项目配置 (添加/删除容器镜像扩展) 的情况下构建容器镜像。要构建您项目的镜像,请执行:
quarkus image build
image build
命令可以直接使用,也可以选择一个子命令。可用的子命令有:
-
docker
-
buildpacks
-
jib
-
openshift
每个子命令都对应 Quarkus 支持的镜像构建工具,并提供特定的配置选项。
例如,要使用带有自定义构建器镜像的 云原生构建包,请使用以下命令:
quarkus image build buildpack --builder-image <your builder image>
扩展 CLI
Quarkus CLI 嵌入了一个插件系统,可用于动态地为 CLI 添加命令和子命令。
什么是插件
插件可以是任何可在本地找到或远程获取的可执行文件、jar 包或 Java 命令。
插件的分类如下:
-
通过 shell 执行的插件
-
可执行文件 (任何以
quarkus-
开头并在本地找到的可执行文件) -
通过 jbang 执行的插件
-
jar 包 (任何可在本地找到的可运行 jar 包)
-
jbang 别名 (任何以
quarkus-
开头并在本地安装或通过 quarkusio 目录安装的 jbang 别名) -
maven (任何 GACTV 格式的 maven 坐标,指向一个可运行的 jar 包)
如何获取插件
可以通过以下几种方式找到插件。
管理插件
插件使用以下命令进行管理。
列出插件
以下命令列出已安装的插件:
quarkus plugin list
No plugins installed!
To include the installable plugins in the list, append --installable to the command.
列出可用/可安装的插件:
quarkus plugin list --installable
Name Type Scope Location Description
kill jbang user quarkus-kill@quarkusio
* fmt jbang user quarkus-fmt@quarkusio
greeter executable user /home/iocanel/bin/quarkus-greeter
Use the 'plugin add' subcommand and pass the location of any plugin listed above, or any remote location in the form of URL / GACTV pointing to a remote plugin.
list
命令的输出可以通过 -t
按类型过滤,或使用 -s
标志和搜索模式按名称过滤。
例如,要列出所有以字母 k
开头的可安装插件,请使用以下命令:
quarkus plugin list --installable -s "k*"
Name Type Scope Location Description
kill jbang user quarkus-kill@quarkusio
Use the 'plugin add' subcommand and pass the location of any plugin listed above, or any remote location in the form of URL / GACTV pointing to a remote plugin.
添加插件
要添加任何可安装的插件,请使用 quarkus plugin add <name or location>
。
quarkus plugin add kill
Added plugin:
Name Type Scope Location Description
* kill jbang user quarkus-kill@quarkusio
上述命令通过 name
添加了一个插件,其中 name
是通过 quarkus plugin list --installable
列出的名称。
现在可以通过 quarkus kill
命令执行已安装的命令。
用户不限于通过 quarkus plug list --installable 发现的插件。只要用户能提供指向可执行 jar 包或 Java 文件的 URL 或 Maven 坐标,他们就可以安装插件。 |
您可以作为插件通过 Maven 坐标安装可执行 jar 包。
例如,我们使用 io.quarkiverse.authzed:quarkus-authzed-cli:runner:jar:0.2.0
,这是一个真实的、可执行的 jar 包,它为 quarkus-authzed
扩展提供了一个 CLI 工具。
quarkus plugin add io.quarkiverse.authzed:quarkus-authzed-cli:runner:jar:0.2.0 -d "Authzed CLI"
Added plugin:
Name Type Location Description
* authzed maven io.quarkiverse.authzed:quarkus-authzed-cli:runner:jar:0.2.0 Authzed CLI
还可以设置一个在帮助输出中显示的描述。 |
quarkus --help
Usage: quarkus [-ehv] [--refresh] [--verbose] [--config=CONFIG]
[-D=<String=String>]... [COMMAND]
...
Commands:
...
plugin, plug Configure plugins of the Quarkus CLI.
list, ls List CLI plugins.
add Add plugin(s) to the Quarkus CLI.
remove Remove plugin(s) to the Quarkus CLI.
sync Sync (discover / purge) CLI Plugins.
completion bash/zsh completion: source <(quarkus completion)
authzed Authzed CLI
...
插件添加到哪里?
插件被添加到插件目录中,该目录位于:<user home>/.quarkus/cli/plugins/quarkus-cli-catalog.json
。
还有一个第二个插件目录,它相对于当前项目 (如果可用):<project root>/.quarkus/cli/plugins/quarkus-cli-catalog.json
。
有效的目录是 user
和 project
目录的组合,其中后者可以覆盖前者的条目 (例如,为插件使用不同的版本或位置)。
如果项目目录可用,它将始终被优先考虑,除非显式使用 --user
标志。
插件表的 scope
列指示插件在哪里被添加/将被添加。