@@ -38,6 +38,7 @@ foreach ($rootFolder in $rootFolders) {
3838 # For each artifact root folder try to find maven-metadata-central.xml as that will determine which versions
3939 # of the artifact to retain in the DevOps cache.
4040 $mavenMetadataCentralPath = Join-Path - Path $artifactRootFolder - ChildPath " maven-metadata-central.xml"
41+ $mavenCentralVersions = {}
4142 if (Test-Path - Path $mavenMetadataCentralPath ) {
4243 # Folder contains a 'maven-metadata-central.xml' file, parse it to determine which subfolders should be deleted.
4344 #
@@ -46,18 +47,52 @@ foreach ($rootFolder in $rootFolders) {
4647 # folders which is constant with the previous design where the root folders were indiscriminately cleaned, best case
4748 # this only deletes built from source folders.
4849 $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- }
50+ $mavenCentralVersions = Select-Xml - Xml $metadataXml - XPath " /metadata/versioning/versions/version" | ForEach-Object {$_.Node.InnerXml }
5651 } else {
5752 # Folder doesn't contain a 'maven-metadata-central.xml' file, delete the entire folder as it cannot be determined
5853 # what was built from source vs resolved from Maven central.
5954 Write-Host " Deleting folder '$artifactRootFolder ' as it doesn't have a 'maven-metadata-central.xml' file."
6055 Remove-Item $artifactRootFolder - Recurse - ErrorAction Ignore
56+ continue
57+ }
58+
59+ $mavenMetadataLocalPath = Join-Path - Path $artifactRootFolder - ChildPath " maven-metadata-local.xml"
60+ $mavenLocalVersions = {}
61+ if (Test-Path - Path $mavenMetadataLocalPath ) {
62+ # Folder contains a 'maven-metadata-local.xml' file, parse it to determine which subfolders should be deleted.
63+ #
64+ # For example the metadata file lists azure-core 1.33.0 and 1.34.0-beta.1 and there are folders 1.33.0 and 1.34.0-beta.1
65+ # all folders will be deleted as Maven built those versions locally. Worst case, this over deletes folders which is constant
66+ # with the previous design where the root folders were indiscriminately cleaned, best case this only deletes built from source folders.
67+ $metadataXml = [XML ](Get-Content $mavenMetadataLocalPath )
68+ $mavenLocalVersions = Select-Xml - Xml $metadataXml - XPath " /metadata/versioning/versions/version" | ForEach-Object {$_.Node.InnerXml }
69+
70+ # Additionally, since we know the file exists, delete it. We don't want to cache information about what packages were built
71+ # locally as this could change build-to-build. For example, in one job 1.30.0 could be built locally while releasing, but
72+ # in the next job 1.31.0-beta.1 could be built locally as the version incremented. We don't want the file to then state both
73+ # 1.30.0 and 1.31.0-beta.1 were built locally.
74+ Write-Host " Deleting maven-metadata-local.xml '$mavenMetadataLocalPath '."
75+ }
76+
77+ # Now loop over each directory in this package. These directories should be the versions built locally or resolved from
78+ # Maven central.
79+ foreach ($versionFolder in (Get-ChildItem - Path $artifactRootFolder - Directory)) {
80+ # Both maven-metadata-central.xml and maven-metadata-local.xml are used with inverse checks as it's a possibility that
81+ # the project was built locally while Maven central also contains the package. This could happen when a PR has the prep
82+ # work for a release but doesn't pull in changes from main after the release completes.
83+ #
84+ # Ex, 1.30.0 is being released, so the PR uses 1.30.0 as the package version, but if the PR doesn't pull from main after
85+ # the version increments to 1.31.0-beta.1 the job will have 1.30.0 in both maven-metadata-central.xml and maven-metadata.local.xml.
86+ # It's safer to over delete what will be in the cache than to have incorrect data.
87+ if (! $mavenCentralVersions.Contains ($versionFolder.Name )) {
88+ # If maven-metadata-central.xml doesn't contain the version this is an explicit indicator the package was built locally.
89+ Write-Host " Deleting folder '$versionFolder ' as the version isn't in 'maven-metadata-central.xml'."
90+ Remove-Item $versionFolder - Recurse - ErrorAction Ignore
91+ } elseif ($mavenLocalVersions.Contains ($versionFolder.Name )) {
92+ # If maven-metadata-local.xml contains the version this is an explicit indicator the package was built locally.
93+ Write-Host " Deleting folder '$versionFolder ' as the version is in 'maven-metadata-local.xml'."
94+ Remove-Item $versionFolder - Recurse - ErrorAction Ignore
95+ }
6196 }
6297 }
6398}
0 commit comments