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

Move archive classes to kobalt-plugin-api.

This commit is contained in:
Cedric Beust 2016-03-05 00:34:35 +04:00
parent b907880208
commit 9ea91052cf
12 changed files with 179 additions and 143 deletions

View file

@ -1,10 +1,10 @@
package com.beust.kobalt.plugin.packaging package com.beust.kobalt
import com.beust.kobalt.Archives
import com.beust.kobalt.IFileSpec
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.api.ProjectDescription import com.beust.kobalt.api.ProjectDescription
import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.Jar
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.misc.* import com.beust.kobalt.misc.*
@ -15,6 +15,42 @@ import java.nio.file.Paths
import java.util.jar.JarOutputStream import java.util.jar.JarOutputStream
class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) { class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) {
companion object {
fun findIncludedFiles(directory: String, files: List<IncludedFile>, excludes: List<Glob>)
: List<IncludedFile> {
val result = arrayListOf<IncludedFile>()
files.forEach { includedFile ->
val includedSpecs = arrayListOf<IFileSpec>()
includedFile.specs.forEach { spec ->
val fromPath = includedFile.from
if (File(directory, fromPath).exists()) {
spec.toFiles(directory, fromPath).forEach { file ->
val fullFile = File(KFiles.joinDir(directory, fromPath, file.path))
if (! fullFile.exists()) {
throw AssertionError("File should exist: $fullFile")
}
if (!KFiles.isExcluded(fullFile, excludes)) {
val normalized = Paths.get(file.path).normalize().toFile().path
includedSpecs.add(IFileSpec.FileSpec(normalized))
} else {
log(2, "Not adding ${file.path} to jar file because it's excluded")
}
}
} else {
log(2, "Directory $fromPath doesn't exist, not including it in the jar")
}
}
if (includedSpecs.size > 0) {
log(3, "Including specs $includedSpecs")
result.add(IncludedFile(From(includedFile.from), To(includedFile.to), includedSpecs))
}
}
return result
}
}
fun findIncludedFiles(project: Project, context: KobaltContext, jar: Jar) : List<IncludedFile> { fun findIncludedFiles(project: Project, context: KobaltContext, jar: Jar) : List<IncludedFile> {
// //
// Add all the applicable files for the current project // Add all the applicable files for the current project
@ -33,7 +69,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.isExcluded(KFiles.joinDir(project.directory, it.path), jar.excludes) ! KFiles.Companion.isExcluded(KFiles.joinDir(project.directory, it.path), jar.excludes)
} }
val fileSpecs = arrayListOf<IFileSpec>() val fileSpecs = arrayListOf<IFileSpec>()
filesNotExcluded.forEach { filesNotExcluded.forEach {
@ -49,7 +85,7 @@ 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 = PackagingPlugin.findIncludedFiles(project.directory, jar.includedFiles, jar.excludes) val includedFiles = findIncludedFiles(project.directory, jar.includedFiles, jar.excludes)
result.addAll(includedFiles) result.addAll(includedFiles)
} }
@ -71,7 +107,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.isExcluded(file, jar.excludes)) { if (! KFiles.Companion.isExcluded(file, jar.excludes)) {
result.add(IncludedFile(specs = arrayListOf(IFileSpec.FileSpec(file.path)), result.add(IncludedFile(specs = arrayListOf(IFileSpec.FileSpec(file.path)),
expandJarFiles = true)) expandJarFiles = true))
} }

View file

@ -1,5 +1,6 @@
package com.beust.kobalt package com.beust.kobalt.archive
import com.beust.kobalt.Features
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.api.annotation.ExportedProjectProperty import com.beust.kobalt.api.annotation.ExportedProjectProperty
@ -30,7 +31,7 @@ class Archives {
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)
log(2, "Creating $result") log(3, "Creating $result")
if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) { if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) {
val outStream = outputStreamFactory(FileOutputStream(result)) val outStream = outputStreamFactory(FileOutputStream(result))
JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles) JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles)
@ -39,7 +40,7 @@ class Archives {
outStream.close() outStream.close()
log(1, " Created $result") log(1, " Created $result")
} else { } else {
log(2, " $result is up to date") log(3, " $result is up to date")
} }
project.projectProperties.put(JAR_NAME, result.absolutePath) project.projectProperties.put(JAR_NAME, result.absolutePath)
@ -57,7 +58,7 @@ class Archives {
val file = File(KFiles.joinDir(directory, root.from, relFile.path)) val file = File(KFiles.joinDir(directory, root.from, relFile.path))
if (file.isFile) { if (file.isFile) {
if (file.lastModified() > lastModified) { if (file.lastModified() > lastModified) {
log(2, " TS - Outdated $file and $output " log(3, " TS - Outdated $file and $output "
+ Date(file.lastModified()) + " " + Date(output.lastModified())) + Date(file.lastModified()) + " " + Date(output.lastModified()))
return true return true
} }

View file

@ -0,0 +1,6 @@
package com.beust.kobalt.archive
interface AttributeHolder {
fun addAttribute(k: String, v: String)
}

View file

@ -0,0 +1,25 @@
package com.beust.kobalt.archive
import com.beust.kobalt.api.annotation.Directive
/**
* A jar is exactly like a zip with the addition of a manifest and an optional fatJar boolean.
*/
open class Jar(override var name: String? = null, var fatJar: Boolean = false) : Zip(name), AttributeHolder {
@Directive
fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest {
val m = Manifest(this)
m.init(m)
return m
}
// Need to specify the version or attributes will just be dropped
@Directive
val attributes = arrayListOf(Pair("Manifest-Version", "1.0"))
override fun addAttribute(k: String, v: String) {
attributes.add(Pair(k, v))
}
}

View file

@ -0,0 +1,11 @@
package com.beust.kobalt.archive
import com.beust.kobalt.api.annotation.Directive
class Manifest(val jar: AttributeHolder) {
@Directive
fun attributes(k: String, v: String) {
jar.addAttribute(k, v)
}
}

View file

@ -0,0 +1,11 @@
package com.beust.kobalt.archive
import com.beust.kobalt.glob
class War(override var name: String? = null) : Jar(name), AttributeHolder {
init {
include(from("src/main/webapp"),to(""), glob("**"))
include(from("kobaltBuild/classes"), to("WEB-INF/classes"), glob("**"))
}
}

View file

@ -0,0 +1,52 @@
package com.beust.kobalt.archive
import com.beust.kobalt.Glob
import com.beust.kobalt.IFileSpec
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.misc.From
import com.beust.kobalt.misc.IncludedFile
import com.beust.kobalt.misc.To
open class Zip(open var name: String? = null) {
val excludes = arrayListOf<Glob>()
@Directive
fun from(s: String) = From(s)
@Directive
fun to(s: String) = To(s)
@Directive
fun exclude(vararg files: String) {
files.forEach { excludes.add(Glob(it)) }
}
@Directive
fun exclude(vararg specs: Glob) {
specs.forEach { excludes.add(it) }
}
@Directive
fun include(vararg files: String) {
includedFiles.add(IncludedFile(files.map { IFileSpec.FileSpec(it) }))
}
@Directive
fun include(from: From, to: To, vararg specs: String) {
includedFiles.add(IncludedFile(from, to, specs.map { IFileSpec.FileSpec(it) }))
}
@Directive
fun include(from: From, to: To, vararg specs: IFileSpec.GlobSpec) {
includedFiles.add(IncludedFile(from, to, listOf(*specs)))
}
/**
* Prefix path to be removed from the zip file. For example, if you add "build/lib/a.jar" to the zip
* file and the excludePrefix is "build/lib", then "a.jar" will be added at the root of the zip file.
*/
val includedFiles = arrayListOf<IncludedFile>()
}

View file

@ -93,14 +93,14 @@ class IncrementalManager(val fileName: String = IncrementalManager.BUILD_INFO_FI
if (outputChecksum == taskOutputChecksum) { if (outputChecksum == taskOutputChecksum) {
upToDate = true upToDate = true
} else { } else {
logIncremental(2, "Incremental task $taskName output is out of date, running it") logIncremental(3, "Incremental task $taskName output is out of date, running it")
} }
} }
} else { } else {
if (dependsOnDirtyProjects) { if (dependsOnDirtyProjects) {
logIncremental(2, "Project ${project.name} depends on dirty project, running $taskName") logIncremental(3, "Project ${project.name} depends on dirty project, running $taskName")
} else { } else {
logIncremental(2, "Incremental task $taskName input is out of date, running it" logIncremental(3, "Incremental task $taskName input is out of date, running it"
+ " old: $inputChecksum new: ${iit.inputChecksum}") + " old: $inputChecksum new: ${iit.inputChecksum}")
} }
project.projectExtra.isDirty = true project.projectExtra.isDirty = true
@ -109,20 +109,20 @@ class IncrementalManager(val fileName: String = IncrementalManager.BUILD_INFO_FI
if (! upToDate) { if (! upToDate) {
val result = iit.task(project) val result = iit.task(project)
if (result.success) { if (result.success) {
logIncremental(2, "Incremental task $taskName done running, saving checksums") logIncremental(3, "Incremental task $taskName done running, saving checksums")
iit.inputChecksum?.let { iit.inputChecksum?.let {
saveInputChecksum(taskName, it) saveInputChecksum(taskName, it)
logIncremental(2, " input checksum \"$it\" saved") logIncremental(3, " input checksum \"$it\" saved")
} }
// Important to rerun the checksum here since the output of the task might have changed it // Important to rerun the checksum here since the output of the task might have changed it
iit.outputChecksum()?.let { iit.outputChecksum()?.let {
saveOutputChecksum(taskName, it) saveOutputChecksum(taskName, it)
logIncremental(2, " output checksum \"$it\" saved") logIncremental(3, " output checksum \"$it\" saved")
} }
} }
result result
} else { } else {
logIncremental(2, "Incremental task \"$taskName\" is up to date, not running it") logIncremental(3, "Incremental task \"$taskName\" is up to date, not running it")
TaskResult() TaskResult()
} }
} }

View file

@ -4,13 +4,14 @@ import com.beust.kobalt.*
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.Jar
import com.beust.kobalt.internal.ActorUtils import com.beust.kobalt.internal.ActorUtils
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.RunCommand import com.beust.kobalt.misc.RunCommand
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import com.beust.kobalt.plugin.packaging.Jar
import com.beust.kobalt.plugin.packaging.PackageConfig import com.beust.kobalt.plugin.packaging.PackageConfig
import com.beust.kobalt.plugin.packaging.PackagingPlugin import com.beust.kobalt.plugin.packaging.PackagingPlugin
import com.google.inject.Inject import com.google.inject.Inject

View file

@ -1,18 +1,21 @@
package com.beust.kobalt.plugin.packaging package com.beust.kobalt.plugin.packaging
import com.beust.kobalt.* import com.beust.kobalt.JarGenerator
import com.beust.kobalt.IFileSpec.FileSpec import com.beust.kobalt.KobaltException
import com.beust.kobalt.IFileSpec.GlobSpec import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.ExportedProjectProperty import com.beust.kobalt.api.annotation.ExportedProjectProperty
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.archive.*
import com.beust.kobalt.glob
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.PomGenerator import com.beust.kobalt.maven.PomGenerator
import com.beust.kobalt.misc.* import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
import java.io.File import java.io.File
import java.nio.file.Paths
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -35,40 +38,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
const val TASK_ASSEMBLE: String = "assemble" const val TASK_ASSEMBLE: String = "assemble"
const val TASK_INSTALL: String = "install" const val TASK_INSTALL: String = "install"
fun findIncludedFiles(directory: String, files: List<IncludedFile>, excludes: List<Glob>)
: List<IncludedFile> {
val result = arrayListOf<IncludedFile>()
files.forEach { includedFile ->
val includedSpecs = arrayListOf<IFileSpec>()
includedFile.specs.forEach { spec ->
val fromPath = includedFile.from
if (File(directory, fromPath).exists()) {
spec.toFiles(directory, fromPath).forEach { file ->
val fullFile = File(KFiles.joinDir(directory, fromPath, file.path))
if (! fullFile.exists()) {
throw AssertionError("File should exist: $fullFile")
}
if (!KFiles.isExcluded(fullFile, excludes)) {
val normalized = Paths.get(file.path).normalize().toFile().path
includedSpecs.add(FileSpec(normalized))
} else {
log(2, "Not adding ${file.path} to jar file because it's excluded")
}
}
} else {
log(2, "Directory $fromPath doesn't exist, not including it in the jar")
}
}
if (includedSpecs.size > 0) {
log(3, "Including specs $includedSpecs")
result.add(IncludedFile(From(includedFile.from), To(includedFile.to), includedSpecs))
}
}
return result
}
} }
override val name = PLUGIN_NAME override val name = PLUGIN_NAME
@ -222,7 +191,7 @@ class PackageConfig(val project: Project) : AttributeHolder {
class MavenJars(val ah: AttributeHolder, var fatJar: Boolean = false, var manifest: Manifest? = null) : class MavenJars(val ah: AttributeHolder, var fatJar: Boolean = false, var manifest: Manifest? = null) :
AttributeHolder by ah { AttributeHolder by ah {
public fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest { fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest {
val m = Manifest(this) val m = Manifest(this)
m.init(m) m.init(m)
return m return m
@ -230,87 +199,7 @@ class PackageConfig(val project: Project) : AttributeHolder {
} }
} }
open class Zip(open var name: String? = null) {
// internal val includes = arrayListOf<IFileSpec>()
internal val excludes = arrayListOf<Glob>()
@Directive
public fun from(s: String) = From(s)
@Directive
public fun to(s: String) = To(s)
@Directive
public fun exclude(vararg files: String) {
files.forEach { excludes.add(Glob(it)) }
}
@Directive
public fun exclude(vararg specs: Glob) {
specs.forEach { excludes.add(it) }
}
@Directive
public fun include(vararg files: String) {
includedFiles.add(IncludedFile(files.map { FileSpec(it) }))
}
@Directive
public fun include(from: From, to: To, vararg specs: String) {
includedFiles.add(IncludedFile(from, to, specs.map { FileSpec(it) }))
}
@Directive
public fun include(from: From, to: To, vararg specs: GlobSpec) {
includedFiles.add(IncludedFile(from, to, listOf(*specs)))
}
/**
* Prefix path to be removed from the zip file. For example, if you add "build/lib/a.jar" to the zip
* file and the excludePrefix is "build/lib", then "a.jar" will be added at the root of the zip file.
*/
val includedFiles = arrayListOf<IncludedFile>()
}
interface AttributeHolder {
fun addAttribute(k: String, v: String)
}
/**
* A jar is exactly like a zip with the addition of a manifest and an optional fatJar boolean.
*/
open class Jar(override var name: String? = null, var fatJar: Boolean = false) : Zip(name), AttributeHolder {
@Directive
public fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest {
val m = Manifest(this)
m.init(m)
return m
}
// Need to specify the version or attributes will just be dropped
@Directive
val attributes = arrayListOf(Pair("Manifest-Version", "1.0"))
override fun addAttribute(k: String, v: String) {
attributes.add(Pair(k, v))
}
}
class War(override var name: String? = null) : Jar(name), AttributeHolder {
init {
include(from("src/main/webapp"),to(""), glob("**"))
include(from("kobaltBuild/classes"), to("WEB-INF/classes"), glob("**"))
}
}
class Pom { class Pom {
} }
class Manifest(val jar: AttributeHolder) {
@Directive
public fun attributes(k: String, v: String) {
jar.addAttribute(k, v)
}
}

View file

@ -1,7 +1,9 @@
package com.beust.kobalt.plugin.packaging package com.beust.kobalt.plugin.packaging
import com.beust.kobalt.Archives import com.beust.kobalt.archive.Archives
import com.beust.kobalt.IFileSpec import com.beust.kobalt.IFileSpec
import com.beust.kobalt.JarGenerator
import com.beust.kobalt.archive.War
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
@ -73,7 +75,7 @@ class WarGenerator @Inject constructor(val dependencyManager: DependencyManager)
// //
// Finally, all the included/excluded files specified in the war{} directive // Finally, all the included/excluded files specified in the war{} directive
// //
result.addAll(PackagingPlugin.findIncludedFiles(project.directory, war.includedFiles, war.excludes)) result.addAll(JarGenerator.findIncludedFiles(project.directory, war.includedFiles, war.excludes))
return result return result
} }

View file

@ -1,14 +1,16 @@
package com.beust.kobalt.plugin.packaging package com.beust.kobalt.plugin.packaging
import com.beust.kobalt.Archives 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
import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.Zip
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.google.inject.Inject import com.google.inject.Inject
class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){ class ZipGenerator @Inject constructor(val dependencyManager: DependencyManager){
fun generateZip(project: Project, context: KobaltContext, zip: Zip) { fun generateZip(project: Project, context: KobaltContext, zip: Zip) {
val allFiles = PackagingPlugin.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes) val allFiles = JarGenerator.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
Archives.generateArchive(project, context, zip.name, ".zip", allFiles) Archives.generateArchive(project, context, zip.name, ".zip", allFiles)
} }
} }