2.12.2. camel:validate

对于以下 Camel 功能的源代码验证:

  • 端点 URI
  • 简单表达式或 predicates
  • 重复的路由 ID

然后,您可以从命令行运行 camel:validate 目标,或者从 IDEA 或 Eclipse 等 Java 编辑器中运行。

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] ------------------------------------------------------------------------