Skip to content

Commit 29b798b

Browse files
authored
Change How Maven Cache is Cleaned (Azure#32520)
Change How Maven Cache is Cleaned
1 parent 26eaa0c commit 29b798b

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

eng/pipelines/templates/steps/generate-project-list-and-cache-maven-repository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ steps:
4949

5050
- task: Cache@2
5151
inputs:
52-
# Note: CacheSalt is only intented to be used in special cases, as a build is queued, to force a cache miss.
52+
# Note: CacheSalt is only intended to be used in special cases, as a build is queued, to force a cache miss.
5353
# This would be set as a variable in the UI when the run is queued.
5454
key: 'maven | "$(CacheSalt)" | "$(Agent.OS)" | $(Build.SourcesDirectory)/eng/versioning/external_dependencies.txt | "$(ProjectListSha256)" | "${{ parameters.JobType }}" | "$(TestFromSource)"'
5555
path: $(MAVEN_CACHE_FOLDER)

eng/scripts/Remove-Azure-Artifacts-From-Cache.ps1

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,55 @@ param(
1111
$StartTime = $(get-date)
1212

1313
# Any new subdirectories to clean would be added here.
14-
$cacheSubdirsToClean = ("/com/azure", "/com/microsoft/azure")
14+
$rootFolders = ("/com/azure", "/com/microsoft/azure")
1515

16-
foreach ($cacheSubDir in $cacheSubdirsToClean) {
17-
$fullPathToClean = Join-Path -Path $MavenCacheFolder -ChildPath $cacheSubDir
18-
Write-Host "Cleaning $fullPathToClean"
19-
Remove-Item $fullPathToClean -Recurse -ErrorAction Ignore
16+
foreach ($rootFolder in $rootFolders) {
17+
# Determine the starting search path by joining the Maven cache folder with the specific Azure SDKs subpath.
18+
$searchPath = Join-Path -Path $MavenCacheFolder -ChildPath $rootFolder
19+
20+
if (!(Test-Path -Path $searchPath)) {
21+
Write-Host "Skipping '$searchPath' as it doesn't exist."
22+
continue
23+
}
24+
25+
# Find all directories that contain a "maven-metadata*" file. Maven metadata files are the source of truth for
26+
# what was built locally vs downloaded from Maven central. There are three types of Maven metadata files in
27+
# the repository:
28+
#
29+
# maven-metadata-azure-sdk-for-java.xml for dependencies from the Azure SDK for Java DevOps Artifacts.
30+
# maven-metadata-central.xml for dependencies from Maven central.
31+
# maven-metadata-local.xml for dependencies built locally.
32+
#
33+
# Select all folders that contain any of the three types of Maven metadata files, which ones the folder contains
34+
# will be inspected later.
35+
$artifactRootFolders = (Get-ChildItem -Path $searchPath -Recurse -Directory) | Where-Object { (Get-ChildItem -Path $_.FullName -Filter "maven-metadata*" | Measure-Object).Count -gt 0 }
36+
37+
foreach ($artifactRootFolder in $artifactRootFolders) {
38+
# For each artifact root folder try to find maven-metadata-central.xml as that will determine which versions
39+
# of the artifact to retain in the DevOps cache.
40+
$mavenMetadataCentralPath = Join-Path -Path $artifactRootFolder -ChildPath "maven-metadata-central.xml"
41+
if (Test-Path -Path $mavenMetadataCentralPath) {
42+
# Folder contains a 'maven-metadata-central.xml' file, parse it to determine which subfolders should be deleted.
43+
#
44+
# For example the metadata file lists azure-core 1.30.0, 1.31.0, and 1.32.0 and there are folders 1.33.0 and 1.34.0-beta.1
45+
# all folders will be deleted as Maven central doesn't know about those versions. Worst case, this over deletes
46+
# folders which is constant with the previous design where the root folders were indiscriminately cleaned, best case
47+
# this only deletes built from source folders.
48+
$metadataXml = [XML](Get-Content $mavenMetadataCentralPath)
49+
$versions = Select-Xml -Xml $metadataXml -XPath "/metadata/versioning/versions/version" | ForEach-Object {$_.Node.InnerXml}
50+
foreach ($versionFolder in (Get-ChildItem -Path $artifactRootFolder -Directory)) {
51+
if (!$versions.Contains($versionFolder.Name)) {
52+
Write-Host "Deleting folder '$versionFolder' as the version isn't in 'maven-metadata-central.xml'."
53+
Remove-Item $versionFolder -Recurse -ErrorAction Ignore
54+
}
55+
}
56+
} else {
57+
# Folder doesn't contain a 'maven-metadata-central.xml' file, delete the entire folder as it cannot be determined
58+
# what was built from source vs resolved from Maven central.
59+
Write-Host "Deleting folder '$artifactRootFolder' as it doesn't have a 'maven-metadata-central.xml' file."
60+
Remove-Item $artifactRootFolder -Recurse -ErrorAction Ignore
61+
}
62+
}
2063
}
2164

2265
Write-Host "POM files left in the cache folder"

0 commit comments

Comments
 (0)