@@ -11,12 +11,55 @@ param(
11
11
$StartTime = $ (get-date )
12
12
13
13
# Any new subdirectories to clean would be added here.
14
- $cacheSubdirsToClean = (" /com/azure" , " /com/microsoft/azure" )
14
+ $rootFolders = (" /com/azure" , " /com/microsoft/azure" )
15
15
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
+ }
20
63
}
21
64
22
65
Write-Host " POM files left in the cache folder"
0 commit comments