mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Move archive generation to Archives.
This commit is contained in:
parent
16cbfb7246
commit
2b179abf49
6 changed files with 78 additions and 63 deletions
|
@ -0,0 +1,70 @@
|
|||
package com.beust.kobalt
|
||||
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.api.annotation.ExportedProjectProperty
|
||||
import com.beust.kobalt.misc.IncludedFile
|
||||
import com.beust.kobalt.misc.JarUtils
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.OutputStream
|
||||
import java.util.*
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
class Archives {
|
||||
companion object {
|
||||
@ExportedProjectProperty(doc = "The name of the jar file", type = "String")
|
||||
const val JAR_NAME = "jarName"
|
||||
|
||||
private val DEFAULT_STREAM_FACTORY = { os : OutputStream -> ZipOutputStream(os) }
|
||||
|
||||
fun generateArchive(project: Project,
|
||||
context: KobaltContext,
|
||||
archiveName: String?,
|
||||
suffix: String,
|
||||
includedFiles: List<IncludedFile>,
|
||||
expandJarFiles : Boolean = false,
|
||||
outputStreamFactory: (OutputStream) -> ZipOutputStream = DEFAULT_STREAM_FACTORY) : File {
|
||||
val fullArchiveName = context.variant.archiveName(project, archiveName, suffix)
|
||||
val archiveDir = File(KFiles.libsDir(project))
|
||||
val result = File(archiveDir.path, fullArchiveName)
|
||||
log(2, "Creating $result")
|
||||
if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) {
|
||||
val outStream = outputStreamFactory(FileOutputStream(result))
|
||||
JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles)
|
||||
log(2, text = "Added ${includedFiles.size} files to $result")
|
||||
outStream.flush()
|
||||
outStream.close()
|
||||
log(1, " Created $result")
|
||||
} else {
|
||||
log(2, " $result is up to date")
|
||||
}
|
||||
|
||||
project.projectProperties.put(JAR_NAME, result.absolutePath)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun isOutdated(directory: String, includedFiles: List<IncludedFile>, output: File): Boolean {
|
||||
if (! output.exists()) return true
|
||||
|
||||
val lastModified = output.lastModified()
|
||||
includedFiles.forEach { root ->
|
||||
val allFiles = root.allFromFiles(directory)
|
||||
allFiles.forEach { relFile ->
|
||||
val file = File(KFiles.joinDir(directory, root.from, relFile.path))
|
||||
if (file.isFile) {
|
||||
if (file.lastModified() > lastModified) {
|
||||
log(2, " TS - Outdated $file and $output "
|
||||
+ Date(file.lastModified()) + " " + Date(output.lastModified()))
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
|
@ -94,7 +94,7 @@ class ApplicationPlugin @Inject constructor(val configActor: ConfigActor<Applica
|
|||
}
|
||||
|
||||
private fun runJarFile(project: Project, config: ApplicationConfig) : TaskResult {
|
||||
val jarName = project.projectProperties.get(PackagingPlugin.JAR_NAME) as String
|
||||
val jarName = project.projectProperties.get(Archives.JAR_NAME) as String
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val packages = project.projectProperties.get(PackagingPlugin.PACKAGES) as List<PackageConfig>
|
||||
val allDeps = arrayListOf(jarName)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.beust.kobalt.plugin.packaging
|
||||
|
||||
import com.beust.kobalt.Archives
|
||||
import com.beust.kobalt.IFileSpec
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
import com.beust.kobalt.api.Project
|
||||
|
@ -93,7 +94,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager)
|
|||
}
|
||||
val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) }
|
||||
|
||||
return PackagingPlugin.generateArchive(project, context, jar.name, ".jar", allFiles,
|
||||
return Archives.generateArchive(project, context, jar.name, ".jar", allFiles,
|
||||
true /* expandJarFiles */, jarFactory)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,11 +12,7 @@ import com.beust.kobalt.maven.DependencyManager
|
|||
import com.beust.kobalt.maven.PomGenerator
|
||||
import com.beust.kobalt.misc.*
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
import java.util.zip.ZipOutputStream
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
@ -33,9 +29,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
|||
@ExportedProjectProperty(doc = "Where the libraries are saved", type = "String")
|
||||
const val LIBS_DIR = "libsDir"
|
||||
|
||||
@ExportedProjectProperty(doc = "The name of the jar file", type = "String")
|
||||
const val JAR_NAME = "jarName"
|
||||
|
||||
@ExportedProjectProperty(doc = "The list of packages produced for this project",
|
||||
type = "List<PackageConfig>")
|
||||
const val PACKAGES = "packages"
|
||||
|
@ -76,57 +69,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
|||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun generateArchive(project: Project,
|
||||
context: KobaltContext,
|
||||
archiveName: String?,
|
||||
suffix: String,
|
||||
includedFiles: List<IncludedFile>,
|
||||
expandJarFiles : Boolean = false,
|
||||
outputStreamFactory: (OutputStream) -> ZipOutputStream = DEFAULT_STREAM_FACTORY) : File {
|
||||
val fullArchiveName = context.variant.archiveName(project, archiveName, suffix)
|
||||
val archiveDir = File(libsDir(project))
|
||||
val result = File(archiveDir.path, fullArchiveName)
|
||||
log(2, "Creating $result")
|
||||
if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) {
|
||||
val outStream = outputStreamFactory(FileOutputStream(result))
|
||||
JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles)
|
||||
log(2, text = "Added ${includedFiles.size} files to $result")
|
||||
outStream.flush()
|
||||
outStream.close()
|
||||
log(1, " Created $result")
|
||||
} else {
|
||||
log(2, " $result is up to date")
|
||||
}
|
||||
|
||||
project.projectProperties.put(JAR_NAME, result.absolutePath)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private val DEFAULT_STREAM_FACTORY = { os : OutputStream -> ZipOutputStream(os) }
|
||||
|
||||
private fun isOutdated(directory: String, includedFiles: List<IncludedFile>, output: File): Boolean {
|
||||
if (! output.exists()) return true
|
||||
|
||||
val lastModified = output.lastModified()
|
||||
includedFiles.forEach { root ->
|
||||
val allFiles = root.allFromFiles(directory)
|
||||
allFiles.forEach { relFile ->
|
||||
val file = File(KFiles.joinDir(directory, root.from, relFile.path))
|
||||
if (file.isFile) {
|
||||
if (file.lastModified() > lastModified) {
|
||||
log(2, " TS - Outdated $file and $output "
|
||||
+ Date(file.lastModified()) + " " + Date(output.lastModified()))
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun libsDir(project: Project) = KFiles.makeDir(KFiles.buildDir(project).path, "libs").path
|
||||
}
|
||||
|
||||
override val name = PLUGIN_NAME
|
||||
|
@ -135,7 +77,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
|||
|
||||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
project.projectProperties.put(LIBS_DIR, libsDir(project))
|
||||
project.projectProperties.put(LIBS_DIR, KFiles.libsDir(project))
|
||||
taskContributor.addVariantTasks(this, project, context, "assemble", runAfter = listOf("compile"),
|
||||
runTask = { doTaskAssemble(project) })
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.beust.kobalt.plugin.packaging
|
||||
|
||||
import com.beust.kobalt.Archives
|
||||
import com.beust.kobalt.IFileSpec
|
||||
import com.beust.kobalt.api.IClasspathDependency
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
|
@ -86,7 +87,7 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager)
|
|||
|
||||
val allFiles = findIncludedFiles(project, context, war)
|
||||
val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) }
|
||||
return PackagingPlugin.generateArchive(project, context, war.name, ".war", allFiles,
|
||||
return Archives.generateArchive(project, context, war.name, ".war", allFiles,
|
||||
false /* don't expand jar files */, jarFactory)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.beust.kobalt.plugin.packaging
|
||||
|
||||
import com.beust.kobalt.Archives
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.maven.DependencyManager
|
||||
|
@ -8,6 +9,6 @@ import com.google.inject.Inject
|
|||
class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){
|
||||
fun generateZip(project: Project, context: KobaltContext, zip: Zip) {
|
||||
val allFiles = PackagingPlugin.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
|
||||
PackagingPlugin.generateArchive(project, context, zip.name, ".zip", allFiles)
|
||||
Archives.generateArchive(project, context, zip.name, ".zip", allFiles)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue