diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt similarity index 65% rename from src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt rename to modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt index 86c5eb1c..432039ea 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt @@ -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.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.* @@ -14,7 +14,43 @@ import java.io.OutputStream import java.nio.file.Paths 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, excludes: List) + : List { + val result = arrayListOf() + files.forEach { includedFile -> + val includedSpecs = arrayListOf() + 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 { // // Add all the applicable files for the current project @@ -33,7 +69,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) // Class files val files = KFiles.findRecursively(classesDir).map { File(relClassesDir.toFile(), it) } val filesNotExcluded : List = 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() filesNotExcluded.forEach { @@ -49,7 +85,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) // // 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) } @@ -71,7 +107,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) }.forEach { file : File -> if (! seen.contains(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)), expandJarFiles = true)) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt similarity index 92% rename from modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt rename to modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt index 9860a7dd..ca808318 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Archives.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Archives.kt @@ -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 } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/AttributeHolder.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/AttributeHolder.kt new file mode 100644 index 00000000..4abffb21 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/AttributeHolder.kt @@ -0,0 +1,6 @@ +package com.beust.kobalt.archive + +interface AttributeHolder { + fun addAttribute(k: String, v: String) +} + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt new file mode 100644 index 00000000..fbbf23a0 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Jar.kt @@ -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)) + } +} + + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Manifest.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Manifest.kt new file mode 100644 index 00000000..1771dbe8 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Manifest.kt @@ -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) + } +} + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/War.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/War.kt new file mode 100644 index 00000000..c98ebb70 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/War.kt @@ -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("**")) + } +} + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt new file mode 100644 index 00000000..93178c03 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/Zip.kt @@ -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() + + @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() + +} + + diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/IncrementalManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/IncrementalManager.kt index 94d7a1b7..6c38dca9 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/IncrementalManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/IncrementalManager.kt @@ -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() } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt index 42420f0e..234239d1 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt @@ -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 diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 715a9643..c5d90eeb 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -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, excludes: List) - : List { - val result = arrayListOf() - files.forEach { includedFile -> - val includedSpecs = arrayListOf() - 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() - internal val excludes = arrayListOf() - - @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() - -} - -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) - } -} diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt index e3f463e2..ad4b4f8d 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/WarGenerator.kt @@ -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 } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt index eb50dfae..21220534 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/ZipGenerator.kt @@ -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) } }