2.12.2. camel:validate
对于以下 Camel 功能的源代码验证:
- 端点 URI
- 简单表达式或 predicates
- 重复路由 ID
然后,您可以从命令行运行 camel:validate
目标,或者从 Java 编辑器(如 IDEA 或 Eclipse)中运行。
mvn camel:validate
您还可以启用插件以在构建过程中自动运行,以捕获这些错误。
<plugin> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>camel-maven-plugin</artifactId> <executions> <execution> <phase>process-classes</phase> <goals> <goal>validate</goal> </goals> </execution> </executions> </plugin>
该阶段决定插件何时运行。在以上阶段的示例中,进程类
是在主源代码编译后运行的。maven 插件也可以配置为验证测试源代码,这意味着应相应地将阶段改为 process-test-classes
,如下所示:
<plugin> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>camel-maven-plugin</artifactId> <executions> <execution> <configuration> <includeTest>true</includeTest> </configuration> <phase>process-test-classes</phase> <goals> <goal>validate</goal> </goals> </execution> </executions> </plugin>
2.12.2.1. 在任何 Maven 项目上运行目标
您还可以在任何 Maven 项目上运行验证目标,而无需将插件添加到 pom.xml
文件中。这样做需要通过其完全限定名称指定插件。例如,若要在 Apache Camel 的 camel-example-cdi
上运行目标,您可以运行
$cd camel-example-cdi $mvn org.apache.camel:camel-maven-plugin:2.20.0:validate
然后,运行并输出以下内容:
[INFO] ------------------------------------------------------------------------ [INFO] Building Camel :: Example :: CDI 2.20.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- camel-maven-plugin:2.20.0:validate (default-cli) @ camel-example-cdi --- [INFO] Endpoint validation success: (4 = passed, 0 = invalid, 0 = incapable, 0 = unknown components) [INFO] Simple validation success: (0 = passed, 0 = invalid) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
验证通过的验证,4 个端点已被验证。现在假定我们在源代码中的一个 Camel 端点 URI 中出现了拼写错误,例如:
@Uri("timer:foo?period=5000")
被修改为在 period
选项中包含拼写错误
@Uri("timer:foo?perid=5000")
在运行验证目标时,再次报告以下内容:
[INFO] ------------------------------------------------------------------------ [INFO] Building Camel :: Example :: CDI 2.20.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- camel-maven-plugin:2.20.0:validate (default-cli) @ camel-example-cdi --- [WARNING] Endpoint validation error at: org.apache.camel.example.cdi.MyRoutes(MyRoutes.java:32) timer:foo?perid=5000 perid Unknown option. Did you mean: [period] [WARNING] Endpoint validation error: (3 = passed, 1 = invalid, 0 = incapable, 0 = unknown components) [INFO] Simple validation success: (0 = passed, 0 = invalid) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------