diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveFileFinder.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt similarity index 76% rename from modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveFileFinder.kt rename to modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt index 92eae8ac..dc3fda2d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveFileFinder.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ArchiveGenerator.kt @@ -7,12 +7,16 @@ import com.beust.kobalt.misc.IncludedFile import com.beust.kobalt.misc.KFiles import java.io.File -interface ArchiveFileFinder { +interface ArchiveGenerator { fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip) : List - fun fullArchiveName(project: Project, context: KobaltContext, archiveName: String?, suffix: String) : File { + val suffix: String + fun generateArchive(project: Project, context: KobaltContext, zip: Zip, files: List) : File + + fun fullArchiveName(project: Project, context: KobaltContext, archiveName: String?) : File { val fullArchiveName = context.variant.archiveName(project, archiveName, suffix) val archiveDir = File(KFiles.libsDir(project)) val result = File(archiveDir.path, fullArchiveName) return result } + } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt index 0c6a97fd..ddddaebd 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt @@ -3,7 +3,6 @@ package com.beust.kobalt import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.archive.Archives -import com.beust.kobalt.archive.Jar import com.beust.kobalt.archive.Zip import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.aether.Scope @@ -16,7 +15,7 @@ import java.nio.file.Paths import java.util.jar.JarOutputStream import java.util.jar.Manifest -class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) : ArchiveFileFinder { +class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) : ArchiveGenerator { companion object { fun findIncludedFiles(directory: String, files: List, excludes: List, throwOnError: Boolean = true) @@ -54,7 +53,9 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) } } - override fun findIncludedFiles(project: Project, context: KobaltContext, jar: Zip) : List { + override val suffix = ".jar" + + override fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip) : List { // // Add all the applicable files for the current project // @@ -62,7 +63,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) val result = arrayListOf() val classesDir = KFiles.makeDir(buildDir.path, "classes") - if (jar.includedFiles.isEmpty()) { + if (zip.includedFiles.isEmpty()) { // If no includes were specified, assume the user wants a simple jar file made of the // classes of the project, so we specify a From("build/classes/"), To("") and // a list of files containing everything under it @@ -72,7 +73,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) // Class files val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) } val filesNotExcluded : List = files.filter { - ! KFiles.Companion.isExcluded(KFiles.joinDir(project.directory, it.path), jar.excludes) + ! KFiles.Companion.isExcluded(KFiles.joinDir(project.directory, it.path), zip.excludes) } val fileSpecs = arrayListOf() filesNotExcluded.forEach { @@ -88,14 +89,14 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) // // The user specified an include, just use it verbatim // - val includedFiles = findIncludedFiles(project.directory, jar.includedFiles, jar.excludes, false) + val includedFiles = findIncludedFiles(project.directory, zip.includedFiles, zip.excludes, false) result.addAll(includedFiles) } // // If fatJar is true, add all the transitive dependencies as well: compile, runtime and dependent projects // - if (jar.fatJar) { + if (zip.fatJar) { val seen = hashSetOf() @Suppress("UNCHECKED_CAST") val allDependencies = project.compileDependencies + project.compileRuntimeDependencies + @@ -110,7 +111,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) }.forEach { file : File -> if (! seen.contains(file.path)) { seen.add(file.path) - if (! KFiles.Companion.isExcluded(file, jar.excludes)) { + if (! KFiles.Companion.isExcluded(file, zip.excludes)) { result.add(IncludedFile(specs = arrayListOf(IFileSpec.FileSpec(file.absolutePath)), expandJarFiles = true)) } @@ -121,19 +122,18 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) return result } - fun generateJar(project: Project, context: KobaltContext, jar: Jar) : File { - val includedFiles = findIncludedFiles(project, context, jar) - + override fun generateArchive(project: Project, context: KobaltContext, zip: Zip, + includedFiles: List) : File { // // Generate the manifest // If manifest attributes were specified in the build file, use those to generateAndSave the manifest. Otherwise, // try to find a META-INF/MANIFEST.MF and use that one if we find any. Otherwise, use the default manifest. // val manifest = - if (jar.attributes.size > 1) { - context.logger.log(project.name, 2, "Creating MANIFEST.MF from " + jar.attributes.size + " attributes") + if (zip.attributes.size > 1) { + context.logger.log(project.name, 2, "Creating MANIFEST.MF from " + zip.attributes.size + " attributes") Manifest().apply { - jar.attributes.forEach { attribute -> + zip.attributes.forEach { attribute -> mainAttributes.putValue(attribute.first, attribute.second) } } @@ -157,7 +157,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } - return Archives.generateArchive(project, context, jar.name, ".jar", includedFiles, + return Archives.generateArchive(project, context, zip.name, ".jar", includedFiles, true /* expandJarFiles */, jarFactory) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt index 80a7bd1a..d5086cbd 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt @@ -18,7 +18,7 @@ open class Jar(override val project: Project, // Need to specify the version or attributes will just be dropped @Directive - val attributes = arrayListOf(Pair("Manifest-Version", "1.0")) + override val attributes = arrayListOf(Pair("Manifest-Version", "1.0")) override fun addAttribute(k: String, v: String) { attributes.add(Pair(k, v)) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt index 22b5809d..296eb784 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt @@ -9,7 +9,7 @@ import com.beust.kobalt.misc.IncludedFile import com.beust.kobalt.misc.To open class Zip(open val project: Project, open var name: String = Archives.defaultArchiveName(project) + ".zip", - open var fatJar: Boolean = false) { + open var fatJar: Boolean = false): AttributeHolder { val excludes = arrayListOf() @Directive @@ -49,6 +49,13 @@ open class Zip(open val project: Project, open var name: String = Archives.defau */ val includedFiles = arrayListOf() + @Directive + open val attributes = arrayListOf(Pair("Manifest-Version", "1.0")) + + override fun addAttribute(k: String, v: String) { + attributes.add(Pair(k, v)) + } + } 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 4a72131f..ea541efa 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -62,13 +62,14 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana runTask = { taskInstall(project) }) } + val zipToFiles = hashMapOf>() + override fun assemble(project: Project, context: KobaltContext) : IncrementalTaskInfo { val allConfigs = packages.filter { it.project.name == project.name } val benchmark = benchmarkMillis { if (true) { val allIncludedFiles = arrayListOf() - val zipToFiles = hashMapOf>() val outputArchives = arrayListOf() allConfigs.forEach { packageConfig -> listOf(packageConfig.jars, packageConfig.wars, packageConfig.zips).forEach { archives -> @@ -76,7 +77,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana val files = jarGenerator.findIncludedFiles(packageConfig.project, context, it) val suffixIndex = it.name.lastIndexOf(".") val suffix = it.name.substring(suffixIndex) - val outputFile = jarGenerator.fullArchiveName(project, context, it.name, suffix) + val outputFile = jarGenerator.fullArchiveName(project, context, it.name) outputArchives.add(outputFile) allIncludedFiles.addAll(files) zipToFiles[it.name] = files @@ -111,10 +112,27 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana try { project.projectProperties.put(Archives.JAR_NAME, context.variant.archiveName(project, null, ".jar")) + + fun findFiles(ff: ArchiveGenerator, zip: Zip) : List { + val archiveName = ff.fullArchiveName(project, context, zip.name).name + return zipToFiles[archiveName]!! + } + allConfigs.forEach { packageConfig -> - packageConfig.jars.forEach { jarGenerator.generateJar(packageConfig.project, context, it) } - packageConfig.wars.forEach { warGenerator.generateWar(packageConfig.project, context, it) } - packageConfig.zips.forEach { zipGenerator.generateZip(packageConfig.project, context, it) } + val pairs = listOf( + Pair(packageConfig.jars, jarGenerator), + Pair(packageConfig.wars, warGenerator), + Pair(packageConfig.zips, zipGenerator) + ) + + pairs.forEach { pair -> + val zips = pair.first + val generator = pair.second + zips.forEach { + generator.generateArchive(packageConfig.project, context, it, + findFiles(generator, it)) + } + } if (packageConfig.generatePom) { pomFactory.create(project).generateAndSave() } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt index d03588ca..295224c7 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt @@ -1,13 +1,12 @@ package com.beust.kobalt.plugin.packaging -import com.beust.kobalt.ArchiveFileFinder +import com.beust.kobalt.ArchiveGenerator import com.beust.kobalt.IFileSpec import com.beust.kobalt.JarGenerator import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.archive.Archives -import com.beust.kobalt.archive.War import com.beust.kobalt.archive.Zip import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.maven.DependencyManager @@ -22,7 +21,7 @@ import java.nio.file.Paths import java.util.jar.JarOutputStream class WarGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger) - : ArchiveFileFinder { + : ArchiveGenerator { companion object { val WEB_INF = "WEB-INF" @@ -30,6 +29,8 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager, val LIB = "$WEB_INF/lib" } + override val suffix = ".war" + override fun findIncludedFiles(project: Project, context: KobaltContext, war: Zip) : List { // // src/main/web app and classes @@ -82,16 +83,16 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager, return result } - fun generateWar(project: Project, context: KobaltContext, war: War) : File { + override fun generateArchive(project: Project, context: KobaltContext, war: Zip, + files: List) : File { val manifest = java.util.jar.Manifest()//FileInputStream(mf)) war.attributes.forEach { attribute -> manifest.mainAttributes.putValue(attribute.first, attribute.second) } - val allFiles = findIncludedFiles(project, context, war) val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } - return Archives.generateArchive(project, context, war.name, ".war", allFiles, + return Archives.generateArchive(project, context, war.name, ".war", files, false /* don't expand jar files */, jarFactory) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt index af1edbc2..e15c61ae 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt @@ -1,6 +1,6 @@ package com.beust.kobalt.plugin.packaging -import com.beust.kobalt.ArchiveFileFinder +import com.beust.kobalt.ArchiveGenerator import com.beust.kobalt.JarGenerator import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project @@ -12,13 +12,14 @@ import com.beust.kobalt.misc.IncludedFile import com.google.inject.Inject class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger) - : ArchiveFileFinder { + : ArchiveGenerator { + + override val suffix = ".zip" + override fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip): List { return JarGenerator.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes) } - fun generateZip(project: Project, context: KobaltContext, zip: Zip) { - val allFiles = findIncludedFiles(project, context, zip) - Archives.generateArchive(project, context, zip.name, ".zip", allFiles) - } + override fun generateArchive(project: Project, context: KobaltContext, zip: Zip, files: List) + = Archives.generateArchive(project, context, zip.name, ".zip", files) }