Skip to content

Commit

Permalink
Clean up maven repo test kits (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler authored Dec 9, 2024
1 parent b016d84 commit 433329f
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.apache.commons.io.IOUtils
@InheritConstructors
class AppendableMavenFileModule extends MavenFileModule {

Map<String, Map<String, String>> contents = [:].withDefault { [:] }
Map<String, Map<String, String>> contents = [:].withDefault { [:] } as Map<String, Map<String, String>>
Map<String, File> files = [:]

AppendableMavenFileModule use(File file) {
Expand Down Expand Up @@ -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<String, ?> options) {
artifacts << options
return this
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
this.version = version
}

@Override
MavenModule parent(String group, String artifactId, String version) {
parentPomSection = """
<parent>
<groupId>${group}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</parent>
"""
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() {
Expand All @@ -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}"
Expand All @@ -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<String, ?> options) {
artifacts << options
return this
}

String getPackaging() {
return packaging
}
Expand All @@ -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")
Expand All @@ -209,12 +92,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
return moduleDir.resolve(fileName)
}

@Override
MavenModule publishWithChangedContent() {
publishCount++
return publish()
}

protected Map<String, Object> toArtifact(Map<String, ?> options) {
options = new HashMap<String, Object>(options)
def artifact = [type: options.remove('type') ?: type, classifier: options.remove('classifier') ?: null]
Expand Down Expand Up @@ -242,38 +119,37 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
publish(pomFile) { Writer writer ->
def pomPackaging = packaging ?: type
writer << """
<project xmlns="http://maven.apache.org/POM/4.0.0">
<!-- ${getArtifactContent()} -->
<modelVersion>4.0.0</modelVersion>
<groupId>$groupId</groupId>
<artifactId>$artifactId</artifactId>
<packaging>$pomPackaging</packaging>
<version>$version</version>
<description>Published on $publishTimestamp</description>"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
<!-- ${getArtifactContent()} -->
<modelVersion>4.0.0</modelVersion>
<groupId>$groupId</groupId>
<artifactId>$artifactId</artifactId>
<packaging>$pomPackaging</packaging>
<version>$version</version>
<description>Published on $publishTimestamp</description>
""".stripIndent()

if (parentPomSection) {
writer << "\n$parentPomSection\n"
}

if (!dependencies.empty) {
writer << """
<dependencies>"""
writer << "<dependencies>"
}

dependencies.each { dependency ->
def typeAttribute = dependency['type'] == null ? "" : "<type>$dependency.type</type>"
writer << """
<dependency>
<groupId>$dependency.groupId</groupId>
<artifactId>$dependency.artifactId</artifactId>
<version>$dependency.version</version>
$typeAttribute
</dependency>"""
<dependency>
<groupId>$dependency.groupId</groupId>
<artifactId>$dependency.artifactId</artifactId>
<version>$dependency.version</version>
$typeAttribute
</dependency>""".stripIndent()
}

if (!dependencies.empty) {
writer << """
</dependencies>"""
writer << "</dependencies>"
}

writer << "\n</project>"
Expand Down Expand Up @@ -316,7 +192,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule

publishPom()
artifacts.each { artifact ->
publishArtifact(artifact)
publishArtifact(artifact as Map<String, ?>)
}
publishArtifact([:])
return this
Expand All @@ -339,6 +215,4 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
}

protected abstract boolean publishesMetaDataFile()

protected abstract boolean publishesHashFiles()
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 433329f

Please sign in to comment.