使用后期构建脚本

下面是一个示例,说明如何使用 Invoker 插件运行一组 Maven 项目,然后使用 BeanShell 脚本验证它们的输出。在这种情况下,脚本文件的名称是verify.bsh

<project>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-invoker-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <debug>true</debug>
          <projectsDirectory>src/it</projectsDirectory>
          <pomIncludes>
            <pomInclude>**/pom.xml</pomInclude>
          </pomIncludes>
          <pomExcludes>
            <pomExclude>**/child*/pom.xml</pomExclude>
            <pomExclude>**/module*/pom.xml</pomExclude>
          </pomExcludes>
          <postBuildHookScript>verify.bsh</postBuildHookScript>
        </configuration>
        <executions>
          <execution>
            <id>integration-test</id>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
       </executions>
      </plugin>
    </plugins>
  </build>
</project>

这是一个示例构建后脚本 ( verify.bsh ),它在构建运行后检查 JAR 文件是否存在。如果 JAR 文件不存在,则脚本返回false,这会导致 Invoker Plugin 记录构建失败。java.io.File类型的全局变量basedir可用于引用当前集成测试的基目录。

import java.io.*;

try
{
    File file = new File( basedir, "target/my-test-project-1.0-SNAPSHOT.jar" );
    if ( !file.isFile() )
    {
        System.err.println( "Could not find generated JAR: " + file );
        return false;
    }
}
catch( Throwable t )
{
    t.printStackTrace();
    return false;
}

return true;