1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27: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

@ -0,0 +1,137 @@
package com.beust.kobalt
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
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.maven.DependencyManager
import com.beust.kobalt.misc.*
import com.google.inject.Inject
import java.io.File
import java.io.OutputStream
import java.nio.file.Paths
import java.util.jar.JarOutputStream
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> {
//
// Add all the applicable files for the current project
//
val buildDir = KFiles.buildDir(project)
val result = arrayListOf<IncludedFile>()
val classesDir = KFiles.makeDir(buildDir.path, "classes")
if (jar.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
val relClassesDir = Paths.get(project.directory).relativize(Paths.get(classesDir.path))
val prefixPath = Paths.get(project.directory).relativize(Paths.get(classesDir.path + "/"))
// Class files
val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) }
val filesNotExcluded : List<File> = files.filter {
! KFiles.Companion.isExcluded(KFiles.joinDir(project.directory, it.path), jar.excludes)
}
val fileSpecs = arrayListOf<IFileSpec>()
filesNotExcluded.forEach {
fileSpecs.add(IFileSpec.FileSpec(it.path.toString().substring(prefixPath.toString().length + 1)))
}
result.add(IncludedFile(From(prefixPath.toString()), To(""), fileSpecs))
// Resources, if applicable
context.variant.resourceDirectories(project).forEach {
result.add(IncludedFile(From(it.path), To(""), listOf(IFileSpec.GlobSpec("**"))))
}
} else {
//
// The user specified an include, just use it verbatim
//
val includedFiles = findIncludedFiles(project.directory, jar.includedFiles, jar.excludes)
result.addAll(includedFiles)
}
//
// If fatJar is true, add all the transitive dependencies as well: compile, runtime and dependent projects
//
if (jar.fatJar) {
log(2, "Creating fat jar")
val seen = hashSetOf<String>()
@Suppress("UNCHECKED_CAST")
val dependentProjects = project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
as List<ProjectDescription>
val allDependencies = project.compileDependencies + project.compileRuntimeDependencies
val transitiveDependencies = dependencyManager.calculateDependencies(project, context, dependentProjects,
allDependencies)
transitiveDependencies.map {
it.jarFile.get()
}.forEach { file : File ->
if (! seen.contains(file.path)) {
seen.add(file.path)
if (! KFiles.Companion.isExcluded(file, jar.excludes)) {
result.add(IncludedFile(specs = arrayListOf(IFileSpec.FileSpec(file.path)),
expandJarFiles = true))
}
}
}
}
return result
}
fun generateJar(project: Project, context: KobaltContext, jar: Jar) : File {
val allFiles = findIncludedFiles(project, context, jar)
//
// Generate the manifest
//
val manifest = java.util.jar.Manifest()//FileInputStream(mf))
jar.attributes.forEach { attribute ->
manifest.mainAttributes.putValue(attribute.first, attribute.second)
}
val jarFactory = { os: OutputStream -> JarOutputStream(os, manifest) }
return Archives.generateArchive(project, context, jar.name, ".jar", allFiles,
true /* expandJarFiles */, jarFactory)
}
}

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.Project
import com.beust.kobalt.api.annotation.ExportedProjectProperty
@ -30,7 +31,7 @@ class Archives {
val fullArchiveName = context.variant.archiveName(project, archiveName, suffix)
val archiveDir = File(KFiles.libsDir(project))
val result = File(archiveDir.path, fullArchiveName)
log(2, "Creating $result")
log(3, "Creating $result")
if (! Features.USE_TIMESTAMPS || isOutdated(project.directory, includedFiles, result)) {
val outStream = outputStreamFactory(FileOutputStream(result))
JarUtils.addFiles(project.directory, includedFiles, outStream, expandJarFiles)
@ -39,7 +40,7 @@ class Archives {
outStream.close()
log(1, " Created $result")
} else {
log(2, " $result is up to date")
log(3, " $result is up to date")
}
project.projectProperties.put(JAR_NAME, result.absolutePath)
@ -57,7 +58,7 @@ class Archives {
val file = File(KFiles.joinDir(directory, root.from, relFile.path))
if (file.isFile) {
if (file.lastModified() > lastModified) {
log(2, " TS - Outdated $file and $output "
log(3, " TS - Outdated $file and $output "
+ Date(file.lastModified()) + " " + Date(output.lastModified()))
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) {
upToDate = true
} 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 {
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 {
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}")
}
project.projectExtra.isDirty = true
@ -109,20 +109,20 @@ class IncrementalManager(val fileName: String = IncrementalManager.BUILD_INFO_FI
if (! upToDate) {
val result = iit.task(project)
if (result.success) {
logIncremental(2, "Incremental task $taskName done running, saving checksums")
logIncremental(3, "Incremental task $taskName done running, saving checksums")
iit.inputChecksum?.let {
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
iit.outputChecksum()?.let {
saveOutputChecksum(taskName, it)
logIncremental(2, " output checksum \"$it\" saved")
logIncremental(3, " output checksum \"$it\" saved")
}
}
result
} 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()
}
}