diff --git a/build/deps/src/org/jetbrains/intellij/build/impl/BundledMavenDownloader.kt b/build/deps/src/org/jetbrains/intellij/build/impl/BundledMavenDownloader.kt index 7d5c14bc1ac0..ac3e92a4b7a6 100644 --- a/build/deps/src/org/jetbrains/intellij/build/impl/BundledMavenDownloader.kt +++ b/build/deps/src/org/jetbrains/intellij/build/impl/BundledMavenDownloader.kt @@ -2,8 +2,11 @@ package org.jetbrains.intellij.build.impl import org.jetbrains.intellij.build.dependencies.* +import java.math.BigInteger import java.nio.file.Files import java.nio.file.Path +import java.nio.file.StandardCopyOption +import java.security.MessageDigest object BundledMavenDownloader { private val mavenCommonLibs: List = listOf( @@ -26,24 +29,60 @@ object BundledMavenDownloader { println("Maven common libs at $commonLibs") } + private fun fileChecksum(path: Path): String? { + return try { + val md5 = MessageDigest.getInstance("MD5") + md5.update(Files.readAllBytes(path)) + val digest = md5.digest() + BigInteger(1, digest).toString(32) + } + catch (e: Exception) { + null + } + } + fun downloadMavenCommonLibs(communityRoot: BuildDependenciesCommunityRoot): Path { val root = communityRoot.communityRoot.resolve("plugins/maven/maven3-server-common/lib") - BuildDependenciesUtil.cleanDirectory(root) + Files.createDirectories(root) + val targetToSourceFiles = HashMap() for (coordinates in mavenCommonLibs) { val split = coordinates.split(':') check(split.size == 3) { "Expected exactly 3 coordinates: $coordinates" } - + val targetFile = root.resolve("${split[1]}-${split[2]}.jar") val uri = BuildDependenciesDownloader.getUriForMavenArtifact( BuildDependenciesConstants.MAVEN_CENTRAL_URL, split[0], split[1], split[2], "jar" ) - - val file = BuildDependenciesDownloader.downloadFileToCacheLocation(communityRoot, uri) - Files.copy(file, root.resolve("${split[1]}-${split[2]}.jar")) + val sourceFile = BuildDependenciesDownloader.downloadFileToCacheLocation(communityRoot, uri) + targetToSourceFiles[targetFile] = sourceFile } + + Files.list(root).use { stream -> + stream.forEach { file: Path? -> + run { + if (!targetToSourceFiles.containsKey(file)) { + BuildDependenciesUtil.deleteFileOrFolder(file) + } + } + } + } + + for (targetFile in targetToSourceFiles.keys) { + val sourceFile = targetToSourceFiles[targetFile]!! + if (!Files.exists(targetFile)) { + Files.copy(sourceFile, targetFile) + } else { + val sourceCheckSum = fileChecksum(sourceFile) + val targetCheckSum = fileChecksum(targetFile) + if (!sourceCheckSum.equals(targetCheckSum)) { + Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING) + } + } + } + return root } diff --git a/platform/build-scripts/downloader/src/org/jetbrains/intellij/build/dependencies/BuildDependenciesUtil.java b/platform/build-scripts/downloader/src/org/jetbrains/intellij/build/dependencies/BuildDependenciesUtil.java index 13eae463cea4..521f1c4dc349 100644 --- a/platform/build-scripts/downloader/src/org/jetbrains/intellij/build/dependencies/BuildDependenciesUtil.java +++ b/platform/build-scripts/downloader/src/org/jetbrains/intellij/build/dependencies/BuildDependenciesUtil.java @@ -440,17 +440,19 @@ public final class BuildDependenciesUtil { return s.substring(start, end); } + public static void deleteFileOrFolder(Path file) { + try { + MoreFiles.deleteRecursively(file, RecursiveDeleteOption.ALLOW_INSECURE); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + public static void cleanDirectory(Path directory) throws IOException { Files.createDirectories(directory); try (Stream stream = Files.list(directory)) { - stream.forEach(path -> { - try { - MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE); - } - catch (IOException e) { - throw new RuntimeException(e); - } - }); + stream.forEach(BuildDependenciesUtil::deleteFileOrFolder); } }