mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better exclusion.
This commit is contained in:
parent
3eca68d7bd
commit
d82f9c5dde
3 changed files with 39 additions and 31 deletions
|
@ -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<Path>()
|
||||
val seen = hashSetOf<java.nio.file.Path>()
|
||||
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<IFileSpec.Glob>) = isExcluded(file.path, excludes)
|
||||
|
||||
fun isExcluded(file: String, excludes: List<IFileSpec.Glob>) : Boolean {
|
||||
if (excludes.isEmpty()) {
|
||||
return false
|
||||
} else {
|
||||
val ex = arrayListOf<PathMatcher>()
|
||||
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<String, Boolean>): List<String> {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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<Glob>) : Boolean {
|
||||
if (excludes.isEmpty()) {
|
||||
return false
|
||||
} else {
|
||||
val ex = arrayListOf<PathMatcher>()
|
||||
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<File> = files.filter { ! isExcluded(it, jar.excludes) }
|
||||
val filesNotExcluded : List<File> = files.filter { ! KFiles.isExcluded(it, jar.excludes) }
|
||||
val fileSpecs = arrayListOf<IFileSpec>()
|
||||
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<String>()
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val dependentProjects = project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
|
||||
as List<ProjectDescription>
|
||||
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<IncludedFile>, excludes: List<IFileSpec.Glob>)
|
||||
: List<IncludedFile> {
|
||||
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue