From 5239d0703a2747fd1db49f54c99aa3f9af441558 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 10 Dec 2024 13:12:48 +0800 Subject: [PATCH 01/28] Migrate AbstractMavenModule to using Maven model APIs --- build.gradle.kts | 2 + gradle/libs.versions.toml | 2 + .../repo/maven/AbstractMavenModule.groovy | 193 ++++++++++-------- .../util/repo/maven/MavenFileModule.groovy | 8 +- 4 files changed, 112 insertions(+), 93 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 42fbbde71..c3a0f66ad 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -87,6 +87,8 @@ dependencies { } funcTestImplementation(sourceSets.main.get().output) funcTestImplementation(intiTest.output) + funcTestImplementation(libs.apache.maven.modelBuilder) + funcTestImplementation(libs.apache.maven.repositoryMetadata) lintChecks(libs.androidx.gradlePluginLints) lintChecks(libs.assertk.lint) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7f27a236e..855f3ead6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,6 +3,8 @@ apache-ant = "org.apache.ant:ant:1.10.15" apache-commonsIo = "commons-io:commons-io:2.18.0" apache-commonsLang = "org.apache.commons:commons-lang3:3.17.0" apache-log4j = "org.apache.logging.log4j:log4j-core:2.24.3" +apache-maven-modelBuilder = "org.apache.maven:maven-model-builder:3.9.9" +apache-maven-repositoryMetadata = "org.apache.maven:maven-repository-metadata:3.9.9" asm = "org.ow2.asm:asm-commons:9.7.1" jdependency = "org.vafer:jdependency:2.11" jdom2 = "org.jdom:jdom2:2.0.6.1" 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 90dddd2f6..ebbd25440 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 @@ -1,25 +1,32 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven import com.github.jengelman.gradle.plugins.shadow.util.repo.AbstractModule -import groovy.xml.MarkupBuilder -import groovy.xml.XmlParser +import org.apache.maven.artifact.repository.metadata.Metadata +import org.apache.maven.artifact.repository.metadata.Snapshot +import org.apache.maven.artifact.repository.metadata.Versioning +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer +import org.apache.maven.model.Dependency +import org.apache.maven.model.Model +import org.apache.maven.model.io.xpp3.MavenXpp3Writer import java.text.SimpleDateFormat abstract class AbstractMavenModule extends AbstractModule implements MavenModule { protected static final String MAVEN_METADATA_FILE = "maven-metadata.xml" - final File moduleDir - final String groupId - final String artifactId - final String version - String parentPomSection - String type = 'jar' - String packaging - int publishCount = 1 - private final List dependencies = [] - private final List artifacts = [] - final updateFormat = new SimpleDateFormat("yyyyMMddHHmmss") - final timestampFormat = new SimpleDateFormat("yyyyMMdd.HHmmss") + + protected final File moduleDir + protected final String groupId + protected final String artifactId + protected final String version + protected final def updateFormat = new SimpleDateFormat("yyyyMMddHHmmss") + protected final def timestampFormat = new SimpleDateFormat("yyyyMMdd.HHmmss") + protected final List> dependencies = [] + protected final List> artifacts = [] + + protected String type = 'jar' + protected String packaging + protected int publishCount = 1 AbstractMavenModule(File moduleDir, String groupId, String artifactId, String version) { this.moduleDir = moduleDir @@ -28,7 +35,9 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule this.version = version } - abstract boolean getUniqueSnapshots() + protected abstract boolean isUniqueSnapshots() + + protected abstract boolean isPublishesMetaDataFile() String getPublishArtifactVersion() { if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { @@ -40,9 +49,9 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule private String getUniqueSnapshotVersion() { assert uniqueSnapshots && version.endsWith('-SNAPSHOT') if (metaDataFile.isFile()) { - def metaData = new XmlParser().parse(metaDataFile) - def timestamp = metaData.versioning.snapshot.timestamp[0].text().trim() - def build = metaData.versioning.snapshot.buildNumber[0].text().trim() + Metadata metaData = new MetadataXpp3Reader().read(metaDataFile.newReader()) + String timestamp = metaData.versioning.snapshot.timestamp + String build = metaData.versioning.snapshot.buildNumber return "${timestamp}-${build}" } return "${timestampFormat.format(publishTimestamp)}-${publishCount}" @@ -61,14 +70,6 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule return this } - String getPackaging() { - return packaging - } - - List getDependencies() { - return dependencies - } - @Override File getPomFile() { return moduleDir.resolve("$artifactId-${publishArtifactVersion}.pom") @@ -76,11 +77,11 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule @Override File getMetaDataFile() { - moduleDir.resolve(MAVEN_METADATA_FILE) + return moduleDir.resolve(MAVEN_METADATA_FILE) } File getRootMetaDataFile() { - moduleDir.parentFile.resolve(MAVEN_METADATA_FILE) + return moduleDir.parentFile.resolve(MAVEN_METADATA_FILE) } File artifactFile(Map options) { @@ -107,10 +108,9 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule MavenModule publishPom() { moduleDir.createDir() def rootMavenMetaData = getRootMetaDataFile() - updateRootMavenMetaData(rootMavenMetaData) - if (publishesMetaDataFile()) { + if (publishesMetaDataFile) { publishWithWriter(metaDataFile) { Writer writer -> writer << getMetaDataFileContent() } @@ -118,78 +118,97 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule publishWithWriter(pomFile) { Writer writer -> def pomPackaging = packaging ?: type - writer << """ - - - 4.0.0 - $groupId - $artifactId - $pomPackaging - $version - Published on $publishTimestamp - """.stripIndent() - - if (parentPomSection) { - writer << "\n$parentPomSection\n" - } - - if (!dependencies.empty) { - writer << "" - } - - dependencies.each { dependency -> - def typeAttribute = dependency['type'] == null ? "" : "$dependency.type" - writer << """ - - $dependency.groupId - $dependency.artifactId - $dependency.version - $typeAttribute - """.stripIndent() - } - - if (!dependencies.empty) { - writer << "" + // Create a new Maven Model + Model model = new Model() + model.modelVersion = '4.0.0' + model.groupId = groupId + model.artifactId = artifactId + model.version = version + model.packaging = pomPackaging + model.description = "Published on $publishTimestamp" + + dependencies.each { dep -> + Dependency dependency = new Dependency() + dependency.groupId = dep.groupId + dependency.artifactId = dep.artifactId + dependency.version = dep.version + if (dep.type) { + dependency.type = dep.type + } + if (dep.classifier) { + dependency.classifier = dep.classifier + } + model.addDependency(dependency) } - writer << "\n" + // Write the model to the POM file + MavenXpp3Writer pomWriter = new MavenXpp3Writer() + pomWriter.write(writer, model) } return this } private void updateRootMavenMetaData(File rootMavenMetaData) { - def allVersions = rootMavenMetaData.exists() ? new XmlParser().parseText(rootMavenMetaData.text).versioning.versions.version*.value().flatten() : [] + List allVersions = [] + if (rootMavenMetaData.exists()) { + def metaData = new MetadataXpp3Reader().read(rootMavenMetaData.newReader()) + allVersions = metaData.versioning.versions + } allVersions << version publishWithWriter(rootMavenMetaData) { Writer writer -> - def builder = new MarkupBuilder(writer) - builder.metadata { - groupId(groupId) - artifactId(artifactId) - version(allVersions.max()) - versioning { - if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { - snapshot { - timestamp(timestampFormat.format(publishTimestamp)) - buildNumber(publishCount) - lastUpdated(updateFormat.format(publishTimestamp)) - } - } else { - versions { - allVersions.each { currVersion -> - version(currVersion) - } - } - } - } + // Create Maven Metadata + Metadata metadata = new Metadata() + metadata.groupId = groupId + metadata.artifactId = artifactId + + Versioning versioning = new Versioning() + versioning.versions = allVersions.unique().sort() + versioning.latest = versioning.versions.last() + versioning.release = versioning.versions.last() + versioning.lastUpdated = updateFormat.format(publishTimestamp) + + if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { + Snapshot snapshot = new Snapshot() + snapshot.timestamp = timestampFormat.format(publishTimestamp) + snapshot.buildNumber = publishCount + versioning.snapshot = snapshot } + + metadata.versioning = versioning + + // Write the metadata to the file + MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() + metadataWriter.write(writer, metadata) } } - abstract String getMetaDataFileContent() + String getMetaDataFileContent() { + // Similar to updateRootMavenMetaData but for the artifact's own metadata file + StringWriter writer = new StringWriter() + // Create Maven Metadata + Metadata metadata = new Metadata() + metadata.groupId = groupId + metadata.artifactId = artifactId + metadata.version = version + + Versioning versioning = new Versioning() + versioning.lastUpdated = updateFormat.format(publishTimestamp) + if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { + Snapshot snapshot = new Snapshot() + snapshot.timestamp = timestampFormat.format(publishTimestamp) + snapshot.buildNumber = publishCount + versioning.snapshot = snapshot + } + metadata.versioning = versioning + + // Write the metadata to the string + MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() + metadataWriter.write(writer, metadata) + return writer.toString() + } @Override MavenModule publish() { - publishPom() artifacts.each { artifact -> publishArtifact(artifact as Map) @@ -213,6 +232,4 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule // Some content to include in each artifact, so that its size and content varies on each publish return (0..publishCount).join("-") } - - protected abstract boolean publishesMetaDataFile() } 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 80d482224..fe67f3986 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 @@ -3,15 +3,13 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven import org.jetbrains.annotations.NotNull class MavenFileModule extends AbstractMavenModule { - private boolean uniqueSnapshots = true - MavenFileModule(File moduleDir, String groupId, String artifactId, String version) { super(moduleDir, groupId, artifactId, version) } @Override - boolean getUniqueSnapshots() { - return uniqueSnapshots + boolean isUniqueSnapshots() { + return true } @Override @@ -40,7 +38,7 @@ class MavenFileModule extends AbstractMavenModule { } @Override - protected boolean publishesMetaDataFile() { + protected boolean isPublishesMetaDataFile() { uniqueSnapshots && version.endsWith("-SNAPSHOT") } } From fd998e54681e6f48a1113748bcd693adebbb7e30 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 10 Dec 2024 16:27:50 +0800 Subject: [PATCH 02/28] Migrate MavenFileModule to using Maven model APIs --- .../util/repo/maven/MavenFileModule.groovy | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) 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 fe67f3986..4fbfb8e48 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,5 +1,9 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven +import org.apache.maven.artifact.repository.metadata.Metadata +import org.apache.maven.artifact.repository.metadata.Snapshot +import org.apache.maven.artifact.repository.metadata.Versioning +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer import org.jetbrains.annotations.NotNull class MavenFileModule extends AbstractMavenModule { @@ -14,21 +18,21 @@ class MavenFileModule extends AbstractMavenModule { @Override String getMetaDataFileContent() { - """ - - - $groupId - $artifactId - $version - - - ${timestampFormat.format(publishTimestamp)} - $publishCount - - ${updateFormat.format(publishTimestamp)} - - - """.stripIndent() + Metadata metadata = new Metadata() + metadata.groupId = groupId + metadata.artifactId = artifactId + metadata.version = version + + Versioning versioning = new Versioning() + versioning.snapshot = new Snapshot() + versioning.snapshot.timestamp = timestampFormat.format(publishTimestamp) + versioning.snapshot.buildNumber = publishCount + versioning.lastUpdated = updateFormat.format(publishTimestamp) + metadata.versioning = versioning + + StringWriter writer = new StringWriter() + new MetadataXpp3Writer().write(writer, metadata) + return writer.toString() } @Override From 69c10b67efa85be3d6d923b283f890b77c8c8191 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 11 Dec 2024 12:18:06 +0800 Subject: [PATCH 03/28] Use Dependency in AbstractMavenModule --- .../repo/maven/AbstractMavenModule.groovy | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) 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 ebbd25440..4efa4060c 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 @@ -21,7 +21,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule protected final String version protected final def updateFormat = new SimpleDateFormat("yyyyMMddHHmmss") protected final def timestampFormat = new SimpleDateFormat("yyyyMMdd.HHmmss") - protected final List> dependencies = [] + protected final List dependencies = [] protected final List> artifacts = [] protected String type = 'jar' @@ -66,7 +66,11 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule @Override MavenModule dependsOn(String groupId, String artifactId, String version) { - this.dependencies << [groupId: groupId, artifactId: artifactId, version: version, type: type] + def dep = new Dependency() + dep.groupId = groupId + dep.artifactId = artifactId + dep.version = version + dependencies.add(dep) return this } @@ -126,20 +130,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule model.version = version model.packaging = pomPackaging model.description = "Published on $publishTimestamp" - - dependencies.each { dep -> - Dependency dependency = new Dependency() - dependency.groupId = dep.groupId - dependency.artifactId = dep.artifactId - dependency.version = dep.version - if (dep.type) { - dependency.type = dep.type - } - if (dep.classifier) { - dependency.classifier = dep.classifier - } - model.addDependency(dependency) - } + model.dependencies = dependencies // Write the model to the POM file MavenXpp3Writer pomWriter = new MavenXpp3Writer() From d515e9279188c16e76a41b74972179c9aa430c48 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 11 Dec 2024 14:24:03 +0800 Subject: [PATCH 04/28] Tweak dependsOn params --- .../shadow/util/repo/maven/AbstractMavenModule.groovy | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 4efa4060c..c1a530e84 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 @@ -57,11 +57,8 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule return "${timestampFormat.format(publishTimestamp)}-${publishCount}" } - MavenModule dependsOn(String... dependencyArtifactIds) { - for (String id : dependencyArtifactIds) { - dependsOn(groupId, id, '1.0') - } - return this + MavenModule dependsOn(String artifactId) { + return dependsOn(groupId, artifactId, '1.0') } @Override From 9d773c41f6dd76586055ce01315583eba81e3c91 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 11 Dec 2024 14:29:15 +0800 Subject: [PATCH 05/28] Replace getMetaDataFileContent with getMetaData --- .../repo/maven/AbstractMavenModule.groovy | 41 ++++--------------- .../util/repo/maven/MavenFileModule.groovy | 8 ++-- 2 files changed, 12 insertions(+), 37 deletions(-) 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 c1a530e84..294d0b3c2 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 @@ -113,7 +113,8 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule if (publishesMetaDataFile) { publishWithWriter(metaDataFile) { Writer writer -> - writer << getMetaDataFileContent() + MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() + metadataWriter.write(writer, getMetaData([])) } } @@ -144,42 +145,22 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule } allVersions << version publishWithWriter(rootMavenMetaData) { Writer writer -> - // Create Maven Metadata - Metadata metadata = new Metadata() - metadata.groupId = groupId - metadata.artifactId = artifactId - - Versioning versioning = new Versioning() - versioning.versions = allVersions.unique().sort() - versioning.latest = versioning.versions.last() - versioning.release = versioning.versions.last() - versioning.lastUpdated = updateFormat.format(publishTimestamp) - - if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { - Snapshot snapshot = new Snapshot() - snapshot.timestamp = timestampFormat.format(publishTimestamp) - snapshot.buildNumber = publishCount - versioning.snapshot = snapshot - } - - metadata.versioning = versioning - - // Write the metadata to the file MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(writer, metadata) + metadataWriter.write(writer, getMetaData(allVersions)) } } - String getMetaDataFileContent() { - // Similar to updateRootMavenMetaData but for the artifact's own metadata file - StringWriter writer = new StringWriter() - // Create Maven Metadata + /** + * Similar to updateRootMavenMetaData but for the artifact's own metadata file + */ + Metadata getMetaData(List versions) { Metadata metadata = new Metadata() metadata.groupId = groupId metadata.artifactId = artifactId metadata.version = version Versioning versioning = new Versioning() + versioning.versions = versions versioning.lastUpdated = updateFormat.format(publishTimestamp) if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { Snapshot snapshot = new Snapshot() @@ -188,11 +169,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule versioning.snapshot = snapshot } metadata.versioning = versioning - - // Write the metadata to the string - MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(writer, metadata) - return writer.toString() + return metadata } @Override 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 4fbfb8e48..b934ade49 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 @@ -3,7 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven import org.apache.maven.artifact.repository.metadata.Metadata import org.apache.maven.artifact.repository.metadata.Snapshot import org.apache.maven.artifact.repository.metadata.Versioning -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer import org.jetbrains.annotations.NotNull class MavenFileModule extends AbstractMavenModule { @@ -17,22 +16,21 @@ class MavenFileModule extends AbstractMavenModule { } @Override - String getMetaDataFileContent() { + Metadata getMetaData(List versions) { Metadata metadata = new Metadata() metadata.groupId = groupId metadata.artifactId = artifactId metadata.version = version Versioning versioning = new Versioning() + versioning.versions = versions versioning.snapshot = new Snapshot() versioning.snapshot.timestamp = timestampFormat.format(publishTimestamp) versioning.snapshot.buildNumber = publishCount versioning.lastUpdated = updateFormat.format(publishTimestamp) metadata.versioning = versioning - StringWriter writer = new StringWriter() - new MetadataXpp3Writer().write(writer, metadata) - return writer.toString() + return metadata } @Override From 53594646cd6277ddcbaa838c9c95e2d35a124b7e Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 11 Dec 2024 14:43:15 +0800 Subject: [PATCH 06/28] Replace publishWithWriter with publishWithStream --- .../shadow/util/AppendableMavenFileModule.groovy | 4 ++-- .../util/repo/maven/AbstractMavenModule.groovy | 16 ++++++++-------- .../plugins/shadow/util/repo/AbstractModule.kt | 13 ++----------- 3 files changed, 12 insertions(+), 21 deletions(-) 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 2e9022905..0f054e211 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 @@ -37,11 +37,11 @@ class AppendableMavenFileModule extends MavenFileModule { } String classifier = (String) artifact['classifier'] ?: '' if (files.containsKey(classifier)) { - publishWithStream(artifactFile) { OutputStream os -> + publish(artifactFile) { OutputStream os -> IOUtils.copy(files[classifier].newInputStream(), os) } } else { - publishWithStream(artifactFile) { OutputStream os -> + publish(artifactFile) { OutputStream os -> writeJar(os, contents[classifier]) } } 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 294d0b3c2..9f84c5a15 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 @@ -112,13 +112,13 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule updateRootMavenMetaData(rootMavenMetaData) if (publishesMetaDataFile) { - publishWithWriter(metaDataFile) { Writer writer -> + publish(metaDataFile) { OutputStream outputStream -> MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(writer, getMetaData([])) + metadataWriter.write(outputStream, getMetaData([])) } } - publishWithWriter(pomFile) { Writer writer -> + publish(pomFile) { OutputStream outputStream -> def pomPackaging = packaging ?: type // Create a new Maven Model Model model = new Model() @@ -132,7 +132,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule // Write the model to the POM file MavenXpp3Writer pomWriter = new MavenXpp3Writer() - pomWriter.write(writer, model) + pomWriter.write(outputStream, model) } return this } @@ -144,9 +144,9 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule allVersions = metaData.versioning.versions } allVersions << version - publishWithWriter(rootMavenMetaData) { Writer writer -> + publish(rootMavenMetaData) { OutputStream outputStream -> MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(writer, getMetaData(allVersions)) + metadataWriter.write(outputStream, getMetaData(allVersions)) } } @@ -187,8 +187,8 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule if (type == 'pom') { return artifactFile } - publishWithWriter(artifactFile) { Writer writer -> - writer << "${artifactFile.name} : $artifactContent" + publish(artifactFile) { OutputStream outputStream -> + outputStream.write("${artifactFile.name} : $artifactContent".bytes) } return artifactFile } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index 51d740e5d..72aadf7e7 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -3,25 +3,16 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo import com.github.jengelman.gradle.plugins.shadow.util.HashUtil import java.io.File import java.io.OutputStream -import java.io.Writer import java.math.BigInteger abstract class AbstractModule { protected abstract fun onPublish(file: File) - protected fun publishWithWriter(file: File, action: (Writer) -> Unit) { - publishCommon(file) { it.writer().use(action) } - } - - protected fun publishWithStream(file: File, action: (OutputStream) -> Unit) { - publishCommon(file) { it.outputStream().use(action) } - } - - private fun publishCommon(file: File, action: (File) -> Unit) { + protected fun publish(file: File, action: (OutputStream) -> Unit) { val hashBefore = if (file.exists()) getHash(file, "sha1") else null val tempFile = file.resolveSibling("${file.name}.tmp") - action(tempFile) + tempFile.outputStream().use(action) val hashAfter = getHash(tempFile, "sha1") if (hashAfter == hashBefore) { From bb11c970210c0cc0efc01676c3b79cd6b338ad15 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:10:23 +0800 Subject: [PATCH 07/28] Convert AbstractMavenModule --- build.gradle.kts | 3 + .../repo/maven/AbstractMavenModule.groovy | 200 ------------------ .../util/repo/maven/AbstractMavenModule.kt | 179 ++++++++++++++++ 3 files changed, 182 insertions(+), 200 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt diff --git a/build.gradle.kts b/build.gradle.kts index c3a0f66ad..7a7f4994e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -90,6 +90,9 @@ dependencies { funcTestImplementation(libs.apache.maven.modelBuilder) funcTestImplementation(libs.apache.maven.repositoryMetadata) + intiTestImplementation(libs.apache.maven.modelBuilder) + intiTestImplementation(libs.apache.maven.repositoryMetadata) + lintChecks(libs.androidx.gradlePluginLints) lintChecks(libs.assertk.lint) } 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 deleted file mode 100644 index 9f84c5a15..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.groovy +++ /dev/null @@ -1,200 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -import com.github.jengelman.gradle.plugins.shadow.util.repo.AbstractModule -import org.apache.maven.artifact.repository.metadata.Metadata -import org.apache.maven.artifact.repository.metadata.Snapshot -import org.apache.maven.artifact.repository.metadata.Versioning -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader -import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer -import org.apache.maven.model.Dependency -import org.apache.maven.model.Model -import org.apache.maven.model.io.xpp3.MavenXpp3Writer - -import java.text.SimpleDateFormat - -abstract class AbstractMavenModule extends AbstractModule implements MavenModule { - protected static final String MAVEN_METADATA_FILE = "maven-metadata.xml" - - protected final File moduleDir - protected final String groupId - protected final String artifactId - protected final String version - protected final def updateFormat = new SimpleDateFormat("yyyyMMddHHmmss") - protected final def timestampFormat = new SimpleDateFormat("yyyyMMdd.HHmmss") - protected final List dependencies = [] - protected final List> artifacts = [] - - protected String type = 'jar' - protected String packaging - protected int publishCount = 1 - - AbstractMavenModule(File moduleDir, String groupId, String artifactId, String version) { - this.moduleDir = moduleDir - this.groupId = groupId - this.artifactId = artifactId - this.version = version - } - - protected abstract boolean isUniqueSnapshots() - - protected abstract boolean isPublishesMetaDataFile() - - String getPublishArtifactVersion() { - if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { - return "${version.replaceFirst('-SNAPSHOT$', '')}-${getUniqueSnapshotVersion()}" - } - return version - } - - private String getUniqueSnapshotVersion() { - assert uniqueSnapshots && version.endsWith('-SNAPSHOT') - if (metaDataFile.isFile()) { - Metadata metaData = new MetadataXpp3Reader().read(metaDataFile.newReader()) - String timestamp = metaData.versioning.snapshot.timestamp - String build = metaData.versioning.snapshot.buildNumber - return "${timestamp}-${build}" - } - return "${timestampFormat.format(publishTimestamp)}-${publishCount}" - } - - MavenModule dependsOn(String artifactId) { - return dependsOn(groupId, artifactId, '1.0') - } - - @Override - MavenModule dependsOn(String groupId, String artifactId, String version) { - def dep = new Dependency() - dep.groupId = groupId - dep.artifactId = artifactId - dep.version = version - dependencies.add(dep) - return this - } - - @Override - File getPomFile() { - return moduleDir.resolve("$artifactId-${publishArtifactVersion}.pom") - } - - @Override - File getMetaDataFile() { - return moduleDir.resolve(MAVEN_METADATA_FILE) - } - - File getRootMetaDataFile() { - return moduleDir.parentFile.resolve(MAVEN_METADATA_FILE) - } - - File artifactFile(Map options) { - def artifact = toArtifact(options) - def fileName = "$artifactId-${publishArtifactVersion}.${artifact.type}" - if (artifact.classifier) { - fileName = "$artifactId-$publishArtifactVersion-${artifact.classifier}.${artifact.type}" - } - return moduleDir.resolve(fileName) - } - - protected Map toArtifact(Map options) { - options = new HashMap(options) - def artifact = [type: options.remove('type') ?: type, classifier: options.remove('classifier') ?: null] - assert options.isEmpty(): "Unknown options : ${options.keySet()}" - return artifact - } - - Date getPublishTimestamp() { - return new Date(updateFormat.parse("20100101120000").time + publishCount * 1000) - } - - @Override - MavenModule publishPom() { - moduleDir.createDir() - def rootMavenMetaData = getRootMetaDataFile() - updateRootMavenMetaData(rootMavenMetaData) - - if (publishesMetaDataFile) { - publish(metaDataFile) { OutputStream outputStream -> - MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(outputStream, getMetaData([])) - } - } - - publish(pomFile) { OutputStream outputStream -> - def pomPackaging = packaging ?: type - // Create a new Maven Model - Model model = new Model() - model.modelVersion = '4.0.0' - model.groupId = groupId - model.artifactId = artifactId - model.version = version - model.packaging = pomPackaging - model.description = "Published on $publishTimestamp" - model.dependencies = dependencies - - // Write the model to the POM file - MavenXpp3Writer pomWriter = new MavenXpp3Writer() - pomWriter.write(outputStream, model) - } - return this - } - - private void updateRootMavenMetaData(File rootMavenMetaData) { - List allVersions = [] - if (rootMavenMetaData.exists()) { - def metaData = new MetadataXpp3Reader().read(rootMavenMetaData.newReader()) - allVersions = metaData.versioning.versions - } - allVersions << version - publish(rootMavenMetaData) { OutputStream outputStream -> - MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer() - metadataWriter.write(outputStream, getMetaData(allVersions)) - } - } - - /** - * Similar to updateRootMavenMetaData but for the artifact's own metadata file - */ - Metadata getMetaData(List versions) { - Metadata metadata = new Metadata() - metadata.groupId = groupId - metadata.artifactId = artifactId - metadata.version = version - - Versioning versioning = new Versioning() - versioning.versions = versions - versioning.lastUpdated = updateFormat.format(publishTimestamp) - if (uniqueSnapshots && version.endsWith("-SNAPSHOT")) { - Snapshot snapshot = new Snapshot() - snapshot.timestamp = timestampFormat.format(publishTimestamp) - snapshot.buildNumber = publishCount - versioning.snapshot = snapshot - } - metadata.versioning = versioning - return metadata - } - - @Override - MavenModule publish() { - publishPom() - artifacts.each { artifact -> - publishArtifact(artifact as Map) - } - publishArtifact([:]) - return this - } - - File publishArtifact(Map artifact) { - def artifactFile = artifactFile(artifact) - if (type == 'pom') { - return artifactFile - } - publish(artifactFile) { OutputStream outputStream -> - outputStream.write("${artifactFile.name} : $artifactContent".bytes) - } - return artifactFile - } - - protected String getArtifactContent() { - // Some content to include in each artifact, so that its size and content varies on each publish - return (0..publishCount).join("-") - } -} diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt new file mode 100644 index 000000000..e612af1be --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt @@ -0,0 +1,179 @@ +package com.github.jengelman.gradle.plugins.shadow.util.repo.maven + +import com.github.jengelman.gradle.plugins.shadow.util.repo.AbstractModule +import java.io.File +import java.text.SimpleDateFormat +import java.util.Date +import org.apache.maven.artifact.repository.metadata.Metadata +import org.apache.maven.artifact.repository.metadata.Snapshot +import org.apache.maven.artifact.repository.metadata.Versioning +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer +import org.apache.maven.model.Dependency +import org.apache.maven.model.Model +import org.apache.maven.model.io.xpp3.MavenXpp3Writer + +abstract class AbstractMavenModule( + protected val moduleDir: File, + protected val groupId: String, + protected val artifactId: String, + protected val version: String, +) : AbstractModule(), + MavenModule { + + protected val updateFormat = SimpleDateFormat("yyyyMMddHHmmss") + protected val timestampFormat = SimpleDateFormat("yyyyMMdd.HHmmss") + protected val dependencies = mutableListOf() + protected val artifacts = mutableListOf>() + + protected var type: String = "jar" + protected var packaging: String? = null + protected var publishCount: Int = 1 + + protected abstract val isUniqueSnapshots: Boolean + protected abstract val isPublishesMetaDataFile: Boolean + + fun dependsOn(artifactId: String): MavenModule { + return dependsOn(groupId = groupId, artifactId = artifactId, version = version) + } + + override fun dependsOn(groupId: String, artifactId: String, version: String): MavenModule = apply { + val dep = Dependency().also { + it.groupId = groupId + it.artifactId = artifactId + it.version = version + } + dependencies.add(dep) + } + + override val pomFile: File + get() = moduleDir.resolve("$artifactId-$publishArtifactVersion.pom") + + override val metaDataFile: File + get() = moduleDir.resolve(MAVEN_METADATA_FILE) + + val rootMetaDataFile: File + get() = moduleDir.resolveSibling(MAVEN_METADATA_FILE) + + fun artifactFile(options: Map): File { + val artifact = toArtifact(options) + var fileName = "$artifactId-$publishArtifactVersion.${artifact["type"]}" + if (artifact["classifier"] != null) { + fileName = "$artifactId-$publishArtifactVersion-${artifact["classifier"]}.${artifact["type"]}" + } + return moduleDir.resolve(fileName) + } + + override fun publishPom(): MavenModule = apply { + moduleDir.mkdirs() + val rootMavenMetaData = rootMetaDataFile + updateRootMavenMetaData(rootMavenMetaData) + + if (isPublishesMetaDataFile) { + publish(metaDataFile) { outputStream -> + MetadataXpp3Writer().write(outputStream, getMetaData(emptyList())) + } + } + + publish(pomFile) { outputStream -> + val pomPackaging = packaging ?: type + val model = Model().also { + it.modelVersion = "4.0.0" + it.groupId = groupId + it.artifactId = artifactId + it.version = version + it.packaging = pomPackaging + it.description = "Published on $publishTimestamp" + it.dependencies = dependencies + } + MavenXpp3Writer().write(outputStream, model) + } + } + + open fun getMetaData(versions: List): Metadata = Metadata().also { + it.groupId = groupId + it.artifactId = artifactId + it.version = version + it.versioning = Versioning().also { versioning -> + versioning.versions = versions + versioning.lastUpdated = updateFormat.format(publishTimestamp) + if (isUniqueSnapshots && version.endsWith("-SNAPSHOT")) { + versioning.snapshot = Snapshot().apply { + timestamp = timestampFormat.format(publishTimestamp) + buildNumber = publishCount + } + } + } + } + + override fun publish(): MavenModule = apply { + publishPom() + artifacts.forEach { artifact -> + publishArtifact(artifact) + } + publishArtifact(emptyMap()) + } + + open fun publishArtifact(artifact: Map): File { + val artifactFile = artifactFile(artifact) + if (type == "pom") { + return artifactFile + } + publish(artifactFile) { outputStream -> + outputStream.write("${artifactFile.name} : $artifactContent".toByteArray()) + } + return artifactFile + } + + protected fun toArtifact(options: Map): Map { + val artifact = mutableMapOf( + "type" to (options["type"] ?: type), + "classifier" to options["classifier"], + ) + require(options.keys.isEmpty()) { "Unknown options : ${options.keys}" } + return artifact + } + + protected val publishArtifactVersion: String + get() = if (isUniqueSnapshots && version.endsWith("-SNAPSHOT")) { + "${version.removeSuffix("-SNAPSHOT")}-$uniqueSnapshotVersion" + } else { + version + } + + protected val publishTimestamp: Date + get() = Date(updateFormat.parse("20100101120000").time + publishCount * 1000) + + private fun updateRootMavenMetaData(rootMavenMetaData: File) { + val allVersions = if (rootMavenMetaData.exists()) { + MetadataXpp3Reader().read(rootMavenMetaData.reader()).versioning.versions + } else { + mutableListOf() + } + allVersions.add(version) + publish(rootMavenMetaData) { outputStream -> + MetadataXpp3Writer().write(outputStream, getMetaData(allVersions)) + } + } + + private val artifactContent: String + // Some content to include in each artifact, so that its size and content varies on each pu + get() = (0..publishCount).joinToString("-") + + private val uniqueSnapshotVersion: String + get() { + require(isUniqueSnapshots && version.endsWith("-SNAPSHOT")) + return if (metaDataFile.isFile) { + val metaData = MetadataXpp3Reader().read(metaDataFile.reader()) + val timestamp = metaData.versioning.snapshot.timestamp + val build = metaData.versioning.snapshot.buildNumber + "$timestamp-$build" + } else { + "${timestampFormat.format(publishTimestamp)}-$publishCount" + } + } + + protected companion object { + const val MAVEN_METADATA_FILE = "maven-metadata.xml" + } +} From eb2e48dfd9d98a5ce168f215b32b8680d4d1609b Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:23:05 +0800 Subject: [PATCH 08/28] Convert MavenFileModule --- .../util/repo/maven/MavenFileModule.groovy | 46 ------------------- .../shadow/util/repo/maven/MavenFileModule.kt | 40 ++++++++++++++++ 2 files changed, 40 insertions(+), 46 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt 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 deleted file mode 100644 index b934ade49..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.groovy +++ /dev/null @@ -1,46 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -import org.apache.maven.artifact.repository.metadata.Metadata -import org.apache.maven.artifact.repository.metadata.Snapshot -import org.apache.maven.artifact.repository.metadata.Versioning -import org.jetbrains.annotations.NotNull - -class MavenFileModule extends AbstractMavenModule { - MavenFileModule(File moduleDir, String groupId, String artifactId, String version) { - super(moduleDir, groupId, artifactId, version) - } - - @Override - boolean isUniqueSnapshots() { - return true - } - - @Override - Metadata getMetaData(List versions) { - Metadata metadata = new Metadata() - metadata.groupId = groupId - metadata.artifactId = artifactId - metadata.version = version - - Versioning versioning = new Versioning() - versioning.versions = versions - versioning.snapshot = new Snapshot() - versioning.snapshot.timestamp = timestampFormat.format(publishTimestamp) - versioning.snapshot.buildNumber = publishCount - versioning.lastUpdated = updateFormat.format(publishTimestamp) - metadata.versioning = versioning - - return metadata - } - - @Override - protected void onPublish(@NotNull File file) { - sha1File(file) - md5File(file) - } - - @Override - protected boolean isPublishesMetaDataFile() { - uniqueSnapshots && version.endsWith("-SNAPSHOT") - } -} diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt new file mode 100644 index 000000000..172e7c137 --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt @@ -0,0 +1,40 @@ +package com.github.jengelman.gradle.plugins.shadow.util.repo.maven + +import java.io.File +import org.apache.maven.artifact.repository.metadata.Metadata +import org.apache.maven.artifact.repository.metadata.Snapshot +import org.apache.maven.artifact.repository.metadata.Versioning + +open class MavenFileModule( + moduleDir: File, + groupId: String, + artifactId: String, + version: String, +) : AbstractMavenModule(moduleDir, groupId, artifactId, version) { + + override val isUniqueSnapshots: Boolean = true + + override fun getMetaData(versions: List): Metadata { + return Metadata().also { + it.groupId = groupId + it.artifactId = artifactId + it.version = version + it.versioning = Versioning().also { versioning -> + versioning.versions = versions + versioning.snapshot = Snapshot().apply { + timestamp = timestampFormat.format(publishTimestamp) + buildNumber = publishCount + } + versioning.lastUpdated = updateFormat.format(publishTimestamp) + } + } + } + + override fun onPublish(file: File) { + sha1File(file) + md5File(file) + } + + override val isPublishesMetaDataFile: Boolean + get() = isUniqueSnapshots && version.endsWith("-SNAPSHOT") +} From 4f4940ac6a54a6e20a8c729ccc4ba4623a3b57d7 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:25:50 +0800 Subject: [PATCH 09/28] Convert MavenFileRepository --- .../repo/maven/MavenFileRepository.groovy | 23 ------------------- .../util/repo/maven/MavenFileRepository.kt | 21 +++++++++++++++++ 2 files changed, 21 insertions(+), 23 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt 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 deleted file mode 100644 index fdff3a8ed..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.groovy +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util.repo.maven - -/** - * A fixture for dealing with file Maven repositories. - */ -class MavenFileRepository implements MavenRepository { - final File rootDir - - MavenFileRepository(File rootDir) { - this.rootDir = rootDir - } - - @Override - URI getUri() { - return rootDir.toURI() - } - - @Override - 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/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt new file mode 100644 index 000000000..620351f02 --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt @@ -0,0 +1,21 @@ +package com.github.jengelman.gradle.plugins.shadow.util.repo.maven + +import java.io.File +import java.net.URI + +/** + * A fixture for dealing with file Maven repositories. + */ +open class MavenFileRepository(protected val rootDir: File) : MavenRepository { + + override val uri: URI = rootDir.toURI() + + override fun module(groupId: String, artifactId: String): MavenFileModule { + return module(groupId = groupId, artifactId = artifactId, version = "0.0.0") + } + + override fun module(groupId: String, artifactId: String, version: String): MavenFileModule { + val artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") + return MavenFileModule(artifactDir, groupId, artifactId, version) + } +} From 91696ab4245c3c26478db1b5ef13f2963625a3cb Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:42:09 +0800 Subject: [PATCH 10/28] Update AppendableJar --- .../plugins/shadow/ShadowPluginSpec.groovy | 2 +- .../plugins/shadow/TransformerSpec.groovy | 62 +++++++++---------- .../plugins/shadow/util/AppendableJar.kt | 20 ++++-- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index f214975d5..ad867a09b 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -63,7 +63,7 @@ class ShadowPluginSpec extends PluginSpecification { @Unroll def 'Compatible with Gradle #version'() { given: - File one = buildJar('one.jar').insertFile('META-INF/services/shadow.Shadow', + File one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', 'one # NOTE: No newline terminates this line/file').write() repo.module('shadow', 'two', '1.0').insertFile('META-INF/services/shadow.Shadow', diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index aac98bc18..b6607bc0c 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -16,15 +16,15 @@ class TransformerSpec extends PluginSpecification { def 'service resource transformer'() { given: File one = buildJar('one.jar') - .insertFile('META-INF/services/org.apache.maven.Shade', + .insert('META-INF/services/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file') - .insertFile('META-INF/services/com.acme.Foo', 'one') + .insert('META-INF/services/com.acme.Foo', 'one') .write() File two = buildJar('two.jar') - .insertFile('META-INF/services/org.apache.maven.Shade', + .insert('META-INF/services/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file') - .insertFile('META-INF/services/com.acme.Foo', 'two') + .insert('META-INF/services/com.acme.Foo', 'two') .write() buildFile << """ @@ -61,10 +61,10 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer alternate path'() { given: - File one = buildJar('one.jar').insertFile('META-INF/foo/org.apache.maven.Shade', + File one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insertFile('META-INF/foo/org.apache.maven.Shade', + File two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -97,15 +97,15 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer short syntax'() { given: File one = buildJar('one.jar') - .insertFile('META-INF/services/org.apache.maven.Shade', + .insert('META-INF/services/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file') - .insertFile('META-INF/services/com.acme.Foo', 'one') + .insert('META-INF/services/com.acme.Foo', 'one') .write() File two = buildJar('two.jar') - .insertFile('META-INF/services/org.apache.maven.Shade', + .insert('META-INF/services/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file') - .insertFile('META-INF/services/com.acme.Foo', 'two') + .insert('META-INF/services/com.acme.Foo', 'two') .write() buildFile << """ @@ -142,22 +142,22 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer short syntax relocation'() { given: File one = buildJar('one.jar') - .insertFile('META-INF/services/java.sql.Driver', + .insert('META-INF/services/java.sql.Driver', '''oracle.jdbc.OracleDriver org.apache.hive.jdbc.HiveDriver'''.stripIndent()) - .insertFile('META-INF/services/org.apache.axis.components.compiler.Compiler', + .insert('META-INF/services/org.apache.axis.components.compiler.Compiler', 'org.apache.axis.components.compiler.Javac') - .insertFile('META-INF/services/org.apache.commons.logging.LogFactory', + .insert('META-INF/services/org.apache.commons.logging.LogFactory', 'org.apache.commons.logging.impl.LogFactoryImpl') .write() File two = buildJar('two.jar') - .insertFile('META-INF/services/java.sql.Driver', + .insert('META-INF/services/java.sql.Driver', '''org.apache.derby.jdbc.AutoloadedDriver com.mysql.jdbc.Driver'''.stripIndent()) - .insertFile('META-INF/services/org.apache.axis.components.compiler.Compiler', + .insert('META-INF/services/org.apache.axis.components.compiler.Compiler', 'org.apache.axis.components.compiler.Jikes') - .insertFile('META-INF/services/org.apache.commons.logging.LogFactory', + .insert('META-INF/services/org.apache.commons.logging.LogFactory', 'org.mortbay.log.Factory') .write() @@ -207,10 +207,10 @@ org.mortbay.log.Factory'''.stripIndent() def 'service resource transformer short syntax alternate path'() { given: - File one = buildJar('one.jar').insertFile('META-INF/foo/org.apache.maven.Shade', + File one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insertFile('META-INF/foo/org.apache.maven.Shade', + File two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -240,7 +240,7 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() @Issue(['SHADOW-70', 'SHADOW-71']) def 'apply transformers to project resources'() { given: - File one = buildJar('one.jar').insertFile('META-INF/services/shadow.Shadow', + File one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', 'one # NOTE: No newline terminates this line/file').write() repo.module('shadow', 'two', '1.0').insertFile('META-INF/services/shadow.Shadow', @@ -277,10 +277,10 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'appending transformer'() { given: - File one = buildJar('one.jar').insertFile('test.properties', + File one = buildJar('one.jar').insert('test.properties', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insertFile('test.properties', + File two = buildJar('two.jar').insert('test.properties', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -313,10 +313,10 @@ two # NOTE: No newline terminates this line/file def 'appending transformer short syntax'() { given: - File one = buildJar('one.jar').insertFile('test.properties', + File one = buildJar('one.jar').insert('test.properties', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insertFile('test.properties', + File two = buildJar('two.jar').insert('test.properties', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -428,7 +428,7 @@ two # NOTE: No newline terminates this line/file def 'append xml files'() { given: - File xml1 = buildJar('xml1.jar').insertFile('properties.xml', + File xml1 = buildJar('xml1.jar').insert('properties.xml', ''' @@ -437,7 +437,7 @@ two # NOTE: No newline terminates this line/file '''.stripIndent() ).write() - File xml2 = buildJar('xml2.jar').insertFile('properties.xml', + File xml2 = buildJar('xml2.jar').insert('properties.xml', ''' @@ -602,14 +602,14 @@ two # NOTE: No newline terminates this line/file def 'Groovy extension module transformer'() { given: def one = buildJar('one.jar') - .insertFile('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=foo moduleVersion=1.0.5 extensionClasses=com.acme.foo.FooExtension,com.acme.foo.BarExtension staticExtensionClasses=com.acme.foo.FooStaticExtension'''.stripIndent()).write() def two = buildJar('two.jar') - .insertFile('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=bar moduleVersion=2.3.5 extensionClasses=com.acme.bar.SomeExtension,com.acme.bar.AnotherExtension @@ -646,14 +646,14 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( def 'Groovy extension module transformer works for Groovy2_5+'() { given: def one = buildJar('one.jar') - .insertFile('META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=foo moduleVersion=1.0.5 extensionClasses=com.acme.foo.FooExtension,com.acme.foo.BarExtension staticExtensionClasses=com.acme.foo.FooStaticExtension'''.stripIndent()).write() def two = buildJar('two.jar') - .insertFile('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=bar moduleVersion=2.3.5 extensionClasses=com.acme.bar.SomeExtension,com.acme.bar.AnotherExtension @@ -691,14 +691,14 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( def 'Groovy extension module transformer short syntax'() { given: def one = buildJar('one.jar') - .insertFile('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=foo moduleVersion=1.0.5 extensionClasses=com.acme.foo.FooExtension,com.acme.foo.BarExtension staticExtensionClasses=com.acme.foo.FooStaticExtension'''.stripIndent()).write() def two = buildJar('two.jar') - .insertFile('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', + .insert('META-INF/services/org.codehaus.groovy.runtime.ExtensionModule', '''moduleName=bar moduleVersion=2.3.5 extensionClasses=com.acme.bar.SomeExtension,com.acme.bar.AnotherExtension diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt index 798a06bf7..ab23af743 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt @@ -1,20 +1,30 @@ package com.github.jengelman.gradle.plugins.shadow.util import java.io.File +import java.io.OutputStream -class AppendableJar(private val file: File) { - private val contents = mutableMapOf() +class AppendableJar(initialContents: Map) { + private val contents = initialContents.toMutableMap() + private lateinit var outputFile: File - fun insertFile(path: String, content: String): AppendableJar = apply { + constructor(outputFile: File) : this(emptyMap()) { + this.outputFile = outputFile + } + + fun insert(path: String, content: String): AppendableJar = apply { contents[path] = content } fun write(): File { - val builder = JarBuilder(file.outputStream()) + write(outputFile.outputStream()) + return outputFile + } + + private fun write(outputStream: OutputStream) { + val builder = JarBuilder(outputStream) contents.forEach { (path, content) -> builder.withFile(path, content) } builder.build() - return file } } From 4b3875577f4129c3fe6a8897bd72efaae8c2fa5a Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:50:48 +0800 Subject: [PATCH 11/28] Convert AppendableMavenFileModule --- .../util/AppendableMavenFileModule.groovy | 60 ------------------- .../plugins/shadow/util/AppendableJar.kt | 2 +- .../shadow/util/AppendableMavenFileModule.kt | 50 ++++++++++++++++ 3 files changed, 51 insertions(+), 61 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt 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 deleted file mode 100644 index 0f054e211..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.groovy +++ /dev/null @@ -1,60 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util - -import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileModule -import groovy.transform.InheritConstructors -import org.apache.commons.io.IOUtils - -@InheritConstructors -class AppendableMavenFileModule extends MavenFileModule { - - Map> contents = [:].withDefault { [:] } as Map> - Map files = [:] - - AppendableMavenFileModule use(File file) { - return use('', file) - } - - AppendableMavenFileModule use(String classifier, File file) { - files[classifier] = file - return this - } - - AppendableMavenFileModule insertFile(String path, String content) { - insertFile('', path, content) - return this - } - - AppendableMavenFileModule insertFile(String classifier, String path, String content) { - contents[classifier][path] = content - return this - } - - @Override - File publishArtifact(Map artifact) { - def artifactFile = artifactFile(artifact) - if (type == 'pom') { - return artifactFile - } - String classifier = (String) artifact['classifier'] ?: '' - if (files.containsKey(classifier)) { - publish(artifactFile) { OutputStream os -> - IOUtils.copy(files[classifier].newInputStream(), os) - } - } else { - publish(artifactFile) { OutputStream os -> - writeJar(os, contents[classifier]) - } - } - return artifactFile - } - - static void writeJar(OutputStream os, Map contents) { - if (contents) { - JarBuilder builder = new JarBuilder(os) - contents.each { path, content -> - builder.withFile(path, content) - } - builder.build() - } - } -} diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt index ab23af743..388b98b39 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt @@ -20,7 +20,7 @@ class AppendableJar(initialContents: Map) { return outputFile } - private fun write(outputStream: OutputStream) { + fun write(outputStream: OutputStream) { val builder = JarBuilder(outputStream) contents.forEach { (path, content) -> builder.withFile(path, content) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt new file mode 100644 index 000000000..49f575866 --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt @@ -0,0 +1,50 @@ +package com.github.jengelman.gradle.plugins.shadow.util + +import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileModule +import java.io.File + +class AppendableMavenFileModule( + moduleDir: File, + groupId: String, + artifactId: String, + version: String, +) : MavenFileModule(moduleDir, groupId, artifactId, version) { + + private val contents = mutableMapOf>().withDefault { mutableMapOf() } + private val files = mutableMapOf() + + fun use(file: File): AppendableMavenFileModule { + return use("", file) + } + + fun use(classifier: String, file: File): AppendableMavenFileModule = apply { + files[classifier] = file + } + + fun insertFile(path: String, content: String): AppendableMavenFileModule { + return insertFile("", path, content) + } + + fun insertFile(classifier: String, path: String, content: String): AppendableMavenFileModule = apply { + contents.getOrPut(classifier) { mutableMapOf() }[path] = content + } + + override fun publishArtifact(artifact: Map): File { + val artifactFile = artifactFile(artifact) + if (type == "pom") { + return artifactFile + } + val classifier = artifact["classifier"] as? String ?: "" + val classifierFile = files[classifier] + if (classifierFile != null) { + publish(artifactFile) { os -> + classifierFile.inputStream().copyTo(os) + } + } else { + publish(artifactFile) { os -> + AppendableJar(contents[classifier].orEmpty()).write(os) + } + } + return artifactFile + } +} From 0e55748c8d1899b1a128a6fd88da0a10b310940b Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:53:28 +0800 Subject: [PATCH 12/28] Convert AppendableMavenFileRepository --- .../util/AppendableMavenFileRepository.groovy | 14 -------------- .../shadow/util/AppendableMavenFileRepository.kt | 12 ++++++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt 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 deleted file mode 100644 index 18e4331da..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.groovy +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util - -import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileRepository -import groovy.transform.InheritConstructors - -@InheritConstructors -class AppendableMavenFileRepository extends MavenFileRepository { - - @Override - 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/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt new file mode 100644 index 000000000..b0441362b --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt @@ -0,0 +1,12 @@ +package com.github.jengelman.gradle.plugins.shadow.util + +import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileRepository +import java.io.File + +class AppendableMavenFileRepository(rootDir: File) : MavenFileRepository(rootDir) { + + override fun module(groupId: String, artifactId: String, version: String): AppendableMavenFileModule { + val artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") + return AppendableMavenFileModule(artifactDir, groupId, artifactId, version) + } +} From 37c06962383fc076afd11525490a86109814b2cb Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:54:19 +0800 Subject: [PATCH 13/28] Rename PluginSpecification to BasePluginSpecification --- .../jengelman/gradle/plugins/shadow/ApplicationSpec.groovy | 4 ++-- ...nSpecification.groovy => BasePluginSpecification.groovy} | 6 ++++-- .../gradle/plugins/shadow/ConfigurationCacheSpec.groovy | 4 +--- .../plugins/shadow/ConfigureShadowRelocationSpec.groovy | 5 +---- .../jengelman/gradle/plugins/shadow/FilteringSpec.groovy | 4 ++-- .../jengelman/gradle/plugins/shadow/PublishingSpec.groovy | 3 +-- .../jengelman/gradle/plugins/shadow/RelocationSpec.groovy | 4 ++-- .../jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy | 3 +-- .../jengelman/gradle/plugins/shadow/TransformerSpec.groovy | 3 +-- .../plugins/shadow/caching/AbstractCachingSpec.groovy | 4 ++-- 10 files changed, 17 insertions(+), 23 deletions(-) rename src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/{util/PluginSpecification.groovy => BasePluginSpecification.groovy} (95%) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy index ff6f72cb6..f31d8d06e 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy @@ -1,6 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification + import org.apache.tools.zip.ZipFile import org.gradle.testkit.runner.BuildResult import spock.lang.Issue @@ -8,7 +8,7 @@ import spock.lang.Issue import java.util.jar.Attributes import java.util.jar.JarFile -class ApplicationSpec extends PluginSpecification { +class ApplicationSpec extends BasePluginSpecification { def 'integration with application plugin'() { given: diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/PluginSpecification.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy similarity index 95% rename from src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/PluginSpecification.groovy rename to src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy index 780763716..0c669fa03 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/PluginSpecification.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy @@ -1,5 +1,7 @@ -package com.github.jengelman.gradle.plugins.shadow.util +package com.github.jengelman.gradle.plugins.shadow +import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar +import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository import org.codehaus.plexus.util.IOUtil import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner @@ -11,7 +13,7 @@ import java.util.function.Function import java.util.jar.JarEntry import java.util.jar.JarFile -abstract class PluginSpecification extends Specification { +abstract class BasePluginSpecification extends Specification { @TempDir Path dir diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.groovy index 9bb5ef2a0..253db82bc 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.groovy @@ -1,8 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification - -class ConfigurationCacheSpec extends PluginSpecification { +class ConfigurationCacheSpec extends BasePluginSpecification { @Override def setup() { diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy index 2c5af9c15..654ef5800 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ConfigureShadowRelocationSpec.groovy @@ -1,9 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification - - -class ConfigureShadowRelocationSpec extends PluginSpecification { +class ConfigureShadowRelocationSpec extends BasePluginSpecification { def "auto relocate plugin dependencies"() { given: diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy index 970e33e8b..2829eeb09 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy @@ -1,12 +1,12 @@ package com.github.jengelman.gradle.plugins.shadow -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification + import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome import spock.lang.Ignore import spock.lang.Issue -class FilteringSpec extends PluginSpecification { +class FilteringSpec extends BasePluginSpecification { @Override def setup() { diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy index e6e399b01..827144ca9 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy @@ -1,14 +1,13 @@ package com.github.jengelman.gradle.plugins.shadow import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification import groovy.json.JsonSlurper import groovy.xml.XmlSlurper import org.gradle.api.attributes.Bundling import org.gradle.api.attributes.Usage import spock.lang.Issue -class PublishingSpec extends PluginSpecification { +class PublishingSpec extends BasePluginSpecification { AppendableMavenFileRepository publishingRepo diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy index a72008197..cbd01bcb3 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy @@ -1,13 +1,13 @@ package com.github.jengelman.gradle.plugins.shadow -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification + import spock.lang.Ignore import spock.lang.Issue import java.util.jar.Attributes import java.util.jar.JarFile -class RelocationSpec extends PluginSpecification { +class RelocationSpec extends BasePluginSpecification { @Issue('SHADOW-58') def "relocate dependency files"() { diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index ad867a09b..5d06809f5 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -1,7 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification import org.gradle.api.JavaVersion import org.gradle.api.Project import org.gradle.api.artifacts.Configuration @@ -17,7 +16,7 @@ import spock.lang.Unroll import java.util.jar.Attributes import java.util.jar.JarFile -class ShadowPluginSpec extends PluginSpecification { +class ShadowPluginSpec extends BasePluginSpecification { def 'apply plugin'() { given: diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index b6607bc0c..767eb253e 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -4,14 +4,13 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransfor import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer import com.github.jengelman.gradle.plugins.shadow.transformers.XmlAppendingTransformer -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification import spock.lang.Issue import spock.lang.Unroll import java.util.jar.JarInputStream import java.util.jar.Manifest -class TransformerSpec extends PluginSpecification { +class TransformerSpec extends BasePluginSpecification { def 'service resource transformer'() { given: diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy index 72ebb78d4..369e81cfa 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy @@ -1,6 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.caching -import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification +import com.github.jengelman.gradle.plugins.shadow.BasePluginSpecification import org.apache.commons.io.FileUtils import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome @@ -11,7 +11,7 @@ import java.nio.file.Path import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE import static org.gradle.testkit.runner.TaskOutcome.SUCCESS -abstract class AbstractCachingSpec extends PluginSpecification { +abstract class AbstractCachingSpec extends BasePluginSpecification { @TempDir Path alternateDir From a93eba6c4fdd13eb293960a7fc5e3397dd51cac7 Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Tue, 17 Dec 2024 11:56:49 +0800 Subject: [PATCH 14/28] Apply suggestions from code review --- .../jengelman/gradle/plugins/shadow/ApplicationSpec.groovy | 1 - .../github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy | 1 - .../github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy | 1 - 3 files changed, 3 deletions(-) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy index f31d8d06e..77c888a38 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ApplicationSpec.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow - import org.apache.tools.zip.ZipFile import org.gradle.testkit.runner.BuildResult import spock.lang.Issue diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy index 2829eeb09..60dc6ac47 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/FilteringSpec.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow - import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome import spock.lang.Ignore diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy index cbd01bcb3..244fbbcdf 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/RelocationSpec.groovy @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow - import spock.lang.Ignore import spock.lang.Issue From 14ca3892c0ef51d0b41a99502aaeaa4de0c4afdd Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 17 Dec 2024 11:58:59 +0800 Subject: [PATCH 15/28] Cleanups --- build.gradle.kts | 4 ++-- gradle/libs.versions.toml | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7a7f4994e..feb7d1439 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -54,6 +54,8 @@ val intiTestRuntimeOnly: Configuration by configurations.getting { val funcTest: SourceSet by sourceSets.creating val funcTestImplementation: Configuration by configurations.getting { extendsFrom(configurations.testImplementation.get()) + // TODO: this will be removed after we migrated all functional tests to Kotlin. + extendsFrom(intiTestImplementation) } val funcTestRuntimeOnly: Configuration by configurations.getting { extendsFrom(configurations.testRuntimeOnly.get()) @@ -87,8 +89,6 @@ dependencies { } funcTestImplementation(sourceSets.main.get().output) funcTestImplementation(intiTest.output) - funcTestImplementation(libs.apache.maven.modelBuilder) - funcTestImplementation(libs.apache.maven.repositoryMetadata) intiTestImplementation(libs.apache.maven.modelBuilder) intiTestImplementation(libs.apache.maven.repositoryMetadata) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 855f3ead6..7604e3ad3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,10 +1,13 @@ +[versions] +maven = "3.9.9" + [libraries] apache-ant = "org.apache.ant:ant:1.10.15" apache-commonsIo = "commons-io:commons-io:2.18.0" apache-commonsLang = "org.apache.commons:commons-lang3:3.17.0" apache-log4j = "org.apache.logging.log4j:log4j-core:2.24.3" -apache-maven-modelBuilder = "org.apache.maven:maven-model-builder:3.9.9" -apache-maven-repositoryMetadata = "org.apache.maven:maven-repository-metadata:3.9.9" +apache-maven-modelBuilder = { module = "org.apache.maven:maven-model-builder", version.ref = "maven" } +apache-maven-repositoryMetadata = { module = "org.apache.maven:maven-repository-metadata", version.ref = "maven" } asm = "org.ow2.asm:asm-commons:9.7.1" jdependency = "org.vafer:jdependency:2.11" jdom2 = "org.jdom:jdom2:2.0.6.1" From 290ac5a0ab7aebc72131cf09514d9192855e7653 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 20 Dec 2024 09:48:23 +0800 Subject: [PATCH 16/28] Simplify JarBuilder --- .../gradle/plugins/shadow/util/JarBuilder.kt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt index d4c81cabb..c2993238c 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt @@ -5,20 +5,17 @@ import java.util.jar.JarEntry import java.util.jar.JarOutputStream class JarBuilder(os: OutputStream) { - private val entries = mutableListOf() + private val entries = mutableSetOf() private val jos = JarOutputStream(os) private fun addDirectory(name: String) { - if (!entries.contains(name)) { + if (entries.add(name)) { val parent = name.substringBeforeLast('/', "") if (parent.isNotEmpty() && !entries.contains(parent)) { addDirectory(parent) } - // directory entries must end in "/" - val entry = JarEntry("$name/") - jos.putNextEntry(entry) - entries.add(name) + jos.putNextEntry(JarEntry("$name/")) } } @@ -27,11 +24,9 @@ class JarBuilder(os: OutputStream) { if (idx != -1) { addDirectory(path.substring(0, idx)) } - if (!entries.contains(path)) { - val entry = JarEntry(path) - jos.putNextEntry(entry) - entries.add(path) - data.byteInputStream().use { it.copyTo(jos) } + if (entries.add(path)) { + jos.putNextEntry(JarEntry(path)) + data.byteInputStream().copyTo(jos) } return this } From fc470ae4fddc5d5414be0c4410d44409ff55e080 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 21 Dec 2024 20:16:02 +0800 Subject: [PATCH 17/28] Simplify HashUtil --- build.gradle.kts | 1 + gradle/libs.versions.toml | 1 + .../gradle/plugins/shadow/util/HashUtil.kt | 54 ------------------- .../gradle/plugins/shadow/util/HashValue.kt | 7 --- .../shadow/util/repo/AbstractModule.kt | 24 ++++++++- 5 files changed, 24 insertions(+), 63 deletions(-) delete mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashUtil.kt delete mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashValue.kt diff --git a/build.gradle.kts b/build.gradle.kts index feb7d1439..82187aafe 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -90,6 +90,7 @@ dependencies { funcTestImplementation(sourceSets.main.get().output) funcTestImplementation(intiTest.output) + intiTestImplementation(libs.okio) intiTestImplementation(libs.apache.maven.modelBuilder) intiTestImplementation(libs.apache.maven.repositoryMetadata) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7604e3ad3..9112523a9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,6 +14,7 @@ jdom2 = "org.jdom:jdom2:2.0.6.1" plexus-utils = "org.codehaus.plexus:plexus-utils:4.0.2" plexus-xml = "org.codehaus.plexus:plexus-xml:4.0.4" xmlunit = "org.xmlunit:xmlunit-legacy:2.10.0" +okio = "com.squareup.okio:okio:3.9.1" pluginPublish = "com.gradle.publish:plugin-publish-plugin:1.3.0" mavenPublish = "com.vanniktech:gradle-maven-publish-plugin:0.30.0" diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashUtil.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashUtil.kt deleted file mode 100644 index 492a8a526..000000000 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashUtil.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util -import java.io.File -import java.io.FileInputStream -import java.io.FileNotFoundException -import java.io.IOException -import java.io.InputStream -import java.io.UncheckedIOException -import java.security.MessageDigest -import java.security.NoSuchAlgorithmException -import org.gradle.internal.UncheckedException - -object HashUtil { - fun createHash(file: File, algorithm: String): HashValue { - try { - return createHash(FileInputStream(file), algorithm) - } catch (e: UncheckedIOException) { - // Catch any unchecked io exceptions and add the file path for troubleshooting - throw UncheckedIOException( - "Failed to create $algorithm hash for file ${file.absolutePath}.", - e.cause, - ) - } catch (e: FileNotFoundException) { - throw UncheckedIOException(e) - } - } - - private fun createHash(inputStream: InputStream, algorithm: String): HashValue { - val messageDigest: MessageDigest - try { - messageDigest = createMessageDigest(algorithm) - val buffer = ByteArray(4096) - inputStream.use { - while (true) { - val nread = it.read(buffer) - if (nread < 0) { - break - } - messageDigest.update(buffer, 0, nread) - } - } - } catch (e: IOException) { - throw UncheckedIOException(e) - } - return HashValue(messageDigest.digest()) - } - - private fun createMessageDigest(algorithm: String): MessageDigest { - try { - return MessageDigest.getInstance(algorithm) - } catch (e: NoSuchAlgorithmException) { - throw UncheckedException.throwAsUncheckedException(e) - } - } -} diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashValue.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashValue.kt deleted file mode 100644 index 821a0953b..000000000 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/HashValue.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util - -import java.math.BigInteger - -data class HashValue(val digest: BigInteger) { - constructor(digest: ByteArray) : this(BigInteger(1, digest)) -} diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index 72aadf7e7..61a9a41d6 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -1,9 +1,11 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo -import com.github.jengelman.gradle.plugins.shadow.util.HashUtil import java.io.File +import java.io.FileNotFoundException import java.io.OutputStream +import java.io.UncheckedIOException import java.math.BigInteger +import okio.ByteString.Companion.toByteString abstract class AbstractModule { @@ -48,7 +50,25 @@ abstract class AbstractModule { } private fun getHash(file: File, algorithm: String): BigInteger { - return HashUtil.createHash(file, algorithm.uppercase()).digest + try { + val byteString = file.readBytes().toByteString() + val byteArray = when (algorithm.uppercase()) { + "MD5" -> byteString.md5() + "SHA1" -> byteString.sha1() + "SHA256" -> byteString.sha256() + "SHA512" -> byteString.sha512() + else -> throw IllegalArgumentException("Unsupported algorithm: $algorithm") + }.toByteArray() + return BigInteger(1, byteArray) + } catch (e: UncheckedIOException) { + // Catch any unchecked io exceptions and add the file path for troubleshooting + throw UncheckedIOException( + "Failed to create $algorithm hash for file ${file.absolutePath}.", + e.cause, + ) + } catch (e: FileNotFoundException) { + throw UncheckedIOException(e) + } } } } From 970bd5dd82d1bb4f5be728e308873273a6462342 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 21 Dec 2024 20:29:05 +0800 Subject: [PATCH 18/28] Simplify AbstractModule --- .../gradle/plugins/shadow/util/repo/AbstractModule.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index 61a9a41d6..ccb87e235 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -28,12 +28,10 @@ abstract class AbstractModule { } companion object { - @JvmStatic fun sha1File(file: File): File { return hashFile(file, "sha1", 40) } - @JvmStatic fun md5File(file: File): File { return hashFile(file, "md5", 32) } @@ -46,7 +44,7 @@ abstract class AbstractModule { } private fun getHashFile(file: File, algorithm: String): File { - return File(file.parentFile, "${file.name}.$algorithm") + return file.resolveSibling("${file.name}.$algorithm") } private fun getHash(file: File, algorithm: String): BigInteger { From 83dfbbff80829189075c1394c03bb43cf88d0ec5 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 11:43:38 +0800 Subject: [PATCH 19/28] Mark rootDir public --- .../plugins/shadow/util/repo/maven/MavenFileRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt index 620351f02..0e3dedc12 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt @@ -6,7 +6,7 @@ import java.net.URI /** * A fixture for dealing with file Maven repositories. */ -open class MavenFileRepository(protected val rootDir: File) : MavenRepository { +open class MavenFileRepository(val rootDir: File) : MavenRepository { override val uri: URI = rootDir.toURI() From b75ef13fc1cb793322d06cfd60e432a98087d360 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 11:54:41 +0800 Subject: [PATCH 20/28] Rename onPublish to postPublish --- .../gradle/plugins/shadow/util/repo/AbstractModule.kt | 4 ++-- .../gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index ccb87e235..b728a4b55 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -9,7 +9,7 @@ import okio.ByteString.Companion.toByteString abstract class AbstractModule { - protected abstract fun onPublish(file: File) + protected abstract fun postPublish(file: File) protected fun publish(file: File, action: (OutputStream) -> Unit) { val hashBefore = if (file.exists()) getHash(file, "sha1") else null @@ -24,7 +24,7 @@ abstract class AbstractModule { check(!file.exists() || file.delete()) check(tempFile.renameTo(file)) - onPublish(file) + postPublish(file) } companion object { diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt index 172e7c137..ca309a01d 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt @@ -30,7 +30,7 @@ open class MavenFileModule( } } - override fun onPublish(file: File) { + override fun postPublish(file: File) { sha1File(file) md5File(file) } From 0e1d01c1440ae3ab7117be751c53cf04bd8d27da Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 12:19:08 +0800 Subject: [PATCH 21/28] Migrate File usages to Path --- .../shadow/BasePluginSpecification.groovy | 13 ++-- .../plugins/shadow/PublishingSpec.groovy | 24 ++++---- .../plugins/shadow/ShadowPluginSpec.groovy | 6 +- .../plugins/shadow/TransformerSpec.groovy | 38 ++++++------ .../shadow/caching/AbstractCachingSpec.groovy | 4 -- .../plugins/shadow/util/AppendableJar.kt | 17 +++--- .../shadow/util/AppendableMavenFileModule.kt | 33 +++++----- .../util/AppendableMavenFileRepository.kt | 4 +- .../gradle/plugins/shadow/util/JarBuilder.kt | 2 +- .../shadow/util/repo/AbstractModule.kt | 60 ++++++++++--------- .../util/repo/maven/AbstractMavenModule.kt | 43 +++++++------ .../shadow/util/repo/maven/MavenFileModule.kt | 10 ++-- .../util/repo/maven/MavenFileRepository.kt | 6 +- .../shadow/util/repo/maven/MavenModule.kt | 6 +- 14 files changed, 136 insertions(+), 130 deletions(-) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy index 0c669fa03..79f4c86ee 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy @@ -9,6 +9,7 @@ import spock.lang.Specification import spock.lang.TempDir import java.nio.file.Path +import java.nio.file.Paths import java.util.function.Function import java.util.jar.JarEntry import java.util.jar.JarFile @@ -120,7 +121,7 @@ abstract class BasePluginSpecification extends Specification { } AppendableMavenFileRepository repo(String path = 'maven-repo') { - new AppendableMavenFileRepository(dir.resolve(path).toFile()) + new AppendableMavenFileRepository(dir.resolve(path)) } void assertJarFileContentsEqual(File f, String path, String contents) { @@ -163,7 +164,7 @@ abstract class BasePluginSpecification extends Specification { } AppendableJar buildJar(String path) { - return new AppendableJar(file(path)) + return new AppendableJar(file(path).toPath()) } protected File getOutput() { @@ -174,8 +175,8 @@ abstract class BasePluginSpecification extends Specification { getFile("build/libs/${name}") } - protected File getTestJar(String name = 'junit-3.8.2.jar') { - return new File(this.class.classLoader.getResource(name).toURI()) + protected Path getTestJar(String name = 'junit-3.8.2.jar') { + return Paths.get(this.class.classLoader.getResource(name).toURI()) } protected static File getTestKitDir() { @@ -185,4 +186,8 @@ abstract class BasePluginSpecification extends Specification { } return new File(gradleUserHome, "testkit") } + + protected static String escapedPath(Path path) { + path.toString().replaceAll('\\\\', '\\\\\\\\') + } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy index 827144ca9..6f2616897 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy @@ -59,14 +59,14 @@ class PublishingSpec extends BasePluginSpecification { run('publish') then: - File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile + File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').toFile().canonicalFile assert publishedFile.exists() and: contains(publishedFile, ['a.properties', 'a2.properties']) and: - File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile + File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile assert pom.exists() def contents = new XmlSlurper().parse(pom) @@ -127,7 +127,7 @@ class PublishingSpec extends BasePluginSpecification { run('publish') then: - File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0-my-classifier.my-ext').canonicalFile + File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0-my-classifier.my-ext').toFile().canonicalFile assert publishedFile.exists() } @@ -208,14 +208,14 @@ class PublishingSpec extends BasePluginSpecification { run('publish') then: - File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile + File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').toFile().canonicalFile assert publishedFile.exists() and: contains(publishedFile, ['a.properties', 'a2.properties']) and: - File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile + File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile assert pom.exists() def contents = new XmlSlurper().parse(pom) @@ -272,8 +272,8 @@ class PublishingSpec extends BasePluginSpecification { run('publish') then: - File mainJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.jar').canonicalFile - File shadowJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0-all.jar').canonicalFile + File mainJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.jar').toFile().canonicalFile + File shadowJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0-all.jar').toFile().canonicalFile assert mainJar.exists() assert shadowJar.exists() @@ -281,8 +281,8 @@ class PublishingSpec extends BasePluginSpecification { contains(shadowJar, ['a.properties', 'a2.properties']) and: "publishes both a POM file and a Gradle metadata file" - File pom = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.pom').canonicalFile - File gmm = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.module').canonicalFile + File pom = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.pom').toFile().canonicalFile + File gmm = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.module').toFile().canonicalFile pom.exists() gmm.exists() @@ -327,13 +327,13 @@ class PublishingSpec extends BasePluginSpecification { and: "verify shadow publication" assertions { - shadowJar = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0-all.jar').canonicalFile + shadowJar = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0-all.jar').toFile().canonicalFile assert shadowJar.exists() contains(shadowJar, ['a.properties', 'a2.properties']) } assertions { - pom = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.pom').canonicalFile + pom = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile assert pom.exists() pomContents = new XmlSlurper().parse(pom) assert pomContents.dependencies[0].dependency.size() == 1 @@ -350,7 +350,7 @@ class PublishingSpec extends BasePluginSpecification { } assertions { - gmm = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.module').canonicalFile + gmm = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.module').toFile().canonicalFile assert gmm.exists() gmmContents = new JsonSlurper().parse(gmm) assert gmmContents.variants.size() == 1 diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index 5d06809f5..33d49dcc9 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -62,7 +62,7 @@ class ShadowPluginSpec extends BasePluginSpecification { @Unroll def 'Compatible with Gradle #version'() { given: - File one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', + def one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', 'one # NOTE: No newline terminates this line/file').write() repo.module('shadow', 'two', '1.0').insertFile('META-INF/services/shadow.Shadow', @@ -1271,8 +1271,4 @@ class ShadowPluginSpec extends BasePluginSpecification { def jarFile = new JarFile(output("shadow-1.0-tests.jar")) assert jarFile.getEntry('junit') != null } - - private String escapedPath(File file) { - file.path.replaceAll('\\\\', '\\\\\\\\') - } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index 767eb253e..1e3686980 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -14,13 +14,13 @@ class TransformerSpec extends BasePluginSpecification { def 'service resource transformer'() { given: - File one = buildJar('one.jar') + def one = buildJar('one.jar') .insert('META-INF/services/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file') .insert('META-INF/services/com.acme.Foo', 'one') .write() - File two = buildJar('two.jar') + def two = buildJar('two.jar') .insert('META-INF/services/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file') .insert('META-INF/services/com.acme.Foo', 'two') @@ -60,10 +60,10 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer alternate path'() { given: - File one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', + def one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', + def two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -95,13 +95,13 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer short syntax'() { given: - File one = buildJar('one.jar') + def one = buildJar('one.jar') .insert('META-INF/services/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file') .insert('META-INF/services/com.acme.Foo', 'one') .write() - File two = buildJar('two.jar') + def two = buildJar('two.jar') .insert('META-INF/services/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file') .insert('META-INF/services/com.acme.Foo', 'two') @@ -140,7 +140,7 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'service resource transformer short syntax relocation'() { given: - File one = buildJar('one.jar') + def one = buildJar('one.jar') .insert('META-INF/services/java.sql.Driver', '''oracle.jdbc.OracleDriver org.apache.hive.jdbc.HiveDriver'''.stripIndent()) @@ -150,7 +150,7 @@ org.apache.hive.jdbc.HiveDriver'''.stripIndent()) 'org.apache.commons.logging.impl.LogFactoryImpl') .write() - File two = buildJar('two.jar') + def two = buildJar('two.jar') .insert('META-INF/services/java.sql.Driver', '''org.apache.derby.jdbc.AutoloadedDriver com.mysql.jdbc.Driver'''.stripIndent()) @@ -206,10 +206,10 @@ org.mortbay.log.Factory'''.stripIndent() def 'service resource transformer short syntax alternate path'() { given: - File one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', + def one = buildJar('one.jar').insert('META-INF/foo/org.apache.maven.Shade', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', + def two = buildJar('two.jar').insert('META-INF/foo/org.apache.maven.Shade', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -239,7 +239,7 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() @Issue(['SHADOW-70', 'SHADOW-71']) def 'apply transformers to project resources'() { given: - File one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', + def one = buildJar('one.jar').insert('META-INF/services/shadow.Shadow', 'one # NOTE: No newline terminates this line/file').write() repo.module('shadow', 'two', '1.0').insertFile('META-INF/services/shadow.Shadow', @@ -276,10 +276,10 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() def 'appending transformer'() { given: - File one = buildJar('one.jar').insert('test.properties', + def one = buildJar('one.jar').insert('test.properties', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insert('test.properties', + def two = buildJar('two.jar').insert('test.properties', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -312,10 +312,10 @@ two # NOTE: No newline terminates this line/file def 'appending transformer short syntax'() { given: - File one = buildJar('one.jar').insert('test.properties', + def one = buildJar('one.jar').insert('test.properties', 'one # NOTE: No newline terminates this line/file').write() - File two = buildJar('two.jar').insert('test.properties', + def two = buildJar('two.jar').insert('test.properties', 'two # NOTE: No newline terminates this line/file').write() buildFile << """ @@ -427,7 +427,7 @@ two # NOTE: No newline terminates this line/file def 'append xml files'() { given: - File xml1 = buildJar('xml1.jar').insert('properties.xml', + def xml1 = buildJar('xml1.jar').insert('properties.xml', ''' @@ -436,7 +436,7 @@ two # NOTE: No newline terminates this line/file '''.stripIndent() ).write() - File xml2 = buildJar('xml2.jar').insert('properties.xml', + def xml2 = buildJar('xml2.jar').insert('properties.xml', ''' @@ -765,8 +765,4 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( 'ServiceFileTransformer' | '' 'XmlAppendingTransformer' | '' } - - private String escapedPath(File file) { - file.path.replaceAll('\\\\', '\\\\\\\\') - } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy index 369e81cfa..15a08ced3 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy @@ -46,10 +46,6 @@ abstract class AbstractCachingSpec extends BasePluginSpecification { return runner.withProjectDir(alternateDir.toFile()).withArguments(cacheArguments).build() } - private String escapedPath(File file) { - file.path.replaceAll('\\\\', '\\\\\\\\') - } - void assertShadowJarHasResult(TaskOutcome expectedOutcome) { def result = runWithCacheEnabled(shadowJarTask) assert result.task(shadowJarTask).outcome == expectedOutcome diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt index 388b98b39..a6f69b91d 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt @@ -1,29 +1,30 @@ package com.github.jengelman.gradle.plugins.shadow.util -import java.io.File import java.io.OutputStream +import java.nio.file.Path +import kotlin.io.path.outputStream class AppendableJar(initialContents: Map) { private val contents = initialContents.toMutableMap() - private lateinit var outputFile: File + private lateinit var outputPath: Path - constructor(outputFile: File) : this(emptyMap()) { - this.outputFile = outputFile + constructor(outputPath: Path) : this(emptyMap()) { + this.outputPath = outputPath } fun insert(path: String, content: String): AppendableJar = apply { contents[path] = content } - fun write(): File { - write(outputFile.outputStream()) - return outputFile + fun write(): Path { + write(outputPath.outputStream()) + return outputPath } fun write(outputStream: OutputStream) { val builder = JarBuilder(outputStream) contents.forEach { (path, content) -> - builder.withFile(path, content) + builder.withPath(path, content) } builder.build() } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt index 49f575866..2d4384de2 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt @@ -1,24 +1,25 @@ package com.github.jengelman.gradle.plugins.shadow.util import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileModule -import java.io.File +import java.nio.file.Path +import kotlin.io.path.inputStream class AppendableMavenFileModule( - moduleDir: File, + moduleDir: Path, groupId: String, artifactId: String, version: String, ) : MavenFileModule(moduleDir, groupId, artifactId, version) { private val contents = mutableMapOf>().withDefault { mutableMapOf() } - private val files = mutableMapOf() + private val paths = mutableMapOf() - fun use(file: File): AppendableMavenFileModule { - return use("", file) + fun use(path: Path): AppendableMavenFileModule { + return use("", path) } - fun use(classifier: String, file: File): AppendableMavenFileModule = apply { - files[classifier] = file + fun use(classifier: String, path: Path): AppendableMavenFileModule = apply { + paths[classifier] = path } fun insertFile(path: String, content: String): AppendableMavenFileModule { @@ -29,22 +30,22 @@ class AppendableMavenFileModule( contents.getOrPut(classifier) { mutableMapOf() }[path] = content } - override fun publishArtifact(artifact: Map): File { - val artifactFile = artifactFile(artifact) + override fun publishArtifact(artifact: Map): Path { + val artifactPath = artifactPath(artifact) if (type == "pom") { - return artifactFile + return artifactPath } val classifier = artifact["classifier"] as? String ?: "" - val classifierFile = files[classifier] - if (classifierFile != null) { - publish(artifactFile) { os -> - classifierFile.inputStream().copyTo(os) + val classifierPath = paths[classifier] + if (classifierPath != null) { + publish(artifactPath) { os -> + classifierPath.inputStream().copyTo(os) } } else { - publish(artifactFile) { os -> + publish(artifactPath) { os -> AppendableJar(contents[classifier].orEmpty()).write(os) } } - return artifactFile + return artifactPath } } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt index b0441362b..e6e980eff 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt @@ -1,9 +1,9 @@ package com.github.jengelman.gradle.plugins.shadow.util import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileRepository -import java.io.File +import java.nio.file.Path -class AppendableMavenFileRepository(rootDir: File) : MavenFileRepository(rootDir) { +class AppendableMavenFileRepository(rootDir: Path) : MavenFileRepository(rootDir) { override fun module(groupId: String, artifactId: String, version: String): AppendableMavenFileModule { val artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt index c2993238c..2372d5823 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarBuilder.kt @@ -19,7 +19,7 @@ class JarBuilder(os: OutputStream) { } } - fun withFile(path: String, data: String): JarBuilder { + fun withPath(path: String, data: String): JarBuilder { val idx = path.lastIndexOf('/') if (idx != -1) { addDirectory(path.substring(0, idx)) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index b728a4b55..8249bb6db 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -1,55 +1,63 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo -import java.io.File -import java.io.FileNotFoundException import java.io.OutputStream import java.io.UncheckedIOException import java.math.BigInteger +import java.nio.file.Path +import kotlin.io.path.absolutePathString +import kotlin.io.path.deleteIfExists +import kotlin.io.path.exists +import kotlin.io.path.moveTo +import kotlin.io.path.name +import kotlin.io.path.outputStream +import kotlin.io.path.readBytes +import kotlin.io.path.writeText import okio.ByteString.Companion.toByteString abstract class AbstractModule { - protected abstract fun postPublish(file: File) + protected abstract fun postPublish(path: Path) - protected fun publish(file: File, action: (OutputStream) -> Unit) { - val hashBefore = if (file.exists()) getHash(file, "sha1") else null - val tempFile = file.resolveSibling("${file.name}.tmp") - tempFile.outputStream().use(action) + protected fun publish(path: Path, action: (OutputStream) -> Unit) { + val hashBefore = if (path.exists()) getHash(path, "sha1") else null + val tempPath = path.resolveSibling("${path.name}.tmp") + tempPath.outputStream().use(action) - val hashAfter = getHash(tempFile, "sha1") + val hashAfter = getHash(tempPath, "sha1") if (hashAfter == hashBefore) { // Already published return } - check(!file.exists() || file.delete()) - check(tempFile.renameTo(file)) - postPublish(file) + check(!path.deleteIfExists()) + tempPath.moveTo(path) + check(path.exists()) + postPublish(path) } companion object { - fun sha1File(file: File): File { - return hashFile(file, "sha1", 40) + fun writeSha1Path(path: Path): Path { + return writeHashPath(path, "sha1", 40) } - fun md5File(file: File): File { - return hashFile(file, "md5", 32) + fun writeMd5Path(path: Path): Path { + return writeHashPath(path, "md5", 32) } - private fun hashFile(file: File, algorithm: String, len: Int): File { - val hashFile = getHashFile(file, algorithm) - val hash = getHash(file, algorithm) - hashFile.writeText("$hash${len}x") - return hashFile + private fun writeHashPath(path: Path, algorithm: String, len: Int): Path { + val hashPath = getHashPath(path, algorithm) + val hash = getHash(path, algorithm) + hashPath.writeText("$hash${len}x") + return hashPath } - private fun getHashFile(file: File, algorithm: String): File { - return file.resolveSibling("${file.name}.$algorithm") + private fun getHashPath(path: Path, algorithm: String): Path { + return path.resolveSibling("${path.name}.$algorithm") } - private fun getHash(file: File, algorithm: String): BigInteger { + private fun getHash(path: Path, algorithm: String): BigInteger { try { - val byteString = file.readBytes().toByteString() + val byteString = path.readBytes().toByteString() val byteArray = when (algorithm.uppercase()) { "MD5" -> byteString.md5() "SHA1" -> byteString.sha1() @@ -61,11 +69,9 @@ abstract class AbstractModule { } catch (e: UncheckedIOException) { // Catch any unchecked io exceptions and add the file path for troubleshooting throw UncheckedIOException( - "Failed to create $algorithm hash for file ${file.absolutePath}.", + "Failed to create $algorithm hash for file ${path.absolutePathString()}.", e.cause, ) - } catch (e: FileNotFoundException) { - throw UncheckedIOException(e) } } } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt index e612af1be..5b05be47c 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt @@ -1,9 +1,14 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven import com.github.jengelman.gradle.plugins.shadow.util.repo.AbstractModule -import java.io.File +import java.nio.file.Path import java.text.SimpleDateFormat import java.util.Date +import kotlin.io.path.createDirectories +import kotlin.io.path.exists +import kotlin.io.path.isRegularFile +import kotlin.io.path.name +import kotlin.io.path.reader import org.apache.maven.artifact.repository.metadata.Metadata import org.apache.maven.artifact.repository.metadata.Snapshot import org.apache.maven.artifact.repository.metadata.Versioning @@ -14,7 +19,7 @@ import org.apache.maven.model.Model import org.apache.maven.model.io.xpp3.MavenXpp3Writer abstract class AbstractMavenModule( - protected val moduleDir: File, + protected val moduleDir: Path, protected val groupId: String, protected val artifactId: String, protected val version: String, @@ -46,16 +51,16 @@ abstract class AbstractMavenModule( dependencies.add(dep) } - override val pomFile: File + override val pomPath: Path get() = moduleDir.resolve("$artifactId-$publishArtifactVersion.pom") - override val metaDataFile: File + override val metaDataPath: Path get() = moduleDir.resolve(MAVEN_METADATA_FILE) - val rootMetaDataFile: File + val rootMetaDataPath: Path get() = moduleDir.resolveSibling(MAVEN_METADATA_FILE) - fun artifactFile(options: Map): File { + fun artifactPath(options: Map): Path { val artifact = toArtifact(options) var fileName = "$artifactId-$publishArtifactVersion.${artifact["type"]}" if (artifact["classifier"] != null) { @@ -65,17 +70,17 @@ abstract class AbstractMavenModule( } override fun publishPom(): MavenModule = apply { - moduleDir.mkdirs() - val rootMavenMetaData = rootMetaDataFile + moduleDir.createDirectories() + val rootMavenMetaData = rootMetaDataPath updateRootMavenMetaData(rootMavenMetaData) if (isPublishesMetaDataFile) { - publish(metaDataFile) { outputStream -> + publish(metaDataPath) { outputStream -> MetadataXpp3Writer().write(outputStream, getMetaData(emptyList())) } } - publish(pomFile) { outputStream -> + publish(pomPath) { outputStream -> val pomPackaging = packaging ?: type val model = Model().also { it.modelVersion = "4.0.0" @@ -114,15 +119,15 @@ abstract class AbstractMavenModule( publishArtifact(emptyMap()) } - open fun publishArtifact(artifact: Map): File { - val artifactFile = artifactFile(artifact) + open fun publishArtifact(artifact: Map): Path { + val artifactPath = artifactPath(artifact) if (type == "pom") { - return artifactFile + return artifactPath } - publish(artifactFile) { outputStream -> - outputStream.write("${artifactFile.name} : $artifactContent".toByteArray()) + publish(artifactPath) { outputStream -> + outputStream.write("${artifactPath.name} : $artifactContent".toByteArray()) } - return artifactFile + return artifactPath } protected fun toArtifact(options: Map): Map { @@ -144,7 +149,7 @@ abstract class AbstractMavenModule( protected val publishTimestamp: Date get() = Date(updateFormat.parse("20100101120000").time + publishCount * 1000) - private fun updateRootMavenMetaData(rootMavenMetaData: File) { + private fun updateRootMavenMetaData(rootMavenMetaData: Path) { val allVersions = if (rootMavenMetaData.exists()) { MetadataXpp3Reader().read(rootMavenMetaData.reader()).versioning.versions } else { @@ -163,8 +168,8 @@ abstract class AbstractMavenModule( private val uniqueSnapshotVersion: String get() { require(isUniqueSnapshots && version.endsWith("-SNAPSHOT")) - return if (metaDataFile.isFile) { - val metaData = MetadataXpp3Reader().read(metaDataFile.reader()) + return if (metaDataPath.isRegularFile()) { + val metaData = MetadataXpp3Reader().read(metaDataPath.reader()) val timestamp = metaData.versioning.snapshot.timestamp val build = metaData.versioning.snapshot.buildNumber "$timestamp-$build" diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt index ca309a01d..bd75b8941 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt @@ -1,12 +1,12 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven -import java.io.File +import java.nio.file.Path import org.apache.maven.artifact.repository.metadata.Metadata import org.apache.maven.artifact.repository.metadata.Snapshot import org.apache.maven.artifact.repository.metadata.Versioning open class MavenFileModule( - moduleDir: File, + moduleDir: Path, groupId: String, artifactId: String, version: String, @@ -30,9 +30,9 @@ open class MavenFileModule( } } - override fun postPublish(file: File) { - sha1File(file) - md5File(file) + override fun postPublish(path: Path) { + writeSha1Path(path) + writeMd5Path(path) } override val isPublishesMetaDataFile: Boolean diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt index 0e3dedc12..21b306454 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt @@ -1,14 +1,14 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven -import java.io.File import java.net.URI +import java.nio.file.Path /** * A fixture for dealing with file Maven repositories. */ -open class MavenFileRepository(val rootDir: File) : MavenRepository { +open class MavenFileRepository(val rootDir: Path) : MavenRepository { - override val uri: URI = rootDir.toURI() + override val uri: URI = rootDir.toUri() override fun module(groupId: String, artifactId: String): MavenFileModule { return module(groupId = groupId, artifactId = artifactId, version = "0.0.0") diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.kt index b77d25886..e484f2a96 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenModule.kt @@ -1,6 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo.maven -import java.io.File +import java.nio.file.Path interface MavenModule { /** @@ -16,7 +16,7 @@ interface MavenModule { fun dependsOn(groupId: String, artifactId: String, version: String): MavenModule - val pomFile: File + val pomPath: Path - val metaDataFile: File + val metaDataPath: Path } From 675403f325b254e846bc844781ad12f98bf5af38 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 12:51:19 +0800 Subject: [PATCH 22/28] Simplify publish and postPublish --- .../gradle/plugins/shadow/util/repo/AbstractModule.kt | 9 +++++---- .../plugins/shadow/util/repo/maven/MavenFileModule.kt | 5 ----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt index 8249bb6db..827964aa4 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/AbstractModule.kt @@ -5,7 +5,6 @@ import java.io.UncheckedIOException import java.math.BigInteger import java.nio.file.Path import kotlin.io.path.absolutePathString -import kotlin.io.path.deleteIfExists import kotlin.io.path.exists import kotlin.io.path.moveTo import kotlin.io.path.name @@ -16,7 +15,7 @@ import okio.ByteString.Companion.toByteString abstract class AbstractModule { - protected abstract fun postPublish(path: Path) + protected open fun postPublish(path: Path) = Unit protected fun publish(path: Path, action: (OutputStream) -> Unit) { val hashBefore = if (path.exists()) getHash(path, "sha1") else null @@ -29,9 +28,11 @@ abstract class AbstractModule { return } - check(!path.deleteIfExists()) - tempPath.moveTo(path) + tempPath.moveTo(path, true) check(path.exists()) + writeSha1Path(path) + writeMd5Path(path) + postPublish(path) } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt index bd75b8941..0807d4021 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileModule.kt @@ -30,11 +30,6 @@ open class MavenFileModule( } } - override fun postPublish(path: Path) { - writeSha1Path(path) - writeMd5Path(path) - } - override val isPublishesMetaDataFile: Boolean get() = isUniqueSnapshots && version.endsWith("-SNAPSHOT") } From 6877f81a44bb0f9461bec7daacce3d25b614a4cf Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 12:54:10 +0800 Subject: [PATCH 23/28] Remove FileExtensions for Groovy --- .../plugins/shadow/util/FileExtensions.groovy | 26 ------------------- ...rg.codehaus.groovy.runtime.ExtensionModule | 3 --- 2 files changed, 29 deletions(-) delete mode 100644 src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/FileExtensions.groovy delete mode 100644 src/funcTest/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/FileExtensions.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/FileExtensions.groovy deleted file mode 100644 index 936c0bbf9..000000000 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/util/FileExtensions.groovy +++ /dev/null @@ -1,26 +0,0 @@ -package com.github.jengelman.gradle.plugins.shadow.util - -/** - * TODO: this is used as extensions for Groovy, could be replaced after migrated to Kotlin. - * Registered in resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule. - */ -final class FileExtensions { - static final File resolve(File file, String relativePath) { - try { - return new File(file, relativePath) - } catch (RuntimeException e) { - throw new RuntimeException(String.format("Could not locate file '%s' relative to '%s'.", Arrays.toString(relativePath), file), e) - } - } - - static final File createDir(File file) { - if (file.mkdirs()) { - return file - } - if (file.isDirectory()) { - return file - } - throw new AssertionError("Problems creating dir: " + file - + ". Diagnostics: exists=" + file.exists() + ", isFile=" + file.isFile() + ", isDirectory=" + file.isDirectory()) - } -} diff --git a/src/funcTest/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule b/src/funcTest/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule deleted file mode 100644 index bb7d5044f..000000000 --- a/src/funcTest/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule +++ /dev/null @@ -1,3 +0,0 @@ -moduleName = groovy-extensions -moduleVersion = ${moduleVersion} -extensionClasses =com.github.jengelman.gradle.plugins.shadow.util.FileExtensions From 7b1314f0165264ad3bf9bd00089cc68afced9d50 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 13:06:33 +0800 Subject: [PATCH 24/28] Seems we don't need escapedPath --- .../shadow/BasePluginSpecification.groovy | 4 -- .../plugins/shadow/ShadowPluginSpec.groovy | 2 +- .../plugins/shadow/TransformerSpec.groovy | 46 +++++++++---------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy index 79f4c86ee..637589378 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy @@ -186,8 +186,4 @@ abstract class BasePluginSpecification extends Specification { } return new File(gradleUserHome, "testkit") } - - protected static String escapedPath(Path path) { - path.toString().replaceAll('\\\\', '\\\\\\\\') - } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index 33d49dcc9..a0a620132 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -71,7 +71,7 @@ class ShadowPluginSpec extends BasePluginSpecification { buildFile << """ dependencies { implementation 'junit:junit:3.8.2' - implementation files('${escapedPath(one)}') + implementation files('$one') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index 1e3686980..899bdfbcb 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -29,8 +29,8 @@ class TransformerSpec extends BasePluginSpecification { buildFile << """ import ${ServiceFileTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(ServiceFileTransformer) { @@ -69,8 +69,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ import ${ServiceFileTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(ServiceFileTransformer) { @@ -109,8 +109,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles { @@ -162,8 +162,8 @@ com.mysql.jdbc.Driver'''.stripIndent()) buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles() @@ -214,8 +214,8 @@ org.mortbay.log.Factory'''.stripIndent() buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles('META-INF/foo') @@ -248,7 +248,7 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ dependencies { implementation 'shadow:two:1.0' - implementation files('${escapedPath(one)}') + implementation files('$one') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -285,8 +285,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ import ${AppendingTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(AppendingTransformer) { @@ -320,8 +320,8 @@ two # NOTE: No newline terminates this line/file buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { append('test.properties') @@ -449,8 +449,8 @@ two # NOTE: No newline terminates this line/file import ${XmlAppendingTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(xml1)}') - from('${escapedPath(xml2)}') + from('$xml1') + from('$xml2') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -617,8 +617,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ import ${GroovyExtensionModuleTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -661,8 +661,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ import ${GroovyExtensionModuleTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -705,8 +705,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('${escapedPath(one)}') - from('${escapedPath(two)}') + from('$one') + from('$two') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeGroovyExtensionModules() From 1aee7bccf40bc05b12c2291abf575ec1343deddd Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 13:10:22 +0800 Subject: [PATCH 25/28] We don't need the overload for module --- .../plugins/shadow/util/repo/maven/MavenFileRepository.kt | 4 ---- .../gradle/plugins/shadow/util/repo/maven/MavenRepository.kt | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt index 21b306454..e1ba0c089 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenFileRepository.kt @@ -10,10 +10,6 @@ open class MavenFileRepository(val rootDir: Path) : MavenRepository { override val uri: URI = rootDir.toUri() - override fun module(groupId: String, artifactId: String): MavenFileModule { - return module(groupId = groupId, artifactId = artifactId, version = "0.0.0") - } - override fun module(groupId: String, artifactId: String, version: String): MavenFileModule { val artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") return MavenFileModule(artifactDir, groupId, artifactId, version) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.kt index a128e07f5..95ca83c86 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/MavenRepository.kt @@ -8,7 +8,5 @@ import java.net.URI interface MavenRepository { val uri: URI - fun module(groupId: String, artifactId: String): MavenModule - fun module(groupId: String, artifactId: String, version: String): MavenModule } From f9dffa76a9da6ddef18bd1dff54919219fbeddd5 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 13:14:50 +0800 Subject: [PATCH 26/28] Simplify AppendableMavenFileModule --- .../plugins/shadow/util/AppendableMavenFileModule.kt | 7 +------ .../plugins/shadow/util/AppendableMavenFileRepository.kt | 3 +-- .../plugins/shadow/util/repo/maven/AbstractMavenModule.kt | 8 ++++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt index 2d4384de2..86f6cd30a 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt @@ -4,12 +4,7 @@ import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileModul import java.nio.file.Path import kotlin.io.path.inputStream -class AppendableMavenFileModule( - moduleDir: Path, - groupId: String, - artifactId: String, - version: String, -) : MavenFileModule(moduleDir, groupId, artifactId, version) { +class AppendableMavenFileModule(module: MavenFileModule) : MavenFileModule(module.moduleDir, module.groupId, module.artifactId, module.version) { private val contents = mutableMapOf>().withDefault { mutableMapOf() } private val paths = mutableMapOf() diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt index e6e980eff..e2ce40630 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileRepository.kt @@ -6,7 +6,6 @@ import java.nio.file.Path class AppendableMavenFileRepository(rootDir: Path) : MavenFileRepository(rootDir) { override fun module(groupId: String, artifactId: String, version: String): AppendableMavenFileModule { - val artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version") - return AppendableMavenFileModule(artifactDir, groupId, artifactId, version) + return AppendableMavenFileModule(super.module(groupId, artifactId, version)) } } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt index 5b05be47c..8d4ada5d6 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/repo/maven/AbstractMavenModule.kt @@ -19,10 +19,10 @@ import org.apache.maven.model.Model import org.apache.maven.model.io.xpp3.MavenXpp3Writer abstract class AbstractMavenModule( - protected val moduleDir: Path, - protected val groupId: String, - protected val artifactId: String, - protected val version: String, + val moduleDir: Path, + val groupId: String, + val artifactId: String, + val version: String, ) : AbstractModule(), MavenModule { From 4690e5d5824684701e29e309c5e66574c09b6022 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 13:16:26 +0800 Subject: [PATCH 27/28] Revert "Seems we don't need escapedPath" This reverts commit 7b1314f0165264ad3bf9bd00089cc68afced9d50. --- .../shadow/BasePluginSpecification.groovy | 4 ++ .../plugins/shadow/ShadowPluginSpec.groovy | 2 +- .../plugins/shadow/TransformerSpec.groovy | 46 +++++++++---------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy index 637589378..f3393d6ec 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy @@ -186,4 +186,8 @@ abstract class BasePluginSpecification extends Specification { } return new File(gradleUserHome, "testkit") } + + protected static String escapedPath(Path path) { + return path.toString().replaceAll('\\\\', '\\\\\\\\') + } } diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index a0a620132..33d49dcc9 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -71,7 +71,7 @@ class ShadowPluginSpec extends BasePluginSpecification { buildFile << """ dependencies { implementation 'junit:junit:3.8.2' - implementation files('$one') + implementation files('${escapedPath(one)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { diff --git a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy index 899bdfbcb..1e3686980 100644 --- a/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy +++ b/src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy @@ -29,8 +29,8 @@ class TransformerSpec extends BasePluginSpecification { buildFile << """ import ${ServiceFileTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(ServiceFileTransformer) { @@ -69,8 +69,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ import ${ServiceFileTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(ServiceFileTransformer) { @@ -109,8 +109,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles { @@ -162,8 +162,8 @@ com.mysql.jdbc.Driver'''.stripIndent()) buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles() @@ -214,8 +214,8 @@ org.mortbay.log.Factory'''.stripIndent() buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeServiceFiles('META-INF/foo') @@ -248,7 +248,7 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ dependencies { implementation 'shadow:two:1.0' - implementation files('$one') + implementation files('${escapedPath(one)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -285,8 +285,8 @@ two # NOTE: No newline terminates this line/file'''.stripIndent() buildFile << """ import ${AppendingTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { transform(AppendingTransformer) { @@ -320,8 +320,8 @@ two # NOTE: No newline terminates this line/file buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { append('test.properties') @@ -449,8 +449,8 @@ two # NOTE: No newline terminates this line/file import ${XmlAppendingTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$xml1') - from('$xml2') + from('${escapedPath(xml1)}') + from('${escapedPath(xml2)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -617,8 +617,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ import ${GroovyExtensionModuleTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -661,8 +661,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ import ${GroovyExtensionModuleTransformer.name} tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { @@ -705,8 +705,8 @@ staticExtensionClasses=com.acme.bar.SomeStaticExtension'''.stripIndent()).write( buildFile << """ tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { - from('$one') - from('$two') + from('${escapedPath(one)}') + from('${escapedPath(two)}') } tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { mergeGroovyExtensionModules() From d25fe18a4d1f6069ff2a2d6b5b40a7fce3b9d0f6 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 25 Dec 2024 13:29:36 +0800 Subject: [PATCH 28/28] Tweak AppendableJar --- .../plugins/shadow/util/AppendableJar.kt | 23 ++++++++----------- .../shadow/util/AppendableMavenFileModule.kt | 2 +- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt index a6f69b91d..41b102d82 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableJar.kt @@ -4,28 +4,25 @@ import java.io.OutputStream import java.nio.file.Path import kotlin.io.path.outputStream -class AppendableJar(initialContents: Map) { - private val contents = initialContents.toMutableMap() - private lateinit var outputPath: Path - - constructor(outputPath: Path) : this(emptyMap()) { - this.outputPath = outputPath - } +class AppendableJar(private val outputPath: Path) { + private val contents = mutableMapOf() fun insert(path: String, content: String): AppendableJar = apply { contents[path] = content } fun write(): Path { - write(outputPath.outputStream()) + write(contents, outputPath.outputStream()) return outputPath } - fun write(outputStream: OutputStream) { - val builder = JarBuilder(outputStream) - contents.forEach { (path, content) -> - builder.withPath(path, content) + companion object { + fun write(contents: Map, outputStream: OutputStream) { + val builder = JarBuilder(outputStream) + contents.forEach { (path, content) -> + builder.withPath(path, content) + } + builder.build() } - builder.build() } } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt index 86f6cd30a..d41213392 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/AppendableMavenFileModule.kt @@ -38,7 +38,7 @@ class AppendableMavenFileModule(module: MavenFileModule) : MavenFileModule(modul } } else { publish(artifactPath) { os -> - AppendableJar(contents[classifier].orEmpty()).write(os) + AppendableJar.write(contents[classifier].orEmpty(), os) } } return artifactPath