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 99662089..fea87c77 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -11,7 +11,6 @@ import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.glob import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors @@ -21,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 @@ -73,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 @@ -154,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))) @@ -171,6 +150,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana log(2, "Creating fat jar") val seen = hashSetOf() + @Suppress("UNCHECKED_CAST") val dependentProjects = project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS) as List listOf(dependencyManager.calculateDependencies(project, context, dependentProjects, @@ -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)))) } } @@ -204,7 +184,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana true /* expandJarFiles */, jarFactory) } - private fun buildDir(project: Project) = KFiles.makeDir(project.directory, project.buildDirectory!!) + private fun buildDir(project: Project) = KFiles.makeDir(project.directory, project.buildDirectory) private fun findIncludedFiles(directory: String, files: List, excludes: List) : List { @@ -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")