What is a good way in maven/surefire to clean up a

2020-07-24 04:46发布

问题:

We have some unit tests that unfortunately create memory-mapped NIO files that cannot be deleted by the process that created them (some windows issue).

Regardless, I would like to run some sort of clean up after these tests, whether they passed or not. I was going to run a small ant script at the prepare-package phase, but if any test fails, surefire exits immediately.

Apart from going to the failsafe plugin which has a post-test phase, is there any clever way I can run my cleanup regardless of pass or fail?

I suspect not - I've gone through all the surefire config options...

edit: memory-mapped nio files cannot be deleted in the same process, even by deleteOnExit.

回答1:

You should bound the thing you want to do into the post-integration-test lifecycle phase which is running afterwards the integration-test phase which is handled by the maven-failsafe-plugin. It might work if you configure the maven-clean-plugin to do so.



回答2:

I would make sure the files are created in either target/ or the OS's temporary directory (using System.getProperty("java.io.tmpdir"). Then they will be cleaned up automatically when you run mvn clean, or eventually by the OS.



回答3:

Add following dependancy in pom and remove existing from your POM

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                </plugin>
            </plugins>
        </build>