魅力:目标文件夹中的环境文件在 maven clean 上被删除。我如何在每次构建时生成它?

Allure: Environment file in target folder gets deleted on maven clean. How do I generate it on every build?

说明说要将 environment.xml 添加到 Allure 结果目录 (https://github.com/allure-framework/allure-core/wiki/Environment),但是这个文件夹在 mvn clean 上被删除了,所以文件也随之被删除。有没有办法在每次构建时生成这个文件?

谢谢。

只需输入您的 src/main/resources/ 并通过 mvn test 上的 maven resources pluginmvn site:

复制到您的结果目录
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-allure-environment</id>
            <phase>pre-site</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/allure-results</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>environment.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

对我来说,阶段 "pre-site" 没有用 正确的阶段是 validate 我的资源在 src\test\java\resoruces 这是我的 pom.xml 文件

中的有效答案
<plugin>
   <artifactId>maven-resources-plugin</artifactId>
   <version>3.1.0</version>
   <executions>
      <execution>
         <id>copy-resources</id>
         <phase>validate</phase>
         <goals>
            <goal>copy-resources</goal>
         </goals>
         <configuration>
            <outputDirectory>${basedir}/allure-results</outputDirectory>
            <resources>
               <resource>
                  <directory>src/test/resources</directory>
                  <includes>
                     <include>environment.xml</include>
                  </includes>
               </resource>
            </resources>
         </configuration>
      </execution>
   </executions>
</plugin> 

披露:我创建了 Java 库来处理这个问题:https://github.com/AutomatedOwl/allure-environment-writer

它使用 TransformerFactory 在测试的任何阶段将 environment.xml 写入 allure-results 路径。它还会检查目录是否存在,以防 运行 来自已清理的构建。

用法示例:

import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;

public class SomeTests {

    @BeforeSuite
    void setAllureEnvironment() {
        allureEnvironmentWriter(
                ImmutableMap.<String, String>builder()
                        .put("Browser", "Chrome")
                        .put("Browser.Version", "70.0.3538.77")
                        .put("URL", "http://testjs.site88.net")
                        .build(), System.getProperty("user.dir")
                        + "/allure-results/");
    }

    @Test
    void someTest() {
        Assert.assertTrue(true);
    }
}