1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

More refactoring.

This commit is contained in:
Cedric Beust 2017-03-15 13:02:37 -07:00
parent bbadd4904d
commit 7b28290b8b
7 changed files with 67 additions and 36 deletions

View file

@ -7,12 +7,16 @@ import com.beust.kobalt.misc.IncludedFile
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import java.io.File import java.io.File
interface ArchiveFileFinder { interface ArchiveGenerator {
fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip) : List<IncludedFile> fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip) : List<IncludedFile>
fun fullArchiveName(project: Project, context: KobaltContext, archiveName: String?, suffix: String) : File { val suffix: String
fun generateArchive(project: Project, context: KobaltContext, zip: Zip, files: List<IncludedFile>) : File
fun fullArchiveName(project: Project, context: KobaltContext, archiveName: String?) : File {
val fullArchiveName = context.variant.archiveName(project, archiveName, suffix) val fullArchiveName = context.variant.archiveName(project, archiveName, suffix)
val archiveDir = File(KFiles.libsDir(project)) val archiveDir = File(KFiles.libsDir(project))
val result = File(archiveDir.path, fullArchiveName) val result = File(archiveDir.path, fullArchiveName)
return result return result
} }
} }

View file

@ -3,7 +3,6 @@ package com.beust.kobalt
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.archive.Archives import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.Jar
import com.beust.kobalt.archive.Zip import com.beust.kobalt.archive.Zip
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.aether.Scope 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.JarOutputStream
import java.util.jar.Manifest import java.util.jar.Manifest
class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) : ArchiveFileFinder { class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) : ArchiveGenerator {
companion object { companion object {
fun findIncludedFiles(directory: String, files: List<IncludedFile>, excludes: List<Glob>, fun findIncludedFiles(directory: String, files: List<IncludedFile>, excludes: List<Glob>,
throwOnError: Boolean = true) throwOnError: Boolean = true)
@ -54,7 +53,9 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
} }
} }
override fun findIncludedFiles(project: Project, context: KobaltContext, jar: Zip) : List<IncludedFile> { override val suffix = ".jar"
override fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip) : List<IncludedFile> {
// //
// Add all the applicable files for the current project // Add all the applicable files for the current project
// //
@ -62,7 +63,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
val result = arrayListOf<IncludedFile>() val result = arrayListOf<IncludedFile>()
val classesDir = KFiles.makeDir(buildDir.path, "classes") 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 // 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 // classes of the project, so we specify a From("build/classes/"), To("") and
// a list of files containing everything under it // a list of files containing everything under it
@ -72,7 +73,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
// Class files // Class files
val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) } val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) }
val filesNotExcluded : List<File> = files.filter { val filesNotExcluded : List<File> = 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<IFileSpec>() val fileSpecs = arrayListOf<IFileSpec>()
filesNotExcluded.forEach { filesNotExcluded.forEach {
@ -88,14 +89,14 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
// //
// The user specified an include, just use it verbatim // 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) result.addAll(includedFiles)
} }
// //
// If fatJar is true, add all the transitive dependencies as well: compile, runtime and dependent projects // 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<String>() val seen = hashSetOf<String>()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val allDependencies = project.compileDependencies + project.compileRuntimeDependencies + val allDependencies = project.compileDependencies + project.compileRuntimeDependencies +
@ -110,7 +111,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
}.forEach { file : File -> }.forEach { file : File ->
if (! seen.contains(file.path)) { if (! seen.contains(file.path)) {
seen.add(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)), result.add(IncludedFile(specs = arrayListOf(IFileSpec.FileSpec(file.absolutePath)),
expandJarFiles = true)) expandJarFiles = true))
} }
@ -121,19 +122,18 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
return result return result
} }
fun generateJar(project: Project, context: KobaltContext, jar: Jar) : File { override fun generateArchive(project: Project, context: KobaltContext, zip: Zip,
val includedFiles = findIncludedFiles(project, context, jar) includedFiles: List<IncludedFile>) : File {
// //
// Generate the manifest // Generate the manifest
// If manifest attributes were specified in the build file, use those to generateAndSave the manifest. Otherwise, // 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. // try to find a META-INF/MANIFEST.MF and use that one if we find any. Otherwise, use the default manifest.
// //
val manifest = val manifest =
if (jar.attributes.size > 1) { if (zip.attributes.size > 1) {
context.logger.log(project.name, 2, "Creating MANIFEST.MF from " + jar.attributes.size + " attributes") context.logger.log(project.name, 2, "Creating MANIFEST.MF from " + zip.attributes.size + " attributes")
Manifest().apply { Manifest().apply {
jar.attributes.forEach { attribute -> zip.attributes.forEach { attribute ->
mainAttributes.putValue(attribute.first, attribute.second) mainAttributes.putValue(attribute.first, attribute.second)
} }
} }
@ -157,7 +157,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } 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) true /* expandJarFiles */, jarFactory)
} }

View file

@ -18,7 +18,7 @@ open class Jar(override val project: Project,
// Need to specify the version or attributes will just be dropped // Need to specify the version or attributes will just be dropped
@Directive @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) { override fun addAttribute(k: String, v: String) {
attributes.add(Pair(k, v)) attributes.add(Pair(k, v))

View file

@ -9,7 +9,7 @@ import com.beust.kobalt.misc.IncludedFile
import com.beust.kobalt.misc.To import com.beust.kobalt.misc.To
open class Zip(open val project: Project, open var name: String = Archives.defaultArchiveName(project) + ".zip", 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<Glob>() val excludes = arrayListOf<Glob>()
@Directive @Directive
@ -49,6 +49,13 @@ open class Zip(open val project: Project, open var name: String = Archives.defau
*/ */
val includedFiles = arrayListOf<IncludedFile>() val includedFiles = arrayListOf<IncludedFile>()
@Directive
open val attributes = arrayListOf(Pair("Manifest-Version", "1.0"))
override fun addAttribute(k: String, v: String) {
attributes.add(Pair(k, v))
}
} }

View file

@ -62,13 +62,14 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
runTask = { taskInstall(project) }) runTask = { taskInstall(project) })
} }
val zipToFiles = hashMapOf<String, List<IncludedFile>>()
override fun assemble(project: Project, context: KobaltContext) : IncrementalTaskInfo { override fun assemble(project: Project, context: KobaltContext) : IncrementalTaskInfo {
val allConfigs = packages.filter { it.project.name == project.name } val allConfigs = packages.filter { it.project.name == project.name }
val benchmark = benchmarkMillis { val benchmark = benchmarkMillis {
if (true) { if (true) {
val allIncludedFiles = arrayListOf<IncludedFile>() val allIncludedFiles = arrayListOf<IncludedFile>()
val zipToFiles = hashMapOf<String, List<IncludedFile>>()
val outputArchives = arrayListOf<File>() val outputArchives = arrayListOf<File>()
allConfigs.forEach { packageConfig -> allConfigs.forEach { packageConfig ->
listOf(packageConfig.jars, packageConfig.wars, packageConfig.zips).forEach { archives -> 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 files = jarGenerator.findIncludedFiles(packageConfig.project, context, it)
val suffixIndex = it.name.lastIndexOf(".") val suffixIndex = it.name.lastIndexOf(".")
val suffix = it.name.substring(suffixIndex) 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) outputArchives.add(outputFile)
allIncludedFiles.addAll(files) allIncludedFiles.addAll(files)
zipToFiles[it.name] = files zipToFiles[it.name] = files
@ -111,10 +112,27 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
try { try {
project.projectProperties.put(Archives.JAR_NAME, project.projectProperties.put(Archives.JAR_NAME,
context.variant.archiveName(project, null, ".jar")) context.variant.archiveName(project, null, ".jar"))
fun findFiles(ff: ArchiveGenerator, zip: Zip) : List<IncludedFile> {
val archiveName = ff.fullArchiveName(project, context, zip.name).name
return zipToFiles[archiveName]!!
}
allConfigs.forEach { packageConfig -> allConfigs.forEach { packageConfig ->
packageConfig.jars.forEach { jarGenerator.generateJar(packageConfig.project, context, it) } val pairs = listOf(
packageConfig.wars.forEach { warGenerator.generateWar(packageConfig.project, context, it) } Pair(packageConfig.jars, jarGenerator),
packageConfig.zips.forEach { zipGenerator.generateZip(packageConfig.project, context, it) } 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) { if (packageConfig.generatePom) {
pomFactory.create(project).generateAndSave() pomFactory.create(project).generateAndSave()
} }

View file

@ -1,13 +1,12 @@
package com.beust.kobalt.plugin.packaging 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.IFileSpec
import com.beust.kobalt.JarGenerator import com.beust.kobalt.JarGenerator
import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.archive.Archives import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.War
import com.beust.kobalt.archive.Zip import com.beust.kobalt.archive.Zip
import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.internal.ParallelLogger
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
@ -22,7 +21,7 @@ import java.nio.file.Paths
import java.util.jar.JarOutputStream import java.util.jar.JarOutputStream
class WarGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger) class WarGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger)
: ArchiveFileFinder { : ArchiveGenerator {
companion object { companion object {
val WEB_INF = "WEB-INF" val WEB_INF = "WEB-INF"
@ -30,6 +29,8 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager,
val LIB = "$WEB_INF/lib" val LIB = "$WEB_INF/lib"
} }
override val suffix = ".war"
override fun findIncludedFiles(project: Project, context: KobaltContext, war: Zip) : List<IncludedFile> { override fun findIncludedFiles(project: Project, context: KobaltContext, war: Zip) : List<IncludedFile> {
// //
// src/main/web app and classes // src/main/web app and classes
@ -82,16 +83,16 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager,
return result return result
} }
fun generateWar(project: Project, context: KobaltContext, war: War) : File { override fun generateArchive(project: Project, context: KobaltContext, war: Zip,
files: List<IncludedFile>) : File {
val manifest = java.util.jar.Manifest()//FileInputStream(mf)) val manifest = java.util.jar.Manifest()//FileInputStream(mf))
war.attributes.forEach { attribute -> war.attributes.forEach { attribute ->
manifest.mainAttributes.putValue(attribute.first, attribute.second) manifest.mainAttributes.putValue(attribute.first, attribute.second)
} }
val allFiles = findIncludedFiles(project, context, war)
val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) } 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) false /* don't expand jar files */, jarFactory)
} }

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.plugin.packaging 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.JarGenerator
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
@ -12,13 +12,14 @@ import com.beust.kobalt.misc.IncludedFile
import com.google.inject.Inject import com.google.inject.Inject
class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager, val kobaltLog: ParallelLogger) 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<IncludedFile> { override fun findIncludedFiles(project: Project, context: KobaltContext, zip: Zip): List<IncludedFile> {
return JarGenerator.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes) return JarGenerator.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
} }
fun generateZip(project: Project, context: KobaltContext, zip: Zip) { override fun generateArchive(project: Project, context: KobaltContext, zip: Zip, files: List<IncludedFile>)
val allFiles = findIncludedFiles(project, context, zip) = Archives.generateArchive(project, context, zip.name, ".zip", files)
Archives.generateArchive(project, context, zip.name, ".zip", allFiles)
}
} }