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

@ -4,13 +4,14 @@ import com.beust.kobalt.*
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive
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.JvmCompilerPlugin
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.RunCommand
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.PackagingPlugin
import com.google.inject.Inject

View file

@ -1,101 +0,0 @@
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
import com.beust.kobalt.api.ProjectDescription
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){
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.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 = PackagingPlugin.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.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,18 +1,21 @@
package com.beust.kobalt.plugin.packaging
import com.beust.kobalt.*
import com.beust.kobalt.IFileSpec.FileSpec
import com.beust.kobalt.IFileSpec.GlobSpec
import com.beust.kobalt.JarGenerator
import com.beust.kobalt.KobaltException
import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.ExportedProjectProperty
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.maven.DependencyManager
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.nio.file.Paths
import javax.inject.Inject
import javax.inject.Singleton
@ -35,40 +38,6 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
const val TASK_ASSEMBLE: String = "assemble"
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
@ -222,7 +191,7 @@ class PackageConfig(val project: Project) : AttributeHolder {
class MavenJars(val ah: AttributeHolder, var fatJar: Boolean = false, var manifest: Manifest? = null) :
AttributeHolder by ah {
public fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest {
fun manifest(init: Manifest.(p: Manifest) -> Unit) : Manifest {
val m = Manifest(this)
m.init(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 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
import com.beust.kobalt.Archives
import com.beust.kobalt.archive.Archives
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.KobaltContext
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
//
result.addAll(PackagingPlugin.findIncludedFiles(project.directory, war.includedFiles, war.excludes))
result.addAll(JarGenerator.findIncludedFiles(project.directory, war.includedFiles, war.excludes))
return result
}

View file

@ -1,14 +1,16 @@
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.Project
import com.beust.kobalt.archive.Archives
import com.beust.kobalt.archive.Zip
import com.beust.kobalt.maven.DependencyManager
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)
val allFiles = JarGenerator.findIncludedFiles(project.directory, zip.includedFiles, zip.excludes)
Archives.generateArchive(project, context, zip.name, ".zip", allFiles)
}
}