How to delete old subfolder from maven target folder on jenkins?

1.2k Views Asked by At

I use gatling in a separate maven project on Jenkins. Gatling creates reports in the target folder on jenkins. How can I delete the generated reports except the last 15?

I try to do it with the maven-invoker-plugin and a beanshell script. Is there a better way? The maven-invoker-plugin doesn't work. I have an error:

[INFO] --- maven-inviker-plugin:1.6:integration-test (integration-test) @ gatling-loadtests ---
[INFO] No project were selected for execution

Here is the pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>vf.gatling.loadtests</groupId>
    <artifactId>gatling-loadtests</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>gatling-loadtests</name>
    <description>Demo project for Gatling loadtests</description>


    <properties>
        <source.property>1.8</source.property>
        <target.property>1.8</target.property>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <gatling.version>2.2.3</gatling.version>
        <gatling-plugin.version>2.2.1</gatling-plugin.version>
        <scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>io.gatling.highcharts</groupId>
            <artifactId>gatling-charts-highcharts</artifactId>
            <version>${gatling.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>io.gatling</groupId>
                <artifactId>gatling-maven-plugin</artifactId>
                <version>${gatling-plugin.version}</version>
                <configuration>
                    <runMultipleSimulations>false</runMultipleSimulations>
                    <includes>
                        <param>${simulation}</param>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Plugin for Post-Build-Script -->
            <plugin>
                <artifactId>maven-invoker-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <debug>true</debug>
                    <projectsDirectory>test/beanshell</projectsDirectory>
                    <postBuildHookScript>postbuild_delete_old_reports.bsh</postBuildHookScript>
                </configuration>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

This is the project structure

./
+- pom.xml
+- src/
   +- test/
      +- beanshell/
      |  +- postbuild_delete_old_reports.bsh
      +- resources/
         ...            
      +- scala/
         ...
+- target/
   +- gatling/
      +- loadscenario-1479475762095/
         +- js/
         +- style/
         +- index.html
         ...
      +- loadscenario-1479475768887/
      +- loadscenario-1479477505729/
      +- loadscenario-1479479138171/
      +- loadscenario-1479479173198/
   +- genarated-test-sources/
   +- invoker-reports/
   +- maven-archiver/
   +- test-classes/
      ...
3

There are 3 best solutions below

0
On BEST ANSWER
1
On

You can simply use the delete workspace option of Jenkins, and then everytime you build the application it will delete the previous run's workspace.

0
On

I would have stored the results in a separate directory and used the after hook provided by Gatling to perform this activity. As a principal as well you should not store important things in target folder.