Skip to content

Commit ef4b80d

Browse files
Use persistent gradle home between invocations (#1473)
1 parent 9213ad6 commit ef4b80d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver/gradle/GradleResolver.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import com.github.bazelbuild.rules_jvm_external.resolver.netrc.Netrc;
3737
import com.google.common.graph.GraphBuilder;
3838
import com.google.common.graph.MutableGraph;
39+
import com.google.common.hash.Hashing;
3940
import com.google.devtools.build.runfiles.AutoBazelRepository;
4041
import com.google.devtools.build.runfiles.Runfiles;
4142
import java.io.IOException;
4243
import java.net.MalformedURLException;
4344
import java.net.URI;
45+
import java.nio.charset.StandardCharsets;
4446
import java.nio.file.Files;
4547
import java.nio.file.Path;
4648
import java.nio.file.Paths;
@@ -50,6 +52,7 @@
5052
import java.util.HashSet;
5153
import java.util.List;
5254
import java.util.Map;
55+
import java.util.Optional;
5356
import java.util.Set;
5457
import java.util.stream.Collectors;
5558

@@ -427,6 +430,27 @@ private Path getGradleInstallationPath() {
427430
}
428431
}
429432

433+
private Path getPersistentGradleHomeForRepo() {
434+
// We check for BUILD_WORKSPACE_DIRECTORY which will be set for most usages
435+
// with bazel run. It won't be available with tests, so we fall back to TEST_SRCDIR
436+
// which will map to the root of the runfiles tree
437+
String workspaceRoot =
438+
Optional.ofNullable(System.getenv("BUILD_WORKSPACE_DIRECTORY"))
439+
.orElse(System.getenv("TEST_SRCDIR"));
440+
441+
// If none are set, just return null so we fall back to the isolated cache
442+
if (workspaceRoot == null) {
443+
return null;
444+
}
445+
446+
// We want gradle home to be persistent but unique for each repo under which we're running
447+
// so we compute a MD5 hash, similiar to Bazel's output base and use that in the persistent
448+
// directory nameaz
449+
String md5 = Hashing.md5().hashString(workspaceRoot, StandardCharsets.UTF_8).toString();
450+
451+
return Paths.get(System.getProperty("java.io.tmpdir"), "rje-gradle-" + md5);
452+
}
453+
430454
private GradleProject setupFakeGradleProject(
431455
List<Repository> repositories,
432456
List<GradleDependency> dependencies,
@@ -466,6 +490,20 @@ private GradleProject setupFakeGradleProject(
466490
}
467491

468492
Path gradleCacheDir = fakeProjectDirectory.resolve(".gradle");
493+
// Get a persistent directory under temp dir specific to the repo directory under which
494+
// we're running so that we use a gradle home that's persistent between invocations
495+
// to help improve performance
496+
Path persistentGradleHome = getPersistentGradleHomeForRepo();
497+
if (persistentGradleHome != null) {
498+
gradleCacheDir = persistentGradleHome.resolve(".gradle");
499+
if (isVerbose()) {
500+
eventListener.onEvent(
501+
new LogEvent(
502+
"gradle",
503+
"Using persistent directory for gradle home",
504+
"Gradle Home: " + gradleCacheDir));
505+
}
506+
}
469507
Files.createDirectories(gradleCacheDir);
470508
if (useUnsafeCache) {
471509
// Instead of changing gradleCacheDir, symlink the user's caches directory

0 commit comments

Comments
 (0)