From 433329faad525eeabab185e241def5b3110a223f Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Mon, 9 Dec 2024 08:54:16 -0500 Subject: [PATCH] Clean up maven repo test kits (#1097) --- .../util/AppendableMavenFileModule.groovy | 13 +- .../util/AppendableMavenFileRepository.groovy | 3 +- .../shadow/util/repo/AbstractModule.groovy | 8 - .../repo/maven/AbstractMavenModule.groovy | 166 +++--------------- .../repo/maven/DefaultMavenMetaData.groovy | 39 ---- .../util/repo/maven/MavenDependency.groovy | 19 -- .../util/repo/maven/MavenFileModule.groovy | 40 ++--- .../repo/maven/MavenFileRepository.groovy | 3 +- .../util/repo/maven/MavenMetaData.groovy | 6 - .../shadow/util/repo/maven/MavenModule.groovy | 24 --- .../shadow/util/repo/maven/MavenPom.groovy | 44 ----- .../util/repo/maven/MavenRepository.groovy | 2 +- .../shadow/util/repo/maven/MavenScope.groovy | 29 --- 13 files changed, 38 insertions(+), 358 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/DefaultMavenMetaData.groovy delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenDependency.groovy delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenMetaData.groovy delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenPom.groovy delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenScope.groovy diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy index 689f84994..2e9022905 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy @@ -7,7 +7,7 @@ import org.apache.commons.io.IOUtils @InheritConstructors class AppendableMavenFileModule extends MavenFileModule { - Map> contents = [:].withDefault { [:] } + Map> contents = [:].withDefault { [:] } as Map> Map files = [:] AppendableMavenFileModule use(File file) { @@ -57,15 +57,4 @@ class AppendableMavenFileModule extends MavenFileModule { builder.build() } } - - /** - * Adds an additional artifact to this module. - * @param options Can specify any of: type or classifier - */ - @Override - AppendableMavenFileModule artifact(Map options) { - artifacts << options - return this - } - } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy index cc79dc0b6..18e4331da 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy @@ -7,9 +7,8 @@ import groovy.transform.InheritConstructors class AppendableMavenFileRepository extends MavenFileRepository { @Override - AppendableMavenFileModule module(String groupId, String artifactId, Object version = '1.0') { + AppendableMavenFileModule module(String groupId, String artifactId, String version = '1.0') { def artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") return new AppendableMavenFileModule(artifactDir, groupId, artifactId, version as String) } - } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.groovy index 7e5b983d0..8b99bf018 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.groovy @@ -46,18 +46,10 @@ abstract class AbstractModule { protected abstract onPublish(File file) - static File getSha1File(File file) { - getHashFile(file, "sha1") - } - static File sha1File(File file) { hashFile(file, "sha1", 40) } - static File getMd5File(File file) { - getHashFile(file, "md5") - } - static File md5File(File file) { hashFile(file, "md5", 32) } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy index e08a29ad9..aa3ec713b 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy @@ -28,27 +28,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule this.version = version } - @Override - MavenModule parent(String group, String artifactId, String version) { - parentPomSection = """ - - ${group} - ${artifactId} - ${version} - -""" - return this - } - - @Override - File getArtifactFile(Map options = [:]) { - if (version.endsWith("-SNAPSHOT") && !metaDataFile.exists() && uniqueSnapshots) { - def artifact = toArtifact(options) - return moduleDir.resolve("${artifactId}-${version}${artifact.classifier ? "-${artifact.classifier}" : ""}.${artifact.type}") - } - return artifactFile(options) - } - abstract boolean getUniqueSnapshots() String getPublishArtifactVersion() { @@ -61,7 +40,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule private String getUniqueSnapshotVersion() { assert uniqueSnapshots && version.endsWith('-SNAPSHOT') if (metaDataFile.isFile()) { - def metaData = new XmlParser().parse(metaDataFile.assertIsFile()) + def metaData = new XmlParser().parse(metaDataFile) def timestamp = metaData.versioning.snapshot.timestamp[0].text().trim() def build = metaData.versioning.snapshot.buildNumber[0].text().trim() return "${timestamp}-${build}" @@ -77,35 +56,11 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule } @Override - MavenModule dependsOn(String group, String artifactId, String version, String type = null) { + MavenModule dependsOn(String group, String artifactId, String version) { this.dependencies << [groupId: group, artifactId: artifactId, version: version, type: type] return this } - @Override - MavenModule hasPackaging(String packaging) { - this.packaging = packaging - return this - } - - /** - * Specifies the type of the main artifact. - */ - @Override - MavenModule hasType(String type) { - this.type = type - return this - } - - /** - * Adds an additional artifact to this module. - * @param options Can specify any of: type or classifier - */ - MavenModule artifact(Map options) { - artifacts << options - return this - } - String getPackaging() { return packaging } @@ -114,78 +69,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule return dependencies } - List getArtifacts() { - return artifacts - } - - void assertNotPublished() { - pomFile.assertDoesNotExist() - } - - void assertPublished() { - assert pomFile.assertExists() - assert parsedPom.groupId == groupId - assert parsedPom.artifactId == artifactId - assert parsedPom.version == version - } - - void assertPublishedAsPomModule() { - assertPublished() - assertArtifactsPublished("${artifactId}-${publishArtifactVersion}.pom") - assert parsedPom.packaging == "pom" - } - - void assertPublishedAsJavaModule() { - assertPublished() - assertArtifactsPublished("${artifactId}-${publishArtifactVersion}.jar", "${artifactId}-${publishArtifactVersion}.pom") - assert parsedPom.packaging == null - } - - void assertPublishedAsWebModule() { - assertPublished() - assertArtifactsPublished("${artifactId}-${publishArtifactVersion}.war", "${artifactId}-${publishArtifactVersion}.pom") - assert parsedPom.packaging == 'war' - } - - void assertPublishedAsEarModule() { - assertPublished() - assertArtifactsPublished("${artifactId}-${publishArtifactVersion}.ear", "${artifactId}-${publishArtifactVersion}.pom") - assert parsedPom.packaging == 'ear' - } - - /** - * Asserts that exactly the given artifacts have been deployed, along with their checksum files - */ - void assertArtifactsPublished(String... names) { - def artifactNames = names as Set - if (publishesMetaDataFile()) { - artifactNames.add(MAVEN_METADATA_FILE) - } - assert moduleDir.isDirectory() - Set actual = moduleDir.list() as Set - for (name in artifactNames) { - assert actual.remove(name) - - if (publishesHashFiles()) { - assert actual.remove("${name}.md5" as String) - assert actual.remove("${name}.sha1" as String) - } - } - assert actual.isEmpty() - } - - //abstract String getPublishArtifactVersion() - - @Override - MavenPom getParsedPom() { - return new MavenPom(pomFile) - } - - @Override - DefaultMavenMetaData getRootMetaData() { - new DefaultMavenMetaData(rootMetaDataFile) - } - @Override File getPomFile() { return moduleDir.resolve("$artifactId-${publishArtifactVersion}.pom") @@ -209,12 +92,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule return moduleDir.resolve(fileName) } - @Override - MavenModule publishWithChangedContent() { - publishCount++ - return publish() - } - protected Map toArtifact(Map options) { options = new HashMap(options) def artifact = [type: options.remove('type') ?: type, classifier: options.remove('classifier') ?: null] @@ -242,38 +119,37 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule publish(pomFile) { Writer writer -> def pomPackaging = packaging ?: type writer << """ - - - 4.0.0 - $groupId - $artifactId - $pomPackaging - $version - Published on $publishTimestamp""" + + + 4.0.0 + $groupId + $artifactId + $pomPackaging + $version + Published on $publishTimestamp + """.stripIndent() if (parentPomSection) { writer << "\n$parentPomSection\n" } if (!dependencies.empty) { - writer << """ - """ + writer << "" } dependencies.each { dependency -> def typeAttribute = dependency['type'] == null ? "" : "$dependency.type" writer << """ - - $dependency.groupId - $dependency.artifactId - $dependency.version - $typeAttribute - """ + + $dependency.groupId + $dependency.artifactId + $dependency.version + $typeAttribute + """.stripIndent() } if (!dependencies.empty) { - writer << """ - """ + writer << "" } writer << "\n" @@ -316,7 +192,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule publishPom() artifacts.each { artifact -> - publishArtifact(artifact) + publishArtifact(artifact as Map) } publishArtifact([:]) return this @@ -339,6 +215,4 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule } protected abstract boolean publishesMetaDataFile() - - protected abstract boolean publishesHashFiles() } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/DefaultMavenMetaData.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/DefaultMavenMetaData.groovy deleted file mode 100644 index bccdb40f1..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/DefaultMavenMetaData.groovy +++ /dev/null @@ -1,39 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -import groovy.xml.XmlParser - -/** - * http://maven.apache.org/ref/3.0.1/maven-repository-metadata/repository-metadata.html - */ -class DefaultMavenMetaData implements MavenMetaData { - - String text - - String groupId - String artifactId - String version - - String lastUpdated - - DefaultMavenMetaData(File file) { - text = file.text - def xml = new XmlParser().parseText(text) - - groupId = xml.groupId[0]?.text() - artifactId = xml.artifactId[0]?.text() - version = xml.version[0]?.text() - - def versioning = xml.versioning[0] - - lastUpdated = versioning.lastUpdated[0]?.text() - - versioning.versions[0].version.each { - versions << it.text() - } - } - - @Override - List getVersions() { - return [] - } -} diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenDependency.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenDependency.groovy deleted file mode 100644 index 56db25320..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenDependency.groovy +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -class MavenDependency { - String groupId - String artifactId - String version - String classifier - String type - - MavenDependency hasType(def type) { - assert this.type == type - return this - } - - @Override - String toString() { - return String.format("MavenDependency %s:%s:%s:%s@%s", groupId, artifactId, version, classifier, type) - } -} diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy index 71233befb..fad253beb 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - class MavenFileModule extends AbstractMavenModule { private boolean uniqueSnapshots = true @@ -13,29 +12,23 @@ class MavenFileModule extends AbstractMavenModule { return uniqueSnapshots } - @Override - MavenModule withNonUniqueSnapshots() { - uniqueSnapshots = false - return this - } - @Override String getMetaDataFileContent() { """ - - - $groupId - $artifactId - $version - - - ${timestampFormat.format(publishTimestamp)} - $publishCount - - ${updateFormat.format(publishTimestamp)} - - -""" + + + $groupId + $artifactId + $version + + + ${timestampFormat.format(publishTimestamp)} + $publishCount + + ${updateFormat.format(publishTimestamp)} + + + """.stripIndent() } @Override @@ -48,9 +41,4 @@ class MavenFileModule extends AbstractMavenModule { protected boolean publishesMetaDataFile() { uniqueSnapshots && version.endsWith("-SNAPSHOT") } - - @Override - protected boolean publishesHashFiles() { - true - } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy index 455588315..fdff3a8ed 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - /** * A fixture for dealing with file Maven repositories. */ @@ -17,7 +16,7 @@ class MavenFileRepository implements MavenRepository { } @Override - MavenFileModule module(String groupId, String artifactId, Object version = '1.0') { + MavenFileModule module(String groupId, String artifactId, String version = '1.0') { def artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") return new MavenFileModule(artifactDir, groupId, artifactId, version as String) } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenMetaData.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenMetaData.groovy deleted file mode 100644 index 0a3b49b11..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenMetaData.groovy +++ /dev/null @@ -1,6 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -interface MavenMetaData { - List getVersions(); - -} diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.groovy index fcd466d10..dd74383e3 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - interface MavenModule { /** * Publishes the pom.xml plus main artifact, plus any additional artifacts for this module. Publishes only those artifacts whose content has @@ -13,32 +12,9 @@ interface MavenModule { */ MavenModule publishPom() - /** - * Publishes the pom.xml plus main artifact, plus any additional artifacts for this module, with different content (and size) to any - * previous publication. - */ - MavenModule publishWithChangedContent() - - MavenModule withNonUniqueSnapshots() - - MavenModule parent(String group, String artifactId, String version) - MavenModule dependsOn(String group, String artifactId, String version) - MavenModule hasPackaging(String packaging) - - /** - * Sets the type of the main artifact for this module. - */ - MavenModule hasType(String type) - File getPomFile() - File getArtifactFile() - File getMetaDataFile() - - MavenPom getParsedPom() - - MavenMetaData getRootMetaData() } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenPom.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenPom.groovy deleted file mode 100644 index 9aa3d1ba4..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenPom.groovy +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -import groovy.xml.XmlParser - -class MavenPom { - String groupId - String artifactId - String version - String packaging - String description - final Map scopes = [:] - - MavenPom(File pomFile) { - if (pomFile.exists()) { - def pom = new XmlParser().parse(pomFile) - - groupId = pom.groupId[0]?.text() - artifactId = pom.artifactId[0]?.text() - version = pom.version[0]?.text() - packaging = pom.packaging[0]?.text() - description = pom.description[0]?.text() - - pom.dependencies.dependency.each { dep -> - def scopeElement = dep.scope - def scopeName = scopeElement ? scopeElement.text() : "runtime" - def scope = scopes[scopeName] - if (!scope) { - scope = new MavenScope() - scopes[scopeName] = scope - } - MavenDependency mavenDependency = new MavenDependency( - groupId: dep.groupId.text(), - artifactId: dep.artifactId.text(), - version: dep.version.text(), - classifier: dep.classifier ? dep.classifier.text() : null, - type: dep.type ? dep.type.text() : null - ) - def key = "${mavenDependency.groupId}:${mavenDependency.artifactId}:${mavenDependency.version}" - key += mavenDependency.classifier ? ":${mavenDependency.classifier}" : "" - scope.dependencies[key] = mavenDependency - } - } - } -} diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.groovy index 27b077cec..614b083bb 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.groovy @@ -8,5 +8,5 @@ interface MavenRepository { MavenModule module(String groupId, String artifactId) - MavenModule module(String groupId, String artifactId, Object version) + MavenModule module(String groupId, String artifactId, String version) } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenScope.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenScope.groovy deleted file mode 100644 index 8b456d148..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenScope.groovy +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -import org.apache.commons.lang3.StringUtils - -class MavenScope { - Map dependencies = [:] - - void assertDependsOn(String[] expected) { - assert dependencies.size() == expected.length - expected.each { - String key = StringUtils.substringBefore(it, "@") - def dependency = expectDependency(key) - - String type = null - if (it != key) { - type = StringUtils.substringAfter(it, "@") - } - assert dependency.hasType(type) - } - } - - MavenDependency expectDependency(String key) { - final dependency = dependencies[key] - if (dependency == null) { - throw new AssertionError("Could not find expected dependency $key. Actual: ${dependencies.values()}") - } - return dependency - } -}