diff --git a/build.gradle.kts b/build.gradle.kts index 874db5c7..91aa28a1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,9 @@ kotlin { } spotless { + kotlin { + ktlint() + } kotlinGradle { ktlint() } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.kt index 6c40caa5..732ce3a6 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.kt @@ -3,7 +3,8 @@ package com.github.jengelman.gradle.plugins.shadow.internal import java.io.BufferedWriter import java.io.IOException import java.io.Writer -import java.util.* +import java.util.Date +import java.util.Properties internal class CleanProperties : Properties() { @Throws(IOException::class) diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest.kt index 82902bd3..c0543e0f 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest.kt @@ -1,6 +1,5 @@ package com.github.jengelman.gradle.plugins.shadow.tasks -import groovy.lang.Closure import org.gradle.api.Action import org.gradle.api.java.archives.Manifest diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt index 4632ca52..27677d79 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt @@ -7,9 +7,18 @@ import com.github.jengelman.gradle.plugins.shadow.internal.ZipCompressor import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext +import java.io.File +import java.io.InputStream +import java.io.OutputStream +import java.util.GregorianCalendar +import java.util.zip.ZipException import org.apache.commons.io.FilenameUtils import org.apache.log4j.LogManager -import org.apache.tools.zip.* +import org.apache.tools.zip.UnixStat +import org.apache.tools.zip.Zip64RequiredException +import org.apache.tools.zip.ZipEntry +import org.apache.tools.zip.ZipFile +import org.apache.tools.zip.ZipOutputStream import org.gradle.api.GradleException import org.gradle.api.UncheckedIOException import org.gradle.api.file.FileCopyDetails @@ -30,380 +39,376 @@ import org.gradle.api.tasks.util.PatternSet import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassWriter import org.objectweb.asm.commons.ClassRemapper -import java.io.File -import java.io.InputStream -import java.io.OutputStream -import java.util.* -import java.util.zip.ZipException class ShadowCopyAction( - private val zipFile: File, - private val compressor: ZipCompressor, - private val documentationRegistry: DocumentationRegistry, - private val encoding: String?, - private val transformers: List, - private val relocators: List, - private val patternSet: PatternSet, - private val stats: ShadowStats, - private val preserveFileTimestamps: Boolean, - private val minimizeJar: Boolean, - private val unusedTracker: UnusedTracker + private val zipFile: File, + private val compressor: ZipCompressor, + private val documentationRegistry: DocumentationRegistry, + private val encoding: String?, + private val transformers: List, + private val relocators: List, + private val patternSet: PatternSet, + private val stats: ShadowStats, + private val preserveFileTimestamps: Boolean, + private val minimizeJar: Boolean, + private val unusedTracker: UnusedTracker, ) : CopyAction { - private val logger = LogManager.getLogger(this::class.java) - - override fun execute(stream: CopyActionProcessingStream): WorkResult { - val unusedClasses: Set = if (minimizeJar) { - stream.process(object : BaseStreamAction() { - override fun visitFile(fileDetails: FileCopyDetails) { - if (isArchive(fileDetails)) { - unusedTracker.addDependency(fileDetails.file) - } - } - }) - unusedTracker.findUnused() - } else { - emptySet() - } + private val logger = LogManager.getLogger(this::class.java) - val zipOutStr = try { - compressor.createArchiveOutputStream(zipFile) as ZipOutputStream - } catch (e: Exception) { - throw GradleException("Could not create ZIP '${zipFile}'", e) + override fun execute(stream: CopyActionProcessingStream): WorkResult { + val unusedClasses: Set = if (minimizeJar) { + stream.process(object : BaseStreamAction() { + override fun visitFile(fileDetails: FileCopyDetails) { + if (isArchive(fileDetails)) { + unusedTracker.addDependency(fileDetails.file) + } } + }) + unusedTracker.findUnused() + } else { + emptySet() + } + + val zipOutStr = try { + compressor.createArchiveOutputStream(zipFile) as ZipOutputStream + } catch (e: Exception) { + throw GradleException("Could not create ZIP '$zipFile'", e) + } + try { + zipOutStr.use { outputStream -> try { - zipOutStr.use { outputStream -> - try { - val action = - StreamAction(outputStream, encoding, transformers, relocators, patternSet, unusedClasses, stats) - stream.process(action) - processTransformers(outputStream) - } catch (e: Exception) { - throw e - } - } - } catch (e: UncheckedIOException) { - if (e.cause is Zip64RequiredException) { - val message = (e.cause as Zip64RequiredException).message - throw Zip64RequiredException( - "$message\n\nTo build this archive, please enable the zip64 extension." + - "\nSee: ${documentationRegistry.getDslRefForProperty(Zip::class.java, "zip64")}" - ) - } + val action = + StreamAction(outputStream, encoding, transformers, relocators, patternSet, unusedClasses, stats) + stream.process(action) + processTransformers(outputStream) + } catch (e: Exception) { + throw e } - return WorkResults.didWork(true) + } + } catch (e: UncheckedIOException) { + if (e.cause is Zip64RequiredException) { + val message = (e.cause as Zip64RequiredException).message + throw Zip64RequiredException( + "$message\n\nTo build this archive, please enable the zip64 extension." + + "\nSee: ${documentationRegistry.getDslRefForProperty(Zip::class.java, "zip64")}", + ) + } } + return WorkResults.didWork(true) + } + + private fun processTransformers(stream: ZipOutputStream) { + transformers.forEach { transformer -> + if (transformer.hasTransformedResource()) { + transformer.modifyOutputStream(stream, preserveFileTimestamps) + } + } + } - private fun processTransformers(stream: ZipOutputStream) { - transformers.forEach { transformer -> - if (transformer.hasTransformedResource()) { - transformer.modifyOutputStream(stream, preserveFileTimestamps) - } - } + private fun getArchiveTimeFor(timestamp: Long): Long { + return if (preserveFileTimestamps) timestamp else CONSTANT_TIME_FOR_ZIP_ENTRIES + } + + private fun setArchiveTimes(zipEntry: ZipEntry): ZipEntry { + if (!preserveFileTimestamps) { + zipEntry.time = CONSTANT_TIME_FOR_ZIP_ENTRIES } + return zipEntry + } - private fun getArchiveTimeFor(timestamp: Long): Long { - return if (preserveFileTimestamps) timestamp else CONSTANT_TIME_FOR_ZIP_ENTRIES + abstract inner class BaseStreamAction : CopyActionProcessingStreamAction { + protected fun isArchive(fileDetails: FileCopyDetails): Boolean { + return fileDetails.relativePath.pathString.endsWith(".jar") } - private fun setArchiveTimes(zipEntry: ZipEntry): ZipEntry { - if (!preserveFileTimestamps) { - zipEntry.time = CONSTANT_TIME_FOR_ZIP_ENTRIES - } - return zipEntry + protected fun isClass(fileDetails: FileCopyDetails): Boolean { + return FilenameUtils.getExtension(fileDetails.path) == "class" } - abstract inner class BaseStreamAction : CopyActionProcessingStreamAction { - protected fun isArchive(fileDetails: FileCopyDetails): Boolean { - return fileDetails.relativePath.pathString.endsWith(".jar") - } + override fun processFile(details: FileCopyDetailsInternal) { + if (details.isDirectory) { + visitDir(details) + } else { + visitFile(details) + } + } - protected fun isClass(fileDetails: FileCopyDetails): Boolean { - return FilenameUtils.getExtension(fileDetails.path) == "class" - } + protected open fun visitDir(dirDetails: FileCopyDetails) = Unit - override fun processFile(details: FileCopyDetailsInternal) { - if (details.isDirectory) { - visitDir(details) - } else { - visitFile(details) - } - } + protected abstract fun visitFile(fileDetails: FileCopyDetails) + } + + private inner class StreamAction( + private val zipOutStr: ZipOutputStream, + encoding: String?, + private val transformers: List, + private val relocators: List, + private val patternSet: PatternSet, + private val unused: Set, + private val stats: ShadowStats, + ) : BaseStreamAction() { - protected open fun visitDir(dirDetails: FileCopyDetails) = Unit + private val remapper = RelocatorRemapper(relocators, stats) + private val visitedFiles = mutableSetOf() - protected abstract fun visitFile(fileDetails: FileCopyDetails) + init { + if (encoding != null) { + this.zipOutStr.setEncoding(encoding) + } } - private inner class StreamAction( - private val zipOutStr: ZipOutputStream, - encoding: String?, - private val transformers: List, - private val relocators: List, - private val patternSet: PatternSet, - private val unused: Set, - private val stats: ShadowStats - ) : BaseStreamAction() { - - private val remapper = RelocatorRemapper(relocators, stats) - private val visitedFiles = mutableSetOf() - - init { - if (encoding != null) { - this.zipOutStr.setEncoding(encoding) - } - } - - private fun recordVisit(path: RelativePath): Boolean { - return visitedFiles.add(path.pathString) - } + private fun recordVisit(path: RelativePath): Boolean { + return visitedFiles.add(path.pathString) + } - override fun visitFile(fileDetails: FileCopyDetails) { - if (!isArchive(fileDetails)) { - try { - val isClass = isClass(fileDetails) - if (!remapper.hasRelocators() || !isClass) { - if (!isTransformable(fileDetails)) { - val mappedPath = remapper.map(fileDetails.relativePath.pathString) - val archiveEntry = ZipEntry(mappedPath) - archiveEntry.time = getArchiveTimeFor(fileDetails.lastModified) - archiveEntry.unixMode = UnixStat.FILE_FLAG or fileDetails.permissions.toUnixNumeric() - zipOutStr.putNextEntry(archiveEntry) - fileDetails.copyTo(zipOutStr) - zipOutStr.closeEntry() - } else { - transform(fileDetails) - } - } else if (isClass && !isUnused(fileDetails.path)) { - remapClass(fileDetails) - } - recordVisit(fileDetails.relativePath) - } catch (e: Exception) { - throw GradleException("Could not add ${fileDetails} to ZIP '${zipFile}'.", e) - } + override fun visitFile(fileDetails: FileCopyDetails) { + if (!isArchive(fileDetails)) { + try { + val isClass = isClass(fileDetails) + if (!remapper.hasRelocators() || !isClass) { + if (!isTransformable(fileDetails)) { + val mappedPath = remapper.map(fileDetails.relativePath.pathString) + val archiveEntry = ZipEntry(mappedPath) + archiveEntry.time = getArchiveTimeFor(fileDetails.lastModified) + archiveEntry.unixMode = UnixStat.FILE_FLAG or fileDetails.permissions.toUnixNumeric() + zipOutStr.putNextEntry(archiveEntry) + fileDetails.copyTo(zipOutStr) + zipOutStr.closeEntry() } else { - processArchive(fileDetails) + transform(fileDetails) } + } else if (isClass && !isUnused(fileDetails.path)) { + remapClass(fileDetails) + } + recordVisit(fileDetails.relativePath) + } catch (e: Exception) { + throw GradleException("Could not add $fileDetails to ZIP '$zipFile'.", e) } + } else { + processArchive(fileDetails) + } + } - private fun processArchive(fileDetails: FileCopyDetails) { - stats.startJar() - ZipFile(fileDetails.file).use { archive -> - val archiveElements = archive.entries.toList().map { ArchiveFileTreeElement(RelativeArchivePath(it)) } - val patternSpec = patternSet.asSpec - val filteredArchiveElements = - archiveElements.filter { patternSpec.isSatisfiedBy(it.asFileTreeElement()) } - filteredArchiveElements.forEach { archiveElement -> - if (archiveElement.relativePath.isFile) { - visitArchiveFile(archiveElement, archive) - } - } - } - stats.finishJar() + private fun processArchive(fileDetails: FileCopyDetails) { + stats.startJar() + ZipFile(fileDetails.file).use { archive -> + val archiveElements = archive.entries.toList().map { ArchiveFileTreeElement(RelativeArchivePath(it)) } + val patternSpec = patternSet.asSpec + val filteredArchiveElements = + archiveElements.filter { patternSpec.isSatisfiedBy(it.asFileTreeElement()) } + filteredArchiveElements.forEach { archiveElement -> + if (archiveElement.relativePath.isFile) { + visitArchiveFile(archiveElement, archive) + } } + } + stats.finishJar() + } - private fun visitArchiveDirectory(archiveDir: RelativeArchivePath) { - if (recordVisit(archiveDir)) { - zipOutStr.putNextEntry(archiveDir.entry) - zipOutStr.closeEntry() - } - } + private fun visitArchiveDirectory(archiveDir: RelativeArchivePath) { + if (recordVisit(archiveDir)) { + zipOutStr.putNextEntry(archiveDir.entry) + zipOutStr.closeEntry() + } + } - private fun visitArchiveFile(archiveFile: ArchiveFileTreeElement, archive: ZipFile) { - val archiveFilePath = archiveFile.relativePath - if (archiveFile.isClassFile || !isTransformable(archiveFile)) { - if (recordVisit(archiveFilePath) && !isUnused(archiveFilePath.entry.name)) { - if (!remapper.hasRelocators() || !archiveFile.isClassFile) { - copyArchiveEntry(archiveFilePath, archive) - } else { - remapClass(archiveFilePath, archive) - } - } - } else { - transform(archiveFile, archive) - } + private fun visitArchiveFile(archiveFile: ArchiveFileTreeElement, archive: ZipFile) { + val archiveFilePath = archiveFile.relativePath + if (archiveFile.isClassFile || !isTransformable(archiveFile)) { + if (recordVisit(archiveFilePath) && !isUnused(archiveFilePath.entry.name)) { + if (!remapper.hasRelocators() || !archiveFile.isClassFile) { + copyArchiveEntry(archiveFilePath, archive) + } else { + remapClass(archiveFilePath, archive) + } } + } else { + transform(archiveFile, archive) + } + } - private fun addParentDirectories(file: RelativeArchivePath?) { - file?.let { - addParentDirectories(it.parent) - if (!it.isFile) { - visitArchiveDirectory(it) - } - } + private fun addParentDirectories(file: RelativeArchivePath?) { + file?.let { + addParentDirectories(it.parent) + if (!it.isFile) { + visitArchiveDirectory(it) } + } + } - private fun isUnused(classPath: String): Boolean { - val className = FilenameUtils.removeExtension(classPath) - .replace('/', '.') - val result = unused.contains(className) - if (result) { - logger.debug("Dropping unused class: $className") - } - return result - } + private fun isUnused(classPath: String): Boolean { + val className = FilenameUtils.removeExtension(classPath) + .replace('/', '.') + val result = unused.contains(className) + if (result) { + logger.debug("Dropping unused class: $className") + } + return result + } - private fun remapClass(file: RelativeArchivePath, archive: ZipFile) { - if (file.isClassFile) { - val zipEntry = setArchiveTimes(ZipEntry(remapper.mapPath(file) + ".class")) - addParentDirectories(RelativeArchivePath(zipEntry)) - remapClass(archive.getInputStream(file.entry), file.pathString, file.entry.time) - } - } + private fun remapClass(file: RelativeArchivePath, archive: ZipFile) { + if (file.isClassFile) { + val zipEntry = setArchiveTimes(ZipEntry(remapper.mapPath(file) + ".class")) + addParentDirectories(RelativeArchivePath(zipEntry)) + remapClass(archive.getInputStream(file.entry), file.pathString, file.entry.time) + } + } - private fun remapClass(fileCopyDetails: FileCopyDetails) { - if (FilenameUtils.getExtension(fileCopyDetails.name) == "class") { - fileCopyDetails.file.inputStream().use { inputStream -> - remapClass( - inputStream, - fileCopyDetails.path, - fileCopyDetails.lastModified - ) - } - } + private fun remapClass(fileCopyDetails: FileCopyDetails) { + if (FilenameUtils.getExtension(fileCopyDetails.name) == "class") { + fileCopyDetails.file.inputStream().use { inputStream -> + remapClass( + inputStream, + fileCopyDetails.path, + fileCopyDetails.lastModified, + ) } + } + } - private fun remapClass(classInputStream: InputStream, path: String, lastModified: Long) { - val cw = ClassWriter(0) - val cv = ClassRemapper(cw, remapper) - - classInputStream.use { - try { - ClassReader(it).accept(cv, ClassReader.EXPAND_FRAMES) - } catch (ise: Throwable) { - throw GradleException("Error in ASM processing class $path", ise) - } - } + private fun remapClass(classInputStream: InputStream, path: String, lastModified: Long) { + val cw = ClassWriter(0) + val cv = ClassRemapper(cw, remapper) - val renamedClass = cw.toByteArray() - val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty() - val mappedName = multiReleasePrefix + remapper.mapPath(path.replace(multiReleasePrefix, "")) - - renamedClass.inputStream().use { bis -> - try { - val archiveEntry = ZipEntry("$mappedName.class") - archiveEntry.time = getArchiveTimeFor(lastModified) - zipOutStr.putNextEntry(archiveEntry) - bis.copyTo(zipOutStr) - zipOutStr.closeEntry() - } catch (ignored: ZipException) { - } - } + classInputStream.use { + try { + ClassReader(it).accept(cv, ClassReader.EXPAND_FRAMES) + } catch (ise: Throwable) { + throw GradleException("Error in ASM processing class $path", ise) } + } - private fun copyArchiveEntry(archiveFile: RelativeArchivePath, archive: ZipFile) { - val mappedPath = remapper.map(archiveFile.entry.name) - val entry = ZipEntry(mappedPath) - entry.time = getArchiveTimeFor(archiveFile.entry.time) - val mappedFile = RelativeArchivePath(entry) - addParentDirectories(mappedFile) - zipOutStr.putNextEntry(mappedFile.entry) - archive.getInputStream(archiveFile.entry).copyTo(zipOutStr) - zipOutStr.closeEntry() - } + val renamedClass = cw.toByteArray() + val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty() + val mappedName = multiReleasePrefix + remapper.mapPath(path.replace(multiReleasePrefix, "")) - override fun visitDir(dirDetails: FileCopyDetails) { - try { - val path = dirDetails.relativePath.pathString + "/" - val archiveEntry = ZipEntry(path) - archiveEntry.time = getArchiveTimeFor(dirDetails.lastModified) - archiveEntry.unixMode = UnixStat.DIR_FLAG or dirDetails.permissions.toUnixNumeric() - zipOutStr.putNextEntry(archiveEntry) - zipOutStr.closeEntry() - recordVisit(dirDetails.relativePath) - } catch (e: Exception) { - throw GradleException("Could not add $dirDetails to ZIP '${zipFile}'.", e) - } + renamedClass.inputStream().use { bis -> + try { + val archiveEntry = ZipEntry("$mappedName.class") + archiveEntry.time = getArchiveTimeFor(lastModified) + zipOutStr.putNextEntry(archiveEntry) + bis.copyTo(zipOutStr) + zipOutStr.closeEntry() + } catch (ignored: ZipException) { } + } + } - private fun transform(element: ArchiveFileTreeElement, archive: ZipFile) { - transformAndClose(element, archive.getInputStream(element.relativePath.entry)) - } + private fun copyArchiveEntry(archiveFile: RelativeArchivePath, archive: ZipFile) { + val mappedPath = remapper.map(archiveFile.entry.name) + val entry = ZipEntry(mappedPath) + entry.time = getArchiveTimeFor(archiveFile.entry.time) + val mappedFile = RelativeArchivePath(entry) + addParentDirectories(mappedFile) + zipOutStr.putNextEntry(mappedFile.entry) + archive.getInputStream(archiveFile.entry).copyTo(zipOutStr) + zipOutStr.closeEntry() + } - private fun transform(details: FileCopyDetails) { - transformAndClose(details, details.file.inputStream()) - } + override fun visitDir(dirDetails: FileCopyDetails) { + try { + val path = dirDetails.relativePath.pathString + "/" + val archiveEntry = ZipEntry(path) + archiveEntry.time = getArchiveTimeFor(dirDetails.lastModified) + archiveEntry.unixMode = UnixStat.DIR_FLAG or dirDetails.permissions.toUnixNumeric() + zipOutStr.putNextEntry(archiveEntry) + zipOutStr.closeEntry() + recordVisit(dirDetails.relativePath) + } catch (e: Exception) { + throw GradleException("Could not add $dirDetails to ZIP '$zipFile'.", e) + } + } - private fun transformAndClose(element: FileTreeElement, inputStream: InputStream) { - inputStream.use { - val mappedPath = remapper.map(element.relativePath.pathString) - transformers.find { it.canTransformResource(element) } - ?.transform( - TransformerContext.builder() - .path(mappedPath) - .inputStream(it) - .relocators(relocators) - .stats(stats) - .build() - ) - } - } + private fun transform(element: ArchiveFileTreeElement, archive: ZipFile) { + transformAndClose(element, archive.getInputStream(element.relativePath.entry)) + } - private fun isTransformable(element: FileTreeElement): Boolean { - return transformers.any { it.canTransformResource(element) } - } + private fun transform(details: FileCopyDetails) { + transformAndClose(details, details.file.inputStream()) } - inner class RelativeArchivePath(val entry: ZipEntry) : RelativePath( - !entry.isDirectory, - *entry.name.split("/").toTypedArray() - ) { + private fun transformAndClose(element: FileTreeElement, inputStream: InputStream) { + inputStream.use { + val mappedPath = remapper.map(element.relativePath.pathString) + transformers.find { it.canTransformResource(element) } + ?.transform( + TransformerContext.builder() + .path(mappedPath) + .inputStream(it) + .relocators(relocators) + .stats(stats) + .build(), + ) + } + } - val isClassFile: Boolean - get() = lastName.endsWith(".class") + private fun isTransformable(element: FileTreeElement): Boolean { + return transformers.any { it.canTransformResource(element) } + } + } - override fun getParent(): RelativeArchivePath { - return if (segments.isEmpty() || segments.size == 1) { - // TODO: the return type must be non-nullable - null!! - } else { - val path = segments.dropLast(1).joinToString("/") + "/" - RelativeArchivePath(setArchiveTimes(ZipEntry(path))) - } - } + inner class RelativeArchivePath(val entry: ZipEntry) : + RelativePath( + !entry.isDirectory, + *entry.name.split("/").toTypedArray(), + ) { + + val isClassFile: Boolean + get() = lastName.endsWith(".class") + + override fun getParent(): RelativeArchivePath { + return if (segments.isEmpty() || segments.size == 1) { + // TODO: the return type must be non-nullable + null!! + } else { + val path = segments.dropLast(1).joinToString("/") + "/" + RelativeArchivePath(setArchiveTimes(ZipEntry(path))) + } } + } - class ArchiveFileTreeElement(private val archivePath: RelativeArchivePath) : FileTreeElement { + class ArchiveFileTreeElement(private val archivePath: RelativeArchivePath) : FileTreeElement { - val isClassFile: Boolean - get() = archivePath.isClassFile + val isClassFile: Boolean + get() = archivePath.isClassFile - override fun getFile(): File = error("Not supported") + override fun getFile(): File = error("Not supported") - override fun isDirectory(): Boolean = archivePath.entry.isDirectory + override fun isDirectory(): Boolean = archivePath.entry.isDirectory - override fun getLastModified(): Long = archivePath.entry.lastModifiedDate.time + override fun getLastModified(): Long = archivePath.entry.lastModifiedDate.time - override fun getSize(): Long = archivePath.entry.size + override fun getSize(): Long = archivePath.entry.size - override fun open(): InputStream = error("Not supported") + override fun open(): InputStream = error("Not supported") - override fun copyTo(outputStream: OutputStream) {} + override fun copyTo(outputStream: OutputStream) {} - override fun copyTo(file: File): Boolean = false + override fun copyTo(file: File): Boolean = false - override fun getName(): String = archivePath.pathString + override fun getName(): String = archivePath.pathString - override fun getPath(): String = archivePath.lastName + override fun getPath(): String = archivePath.lastName - override fun getRelativePath(): RelativeArchivePath = archivePath + override fun getRelativePath(): RelativeArchivePath = archivePath - override fun getMode(): Int = archivePath.entry.unixMode + override fun getMode(): Int = archivePath.entry.unixMode - override fun getPermissions(): FilePermissions = DefaultFilePermissions(mode) + override fun getPermissions(): FilePermissions = DefaultFilePermissions(mode) - fun asFileTreeElement(): FileTreeElement { - // TODO: the param types must be non-nullable - return DefaultFileTreeElement( - null!!, - RelativePath(!isDirectory, *archivePath.segments), - null!!, - null!! - ) - } + fun asFileTreeElement(): FileTreeElement { + // TODO: the param types must be non-nullable + return DefaultFileTreeElement( + null!!, + RelativePath(!isDirectory, *archivePath.segments), + null!!, + null!!, + ) } + } - companion object { - val CONSTANT_TIME_FOR_ZIP_ENTRIES = GregorianCalendar(1980, 1, 1, 0, 0, 0).timeInMillis - } + companion object { + val CONSTANT_TIME_FOR_ZIP_ENTRIES = GregorianCalendar(1980, 1, 1, 0, 0, 0).timeInMillis + } } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt index 07572bae..26cb1511 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt @@ -43,7 +43,9 @@ import org.gradle.api.tasks.util.PatternSet import org.jetbrains.annotations.NotNull @CacheableTask -class ShadowJar : Jar(), ShadowSpec { +class ShadowJar : + Jar(), + ShadowSpec { private val _transformers = mutableListOf() private val _relocators = mutableListOf() @@ -65,7 +67,6 @@ class ShadowJar : Jar(), ShadowSpec { } } - var transformers: List @Nested get() = _transformers set(value) { @@ -126,11 +127,11 @@ class ShadowJar : Jar(), ShadowSpec { val apiJars: FileCollection get() { if (_apiJars == null) { - _apiJars = if (minimizeJar) + _apiJars = if (minimizeJar) { getApiJarsFromProject(project) - else + } else { project.objects.fileCollection() - + } } return _apiJars!! } @@ -229,7 +230,6 @@ class ShadowJar : Jar(), ShadowSpec { return this } - override fun transform(transformer: Transformer): ShadowJar { addTransform(transformer, null) return this diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt index 86aaff91..3645decc 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt @@ -20,8 +20,9 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext -import java.io.* -import java.util.* +import java.io.BufferedInputStream +import java.io.ByteArrayOutputStream +import java.io.IOException import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream import org.codehaus.plexus.util.xml.XmlStreamReader diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt index 1037e0cc..eb32ccd5 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt @@ -21,8 +21,11 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction -import java.io.* -import java.util.* +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.IOException +import java.io.InputStream import org.apache.tools.zip.ZipEntry import org.apache.tools.zip.ZipOutputStream import org.gradle.api.file.FileTreeElement @@ -32,7 +35,8 @@ import org.gradle.api.tasks.util.PatternSet @CacheableTransformer class ServiceFileTransformer private constructor( private val patternSet: PatternSet, -) : Transformer, PatternFilterable by patternSet { +) : Transformer, + PatternFilterable by patternSet { private val serviceEntries = mutableMapOf() constructor() : this( @@ -65,7 +69,7 @@ class ServiceFileTransformer private constructor( } } lines.forEach { line -> - serviceEntries.getOrPut(targetPath) { ServiceStream() }.append(ByteArrayInputStream(line.toByteArray())) + serviceEntries.getOrPut(targetPath) { ServiceStream() }.append(line.toByteArray().inputStream()) } }