From ec5a8bf69db9c9e0184b548e8ff97cb0d52528a3 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 1 Dec 2015 14:49:32 -0800 Subject: [PATCH] Exclude certificate files from META-INF. --- .../kotlin/com/beust/kobalt/misc/KFiles.kt | 32 ++++++++++++++++--- .../beust/kobalt/plugin/packaging/JarUtils.kt | 8 ++++- .../plugin/packaging/PackagingPlugin.kt | 26 ++------------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index 9bc2d03f..0cdad327 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.misc +import com.beust.kobalt.IFileSpec import com.beust.kobalt.SystemProperties import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Project @@ -7,10 +8,10 @@ import com.beust.kobalt.homeDir import com.beust.kobalt.internal.build.BuildFile import java.io.File import java.io.IOException -import java.nio.file.Files -import java.nio.file.Path -import java.nio.file.Paths -import java.nio.file.StandardCopyOption +import java.nio.file.* +import kotlin.io.FileAlreadyExistsException +import kotlin.io.FileSystemException +import kotlin.io.NoSuchFileException class KFiles { val kobaltJar : String @@ -102,7 +103,7 @@ class KFiles { allDirs.addAll(directories.map { File(rootDir, it.path) }) } - val seen = hashSetOf() + val seen = hashSetOf() allDirs.forEach { dir -> if (! dir.exists()) { log(2, "Couldn't find directory $dir") @@ -248,6 +249,27 @@ class KFiles { fun makeOutputDir(project: Project) : File = makeDir(project, KFiles.CLASSES_DIR) fun makeOutputTestDir(project: Project) : File = makeDir(project, KFiles.TEST_CLASSES_DIR) + + fun isExcluded(file: File, excludes: List) = isExcluded(file.path, excludes) + + fun isExcluded(file: String, excludes: List) : Boolean { + if (excludes.isEmpty()) { + return false + } else { + val ex = arrayListOf() + excludes.forEach { + ex.add(FileSystems.getDefault().getPathMatcher("glob:${it.spec}")) + } + ex.forEach { + if (it.matches(Paths.get(file))) { + log(2, "Excluding $file") + return true + } + } + } + return false + } + } fun findRecursively(directory: File, function: Function1): List { diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarUtils.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarUtils.kt index c83e2704..07403fd4 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarUtils.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarUtils.kt @@ -1,6 +1,7 @@ package com.beust.kobalt.plugin.packaging import com.beust.kobalt.IFileSpec +import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log import com.google.common.io.CharStreams import java.io.* @@ -28,6 +29,11 @@ public class JarUtils { } } + private val DEFAULT_JAR_EXCLUDES = arrayListOf( + IFileSpec.Glob("META-INF/*.SF"), + IFileSpec.Glob("META-INF/*.DSA"), + IFileSpec.Glob("META-INF/*.RSA")) + public fun addSingleFile(directory: String, file: IncludedFile, outputStream: ZipOutputStream, expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) { file.specs.forEach { spec -> @@ -56,7 +62,7 @@ public class JarUtils { val stream = JarInputStream(FileInputStream(source)) var entry = stream.nextEntry while (entry != null) { - if (!entry.isDirectory) { + if (! entry.isDirectory && ! KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) { val ins = JarFile(source).getInputStream(entry) addEntry(ins, JarEntry(entry), outputStream, onError) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 52b039d9..fea87c77 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -20,8 +20,6 @@ import com.beust.kobalt.plugin.java.JavaPlugin import java.io.File import java.io.FileOutputStream import java.io.OutputStream -import java.nio.file.FileSystems -import java.nio.file.PathMatcher import java.nio.file.Paths import java.util.jar.JarOutputStream import java.util.zip.ZipOutputStream @@ -72,24 +70,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana return TaskResult() } - private fun isExcluded(file: File, excludes: List) : Boolean { - if (excludes.isEmpty()) { - return false - } else { - val ex = arrayListOf() - excludes.forEach { - ex.add(FileSystems.getDefault().getPathMatcher("glob:${it.spec}")) - } - ex.forEach { - if (it.matches(Paths.get(file.name))) { - log(2, "Excluding $file") - return true - } - } - } - return false - } - private fun generateWar(project: Project, war: War) : File { // // src/main/web app and classes @@ -153,7 +133,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana // Class files val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) } - val filesNotExcluded : List = files.filter { ! isExcluded(it, jar.excludes) } + val filesNotExcluded : List = files.filter { ! KFiles.isExcluded(it, jar.excludes) } val fileSpecs = arrayListOf() filesNotExcluded.forEach { fileSpecs.add(FileSpec(it.path.toString().substring(prefixPath.toString().length + 1))) @@ -183,7 +163,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana }.forEach { file : File -> if (! seen.contains(file.name)) { seen.add(file.name) - if (!isExcluded(file, jar.excludes)) { + if (! KFiles.isExcluded(file, jar.excludes)) { allFiles.add(IncludedFile(arrayListOf(FileSpec(file.path)))) } } @@ -221,7 +201,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana } } - if (!isExcluded(file, excludes)) { + if (! KFiles.isExcluded(file, excludes)) { includedSpecs.add(FileSpec(file.path)) } else { log(2, "Not adding ${file.path} to jar file because it's excluded")