|
36 | 36 | import com.github.bazelbuild.rules_jvm_external.resolver.netrc.Netrc; |
37 | 37 | import com.google.common.graph.GraphBuilder; |
38 | 38 | import com.google.common.graph.MutableGraph; |
| 39 | +import com.google.common.hash.Hashing; |
39 | 40 | import com.google.devtools.build.runfiles.AutoBazelRepository; |
40 | 41 | import com.google.devtools.build.runfiles.Runfiles; |
41 | 42 | import java.io.IOException; |
42 | 43 | import java.net.MalformedURLException; |
43 | 44 | import java.net.URI; |
| 45 | +import java.nio.charset.StandardCharsets; |
44 | 46 | import java.nio.file.Files; |
45 | 47 | import java.nio.file.Path; |
46 | 48 | import java.nio.file.Paths; |
|
50 | 52 | import java.util.HashSet; |
51 | 53 | import java.util.List; |
52 | 54 | import java.util.Map; |
| 55 | +import java.util.Optional; |
53 | 56 | import java.util.Set; |
54 | 57 | import java.util.stream.Collectors; |
55 | 58 |
|
@@ -427,6 +430,27 @@ private Path getGradleInstallationPath() { |
427 | 430 | } |
428 | 431 | } |
429 | 432 |
|
| 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 | + |
430 | 454 | private GradleProject setupFakeGradleProject( |
431 | 455 | List<Repository> repositories, |
432 | 456 | List<GradleDependency> dependencies, |
@@ -466,6 +490,20 @@ private GradleProject setupFakeGradleProject( |
466 | 490 | } |
467 | 491 |
|
468 | 492 | 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 | + } |
469 | 507 | Files.createDirectories(gradleCacheDir); |
470 | 508 | if (useUnsafeCache) { |
471 | 509 | // Instead of changing gradleCacheDir, symlink the user's caches directory |
|
0 commit comments