5.8. 导出到 Camel Spring Boot
您可以将 Camel CLI 集成 导出到 基于 Java 的传统项目,如 Spring Boot 或 Quarkus。在使用 Camel CLI 构建原型后,您可能需要执行此操作,并需要更需要 Java 编码的传统项目,或使用 Spring Boot、Quarkus 或 vanilla Camel Main 的强大的运行时。
5.8.1. 导出到 Camel Spring Boot 复制链接链接已复制到粘贴板!
命令 export --runtime=spring-boot 会将您当前的 Camel CLI 文件导出到基于 Maven 的 Spring Boot 项目,并在 src/main/ 文件夹结构中组织的文件。
例如,要使用 Maven groupId com.foo 和 artifactId acme 和版本 1.0-SNAPSHOT 导出到 Spring Boot,请运行:
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT
这将导出到 当前目录,这意味着文件被移到所需的文件夹结构中。
要导出到另一个目录,请运行:
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT --directory=../myproject
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT --directory=../myproject
当导出到 Spring Boot 时,pom.xml 或 build.gradle 中定义的 Camel 版本与 Camel CLI 使用的版本相同。但是,您可以指定不同的 Camel 版本,如下所示:
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT --directory=../myproject --camel-spring-boot-version=4.4.0.redhat_00028
camel export --runtime=spring-boot --gav=com.foo:acme:1.0-SNAPSHOT --directory=../myproject --camel-spring-boot-version=4.4.0.redhat_00028
如需了解更多详细信息,请参阅运行 camel export --help 命令。
5.8.2. 使用 Camel CLI 导出包括 复制链接链接已复制到粘贴板!
当导出到 Spring Boot、Quarkus 或 Camel Main 时,Camel JBang CLI 不会开箱即用。要继续使用 Camel CLI (即 camel),您需要在- deps 选项中添加 camel:cli-connector,如下所示:
camel export --runtime=quarkus --gav=com.foo:acme:1.0-SNAPSHOT --deps=camel:cli-connector --directory=../myproject
camel export --runtime=quarkus --gav=com.foo:acme:1.0-SNAPSHOT --deps=camel:cli-connector --directory=../myproject
5.8.3. 配置导出 复制链接链接已复制到粘贴板!
export 命令默认从 application.properties 文件加载配置,该文件用于导出特定参数,如选择运行时和 java 版本。
与 导出 相关的以下选项可以在 application.properties 文件中配置:
| 选项 | Description |
|---|---|
|
| runtime (spring-boot、quarkus 或 camel-main) |
|
| Maven group:artifact:version |
|
| 其他依赖项(使用逗号分隔多个依赖项)。请参阅 添加自定义 JAR 的更多详细信息。 |
|
| 要添加到类路径的额外文件(使用逗号分隔多个文件)。请参阅 添加自定义 JAR 的更多详细信息。 |
|
| Java 版本(11 或 17) |
|
| Apache Camel Kamelets 版本 |
|
| 用于加载 Kamelets 的本地目录 |
|
| 用于 Spring Boot 的 Camel 版本 |
|
| Spring Boot 版本 |
|
| Quarkus Platform Maven groupId |
|
| Quarkus Platform Maven artifactId |
|
| Quarkus Platform 版本 |
|
| 在导出的项目中包括 Maven Wrapper 文件 |
|
| 在导出的项目中包括 Gradle Wrapper 文件 |
|
| 要使用的构建工具(maven 或 gradle) |
|
| 用于下载按需提供的其他 maven 存储库(使用逗号分隔多个存储库) |
|
| maven setting.xml 文件的可选位置,以配置服务器、存储库、镜像和代理。如果设置为 false,则不会使用默认的 ~/.m2/settings.xml。 |
|
| 用于解密 settings.xml 的 maven settings-security.xml 文件的可选位置 |
|
| 导出项目的目录。 |
|
| 运行独立 Camel 时使用的 HTTP 服务器端口,如启用 --console 时(默认为端口 8080)。 |
|
| 运行独立 Camel 时,位于本地 HTTP 服务器上的 /q/dev 的开发人员控制台(默认为端口 8080) |
|
| 运行独立 Camel 时,位于本地 HTTP 服务器上的 /q/health (端口 8080)上的健康检查。 |
这些是 export 命令的选项。您可以使用 camel export --help 查看更多详情和默认值。
5.8.4. 配置 复制链接链接已复制到粘贴板!
Camel CLI config 命令用于存储和使用用户配置。这消除了每次都指定 CLI 选项的需求。例如,要运行不同的 Camel 版本,请使用:
camel run * --camel-version=4.4.0.redhat_00014
camel run * --camel-version=4.4.0.redhat_00014
camel-version 可以添加到用户配置中,例如:
camel config set camel-version=4.4.0.redhat_00014
camel config set camel-version=4.4.0.redhat_00014
run 命令使用用户配置:
camel run *
camel run *
用户配置文件存储在 ~/.camel-jbang-user.properties 中。
5.8.4.1. 设置和取消设置配置 复制链接链接已复制到粘贴板!
每个 Camel CLI 选项都会添加到用户配置中。例如,要导出一个简单的项目,如
使用以下方法取消设置用户配置密钥:
camel config unset camel-spring-boot-version
camel config unset camel-spring-boot-version
5.8.4.2. 列出和获取配置 复制链接链接已复制到粘贴板!
使用以下方法列出用户配置密钥:
camel config list
camel config list
上述配置的输出如下:
runtime = spring-boot deps = org.apache.camel.springboot:camel-timer-starter gav = com.foo:acme:1.0-SNAPSHOT
runtime = spring-boot
deps = org.apache.camel.springboot:camel-timer-starter
gav = com.foo:acme:1.0-SNAPSHOT
要获取给定键的值,请使用 get 命令。
camel config get gav com.foo:acme:1.0-SNAPSHOT
camel config get gav
com.foo:acme:1.0-SNAPSHOT
5.8.4.3. 占位符替换 复制链接链接已复制到粘贴板!
用户配置值可以用作占位符替换命令行属性,例如:
camel config set repos=https://maven.repository.redhat.com/ga camel run 'Test.java' --logging-level=info --repos=#repos,https://packages.atlassian.com/maven-external
camel config set repos=https://maven.repository.redhat.com/ga
camel run 'Test.java' --logging-level=info --repos=#repos,https://packages.atlassian.com/maven-external
在本例中,因为仓库是在用户配置(config set)中设置的,camel run 命令声明占位符 #repos,camel run 将替换占位符,以便在执行过程中同时使用这两个存储库。请注意,要引用配置值,语法为 #optionName eg #repos。
占位符替换仅适用于给定 Camel 命令具有的每个选项。您可以使用 camel run --help 查看命令的所有选项。
5.8.5. 故障排除 复制链接链接已复制到粘贴板!
使用 JBang 时,它将状态存储在 ~/.jbang 目录中。这也是 JBang 存储下载 JAR 的位置。Camel CLI 也在运行时下载所需的依赖项。但是,这些依赖项会下载到您的本地 Maven 存储库 ~/.m2 中。因此,当您在运行 Camel CLI 时对过时的 JAR 等问题进行故障排除时,请尝试删除这些目录或其部分。