骆驼报告 maven 插件
骆驼报告 Maven 插件支持以下目标
-
camel-report:validate - 验证源代码中是否存在无效的 Camel 端点 uris -
camel-report:route-coverage - 在单元测试后报告 Camel 路由的覆盖率
骆驼报告:验证
用于验证错误配置的 Camel 源代码:
-
终端 uris
-
简单表达式或谓词 -
路由标识重复
-
seda/direct 端点对 -
属性文件中的配置
然后,您可以通过命令行或 IDEA 或 Eclipse 等 Java 编辑器运行 validate
目标。
mvn camel-report:validate
您也可以在构建过程中启用插件自动运行,以捕捉这些错误。
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-report-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
阶段决定插件的运行时间。在上面的示例中, process-classes
阶段在主源代码编译后运行。
还可以将 maven 插件配置为验证测试源代码,这意味着如下所示,阶段应相应改为 process-test-classes
:
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-report-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<includeTest>true</includeTest>
</configuration>
<phase>process-test-classes</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
在任何 maven 项目上运行目标
您也可以在任何 Maven 项目上运行验证目标,而无需在 pom.xml
文件中添加插件。这样做需要使用插件的完整限定名称来指定插件。例如,要在 Apache Camel 的 camel-example-cdi
上运行目标,可以运行
$cd camel-example-cdi $mvn org.apache.camel:camel-report-maven-plugin:2.20.0:validate
然后运行并输出以下内容:
[INFO] ------------------------------------------------------------------------ [INFO] Building Camel :: Example :: CDI 3.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- camel-report-maven-plugin:3.0.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 端点 uris 中打错了字,例如
@Uri("timer:foo?period=5000")
选项中的一个错字。
@Uri("timer:foo?perid=5000")
再次运行 validate
目标时,报告如下:
[INFO] ------------------------------------------------------------------------ [INFO] Building Camel :: Example :: CDI 3.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- camel-report-maven-plugin:3.0.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] ------------------------------------------------------------------------
选项
maven 插件验证目标支持以下选项,这些选项可通过命令行配置(使用 -D
语法),或在 pom.xml
文件中的 <configuration>
标记中定义。
参数 | 默认值 | 说明 |
下载版本 | 真 |
|
failOnError | 错误 |
|
覆盖阈值 | 100 |
|
总体覆盖阈值 | 0 |
|
logUnparseable | 错误 |
|
includeJava | 真 |
|
includeXml | 真 |
|
包含测试 | 错误 |
|
包括 |
| |
不包括 |
| |
忽略未知组件 | 真 |
|
忽略无能 | 真 |
|
忽略简化属性 | 真 |
|
忽略旧版 | 真 |
|
重复路由标识 | 真 |
|
直接或间接配对检查 | 真 |
|
配置文件 | 应用程序属性 |
|
显示全部 | 错误 |
|
| 错误 |
|
来源文物 |
| |
extraMavenRepositories |
|
例如,要从命令行打开忽略使用已废弃选项的功能,可以运行
$mvn camel-report:validate -Dcamel.ignoreDeprecated=true
请注意,必须在 -D
命令参数前加上 camel.
。,例如 camel.ignoreDeprecated
作为选项名称。
验证包括测试
如果您有一个 Maven 项目,那么也可以运行该插件来验证单元测试源代码中的端点。如图所示,您可以使用 -D
样式传递选项:
$cd myproject $mvn org.apache.camel:camel-report-maven-plugin:3.0.0:validate -Dcamel.includeTest=true
在传递依赖关系中验证 apache camel 路由
如果您的路由使用了当前项目中不存在的 direct
或 seda
端点,但这些路由被声明为项目的依赖关系,那么您可以相应地编辑插件配置,以便 Camel 验证插件可以考虑这些路由。特别是,为了使用具有传递依赖关系的验证插件,需要使用源 jars,例如
-
给定当前项目中定义的以下 Camel 路由from("direct:in").to("direct:out")
-
路由from("direct:out")
被声明为项目的依赖关系,例如my.company:routes-dependency:1.0
-
如果将routes-dependency
源代码发布到 maven 代码库中,可使用以下插件配置:
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-report-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<sourcesArtifacts>
<sourcesArtifact>my.company:routes-dependency:jar:sources:1.0</sourcesArtifact>
</sourcesArtifacts>
<extraMavenRepositories>
<extraMavenRepository>http://internal.repo:8080/maven</extraMavenRepository>
</extraMavenRepositories>
</configuration>
</execution>
</executions>
</plugin>
camel-report:route-coverage
用于从单元测试中生成 Camel 路由覆盖率报告。与生成 Java 代码覆盖率报告的方式相同,该报告也适用于 Camel 路由。因此,您可以用它来了解 Camel 路由的哪些部分已被使用或未被使用。
Camel 使用 JMX 捕获测试期间的指标,用于计算路由覆盖率。您的类路径中必须有 camel-management
JAR(可以是测试作用域),例如在 pom.xml 文件中声明以下 Maven 依赖关系:
<!-- JMX is needed for route coverage testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<scope>test</scope>
</dependency>
实现航线覆盖
在运行单元测试时,您可以通过以下任一方式启用路由覆盖:
-
设置启用所有测试类的全局 JVM 系统属性 -
如果使用camel-test-spring-junit5
模块,则每个测试类使用@EnableRouteCoverage
注释 -
如果使用camel-test
模块,每个测试类重载isDumpRouteCoverage
方法
通过 jvm 系统属性启用
您可以打开 JVM 系统属性 CamelTestRouteCoverage
以启用所有测试用例的路由覆盖。这可以在 maven-surefire-plugin
.JVM 系统属性 {{2}} 的配置中完成:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<CamelTestRouteCoverage>true</CamelTestRouteCoverage>
</systemPropertyVariables>
</configuration>
</plugin>
或在运行测试时从命令行输入:
mvn clean test -DCamelTestRouteCoverage=true
通过 @enableroutecoverage 注释启用
您需要在单元测试类中启用路由覆盖。如果使用 camel-test-spring-junit5
进行测试,则可以在测试类中添加 @EnableRouteCoverage
注解:
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = SampleCamelApplication.class)
@EnableRouteCoverage
public class FooApplicationTest {
通过 isdumproutecoverage 方法启用
但是,如果您使用的是 camel-test
,而单元测试扩展了 CamelTestSupport
,则可以如图所示打开路由覆盖:
@Override
public boolean isDumpRouteCoverage() {
return true;
}
可进行路由覆盖的路由必须有一个唯一 ID,换句话说,不能使用匿名路由。
您可以使用 Java DSL 中的 routeId
来实现这一功能:
from("jms:queue:cheese").routeId("cheesy")
.to("log:foo")
...
在 XML DSL 中,您只需通过 id 属性指定路由 id
<route id="cheesy">
<from uri="jms:queue:cheese"/>
<to uri="log:foo"/>
...
</route>
生成航线覆盖报告
在使用
mvn test
然后您就可以运行目标,报告路线覆盖情况
mvn camel-report:route-coverage
然后,通过精确的源代码行报告功能,报告哪些路由缺少路由覆盖:
[INFO] --- camel-camel-report-plugin:3.0.0:route-coverage (default-cli) @ camel-example-spring-boot-xml --- [INFO] Discovered 1 routes [INFO] Route coverage summary: File: src/main/resources/my-camel.xml RouteId: hello Line # Count Route ------ ----- ----- 28 1 from 29 1 transform 32 1 filter 34 0 to 36 1 to Coverage: 4 out of 5 (80.0%)
在这里,我们可以看到倒数第 2 行的 to
的计数列中有 0
,因此不包括在内。我们还可以看到,这是源代码文件中的第 34 行,位于 my-camel.xml
XML 文件中的第 34 行。
选项
maven 插件覆盖目标支持以下选项,这些选项可通过命令行配置(使用 -D
语法),或在 pom.xml
文件的 <configuration>
标记中定义。
参数 | 默认值 | 说明 |
failOnError | 错误 |
|
包含测试 | 错误 |
|
包括 |
| |
不包括 |
| |
匿名路线 | 错误 |
|
生成 JacocoXmlReport | 错误 |
|
生成 HtmlReport | 错误 |
|