Change output directory of maven surefire-plugin
Asked Answered
E

3

5

I'm trying to change the output folder for the XML files which get generated by the surefire-plugin in a maven project. I stated the target output folder inside the configuration brackets of the report-plugin as well as in the maven-site-plugin (mentioned in the documentation). I also tried to state the maven-site-plugin within the reporting block but that doesn't seem to work as well. My XML files always get written into the default surefire-reports folder.

My pom.xml has following entries:

<reporting>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-report-plugin</artifactId>
         <version>2.22.0</version>
         <configuration>
            <showSuccess>true</showSuccess>
            <outputDirectory>${basedir}/pb-reporting/test-output</outputDirectory>
            <!--<skipSurefireReport>true</skipSurefireReport>-->
         </configuration>
      </plugin>
   </plugins>
</reporting>

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.5.1</version>
         <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <!--<executable>/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.95-2.6.4.0.el7_2.x86_64/bin/javac</executable>-->
            <compilerArguments>
               <endorseddirs>${endorsed.dir}</endorseddirs>
            </compilerArguments>
         </configuration>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-site-plugin</artifactId>
         <version>3.7.1</version>
         <configuration>
            <outputDirectory>${basedir}/pb-reporting/test-output</outputDirectory>
         </configuration>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>2.22.0</version>
         <configuration>
            <testFailureIgnore>true</testFailureIgnore>
         </configuration>
      </plugin>
   </plugins>
</build>

Project hierarchy looks like this: Image of project hierarchy

Equally answered 30/7, 2018 at 12:50 Comment(0)
E
5

Configuring the output directory in the surefire-reports-plugin and maven-site-plugin isn't enough. The solution is to state the reportsDirectory also in maven-surefire-plugin within the build block:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.22.0</version>
   <configuration>
      <reportsDirectory>${basedir}/pb-reporting/test-output</reportsDirectory>
      <testFailureIgnore>true</testFailureIgnore>
   </configuration>
</plugin>
Equally answered 31/7, 2018 at 11:31 Comment(0)
E
2

According to the documentation you have to use the reportsDirectory instead of outputDirectory which does not exist.

Earthbound answered 30/7, 2018 at 13:19 Comment(2)
Doesn't work. The reports still get written into the default directoryEqually
I found out thqt it ignores the property if you write configurtion under an execution without specifying explictly phase /goal. The clue is your IDE resolving a placeholder within the propertyMicrocircuit
A
0

If you can hardcode your reports output directory in your pom.xml, this is absolutely enough:

<build>
   <plugins>
      <!-- The Maven Surefire Plugin is responsible for running JUnit tests and generating reports
      Configure the reports output directory option, so we can overwrite it in a CI/CD pipeline-->
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>3.1.0</version>
         <configuration>
            <reportsDirectory>your/dream/path</reportsDirectory>
         </configuration>
      </plugin>
   </plugins>
</build>

However, if you need to set your reports output directory dynamically, like with a bash command inside a CI/CD pipeline, you can do the following.

  1. Configure the maven-surefire-plugin's reportsDirectory option with a placeholder in your pom.xml file:
<build>
   <plugins>
      <!-- The Maven Surefire Plugin is responsible for running JUnit tests and generating reports
      Configure the reports output directory option, so we can overwrite it in a CI/CD pipeline-->
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>3.1.0</version>
         <configuration>
            <reportsDirectory>set-in-pipeline</reportsDirectory>
         </configuration>
      </plugin>
   </plugins>
</build>
  1. Before executing mvn test, replace the placeholder in your pom.xml file with the desired reports output directory. This example is for unix-like shells, e.g. bash.

    Here I replace <reportsDirectory>set-in-pipeline</reportsDirectory> with <reportsDirectory>${project.basedir}/desiredReportsOutputDirectory</reportsDirectory>.
sed -i -e 's=<reportsDirectory>set-in-pipeline</reportsDirectory>=<reportsDirectory>${project.basedir}/desiredReportsOutputDirectory</reportsDirectory>=g' pom.xml

I used = as delimiter for the sed command, as shown in this Stackoverflow question.

Notes:

  • this way you can change the reports output directory as often as you want, programmatically
  • other approaches that I tried didn't work, like
    • setting the reportsDirectory variable in my script via -DreportsDirectory => did not change it, but produced a warning saying this variable is read-only
    • following the documentation: editing the pom.xml and using mvn surefire-report:report -DoutputDirectory=newpath
Alexaalexander answered 13/10, 2023 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.