Skip to content

Commit 0d8a0cd

Browse files
committed
Clean up junk from gradle's user home (~/.gradle/.tmp). #14385 (#14387)
1 parent 8914678 commit 0d8a0cd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

gradle/hacks/wipe-temp.gradle

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,35 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
import java.nio.file.Files
18+
import java.time.temporal.ChronoUnit
19+
import java.time.Instant
20+
1721

1822
// See LUCENE-9471. We redirect temporary location for gradle
1923
// so that it doesn't pollute user's tmp. Wipe it during a clean though.
2024

2125
configure(rootProject) {
2226
gradle.buildFinished {
27+
// we clean up the java.io.tmpdir we've redirected gradle to use (LUCENE-9471).
28+
// these are still used and populated with junk.
2329
rootProject.delete fileTree(".gradle/tmp").matching {
2430
include "gradle-worker-classpath*"
2531
}
32+
33+
// clean up any files older than 3 hours from the user's gradle temp. the time
34+
// limit is added so that we don't interfere with any concurrent builds... just in case.
35+
def gradleTmp = rootProject.gradle.gradleUserHomeDir.toPath().resolve(".tmp")
36+
if (Files.exists(gradleTmp)) {
37+
Instant deadline = Instant.now().minus(3, ChronoUnit.HOURS);
38+
try (var stream = Files.list(gradleTmp)) {
39+
stream.forEach(path -> {
40+
if (Files.getLastModifiedTime(path).toInstant().isBefore(deadline)) {
41+
Files.deleteIfExists(path);
42+
}
43+
});
44+
}
45+
}
2646
}
2747
}
2848

0 commit comments

Comments
 (0)