Skip to content

Commit de6c744

Browse files
committed
Create TempDir.tempDir()
Include tetjars and the pid in the temp dir name Closes gh-67
1 parent 0db62a1 commit de6c744

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/ResourceClasspathEntry.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,12 @@ boolean exists() {
5151
@Override
5252
public List<String> resolve() {
5353
if (this.classpath == null) {
54+
this.classpath = TempDir.tempDir();
55+
InputStream resource = getExistingResourceAsStream();
5456
try {
55-
this.classpath = Files.createTempDirectory("classpath-");
56-
InputStream resource = getExistingResourceAsStream();
57-
try {
58-
Path destination = this.classpath.resolve(this.classpathResourceName);
59-
destination.toFile().getParentFile().mkdirs();
60-
Files.copy(resource, destination, StandardCopyOption.REPLACE_EXISTING);
61-
}
62-
catch (IOException ex) {
63-
throw new RuntimeException("Failed to copy existingResourceName '" + this.existingResourceName
64-
+ "' to '" + this.classpathResourceName + "'", ex);
65-
}
57+
Path destination = this.classpath.resolve(this.classpathResourceName);
58+
destination.toFile().getParentFile().mkdirs();
59+
Files.copy(resource, destination, StandardCopyOption.REPLACE_EXISTING);
6660
}
6761
catch (IOException ex) {
6862
throw new RuntimeException("Failed to copy existingResourceName '" + this.existingResourceName

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/ScanningClasspathEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public List<String> resolve() {
7878
private Path createClasspath() {
7979
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
8080
try {
81-
Path classpath = Files.createTempDirectory("classpath-");
81+
Path classpath = TempDir.tempDir();
8282
Resource[] resources = resolver.getResources(this.resourcePattern);
8383
for (Resource resource : resources) {
8484
String path = this.renameResource.apply(getPath(resource));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.experimental.boot.server.exec;
18+
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
23+
/**
24+
* Utility to create a temp directory for testjars that includes the process id in the
25+
* name to make it easier to figure out what temp paths are on the classpath.
26+
*
27+
* @author Rob Winch
28+
*/
29+
final class TempDir {
30+
31+
/**
32+
* Creates a temp directory with the process id in the name.
33+
* @return a temp directory with the process id in the name.
34+
*/
35+
static Path tempDir() {
36+
long processId = ProcessHandle.current().pid();
37+
try {
38+
return Files.createTempDirectory("testjars-classpath-" + processId);
39+
}
40+
catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
45+
private TempDir() {
46+
}
47+
48+
}

0 commit comments

Comments
 (0)