diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 676e9cdc6c..2c63c08d17 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -455,11 +455,12 @@ public abstract class AbstractSurefireMojo extends AbstractMojo implements Suref private boolean failIfNoTests; /** - * Relative path to temporary-surefire-boot directory containing internal Surefire temporary files. + * Path to temporary-surefire-boot directory containing internal Surefire temporary files. *
* The temporary-surefire-boot directory is project.build.directory on most platforms or * system default temporary-directory specified by the system property {@code java.io.tmpdir} * on Windows (see SUREFIRE-1400). + * Absolute paths and relative paths with directory separators are used as explicit directories on all platforms. *
* It is deleted after the test set has completed. * @@ -3361,13 +3362,28 @@ File createSurefireBootDirectoryInBuild() { } File createSurefireBootDirectoryInTemp() { + String tempDir = getTempDir(); + if (isTempDirPath(tempDir)) { + File tmp = new File(tempDir); + if (!tmp.isAbsolute()) { + tmp = new File(getProjectBuildDirectory(), tempDir); + } + //noinspection ResultOfMethodCallIgnored + tmp.mkdirs(); + return tmp; + } + try { - return Files.createTempDirectory(getTempDir()).toFile(); - } catch (IOException e) { + return Files.createTempDirectory(tempDir).toFile(); + } catch (IOException | IllegalArgumentException e) { return createSurefireBootDirectoryInBuild(); } } + private static boolean isTempDirPath(String tempDir) { + return new File(tempDir).isAbsolute() || tempDir.indexOf('/') >= 0 || tempDir.indexOf('\\') >= 0; + } + @Override public String getLocalRepositoryPath() { return Optional.ofNullable( diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java index 9260a137e6..1714b51420 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java @@ -816,6 +816,21 @@ public void shouldExistTmpDirectory() throws IOException { .isDirectory(); } + @Test + public void shouldUseAbsoluteTmpDirectory() throws IOException { + File targetDir = Files.createTempDirectory(tempFolder, "target").toFile(); + File absoluteTempDir = tempFolder.resolve("surefire-tmp").toFile(); + + Mojo mojo = new Mojo(); + mojo.setProjectBuildDirectory(targetDir); + mojo.setTempDir(absoluteTempDir.getAbsolutePath()); + + File bootDir = mojo.createSurefireBootDirectoryInTemp(); + + assertThat(bootDir).isDirectory(); + assertThat(bootDir.getCanonicalFile()).isEqualTo(absoluteTempDir.getCanonicalFile()); + } + @Test public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception { MavenProject mavenProject = new MavenProject();