1. 概述

本文介绍 Maven 构建工具的核心插件之一——verifier 插件。关于其他核心插件的概览,可参考这篇文章

2. 插件目标

verifier 插件只有一个目标:verify该目标用于验证文件或目录的存在性(或不存在性),并可选地通过正则表达式检查文件内容。

⚠️ 踩坑提醒:尽管名为 verify,但该目标默认绑定到 integration-test 阶段,而非 verify 阶段。

3. 配置

verifier 插件需显式添加到 pom.xml 才会触发:

<plugin>
    <artifactId>maven-verifier-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <verificationFile>input-resources/verifications.xml</verificationFile>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

此链接 可查看插件最新版本。

**验证文件的默认位置是 src/test/verifier/verifications.xml**。若需使用其他文件,必须设置 verificationFile 参数。

以下是配置中引用的验证文件内容:

<verifications 
  xmlns="http://maven.apache.org/verifications/1.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/verifications/1.0.0 
  http://maven.apache.org/xsd/verifications-1.0.0.xsd">
    <files>
        <file>
            <location>input-resources/baeldung.txt</location>
            <contains>Welcome</contains>
        </file>
    </files>
</verifications>

该验证文件确认: ✅ 文件 input-resources/baeldung.txt 存在 ✅ 文件内容包含单词 Welcome

(前提是我们已预先创建该文件,否则验证会失败)

4. 总结

本文介绍了 verifier 插件的核心功能及配置方法。完整示例代码可在GitHub获取。


原始标题:The Maven Verifier Plugin | Baeldung