initial commit - version per properties
This commit is contained in:
commit
f5b99c3038
11 files changed
+574
No files matched your search
@@ -0,0 +1,12 @@
|
||||
package net.keinekekse.version
|
||||
|
||||
import org.gradle.api.tasks.Copy
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
abstract class AddVersionTask : Copy() {
|
||||
|
||||
@TaskAction
|
||||
fun updateVersion() {
|
||||
// action is defined in plugin
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package net.keinekekse.version
|
||||
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.file.RegularFile
|
||||
import org.gradle.api.tasks.bundling.Jar
|
||||
import org.gradle.api.tasks.bundling.War
|
||||
import org.gradle.kotlin.dsl.extra
|
||||
import org.gradle.kotlin.dsl.getValue
|
||||
import org.gradle.kotlin.dsl.named
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.OutputStreamWriter
|
||||
import java.util.*
|
||||
|
||||
class VersionPlugin : Plugin<Project> {
|
||||
private lateinit var versionFileName: String
|
||||
private val buildNumProp = "buildNumber"
|
||||
private val addTaskName = "addVersion"
|
||||
private var isWar = false
|
||||
|
||||
override fun apply(project: Project) {
|
||||
// println("Version Plugin")
|
||||
val extension = project.extensions.create("version", VersionPluginExtension::class.java)
|
||||
versionFileName = extension.propertiesFilename.orNull ?: "${project.name}.properties"
|
||||
|
||||
val warSource =
|
||||
project.layout.projectDirectory.file("src/main/webapp/WEB-INF/classes/${versionFileName}")
|
||||
val jarSource =
|
||||
project.layout.projectDirectory.file("src/main/properties/${versionFileName}")
|
||||
val targetDir = project.layout.buildDirectory.dir("resources/main")
|
||||
val countFile = project.layout.projectDirectory.file("buildnumber.properties")
|
||||
if (warSource.asFile.parentFile.parentFile.exists()) {
|
||||
isWar = true
|
||||
}
|
||||
if (isWar) {
|
||||
if (!warSource.asFile.parentFile.exists())
|
||||
warSource.asFile.parentFile.mkdir()
|
||||
checkVersionFile(warSource)
|
||||
} else {
|
||||
checkVersionFile(jarSource)
|
||||
}
|
||||
|
||||
if (isWar) {
|
||||
try {
|
||||
project.tasks.named<War>("war") {
|
||||
filesMatching("**/${versionFileName}") {
|
||||
val num = buildNumber(countFile)
|
||||
project.extra[buildNumProp] = num
|
||||
expand(project.properties)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("\n*** Falsche Reihenfolge ***\n\nWAR-Plugin muss vor dem Versions-Plugin eingetragen werden!\n")
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
project.tasks.named<Jar>("jar") {
|
||||
dependsOn(project.tasks.named(addTaskName))
|
||||
}
|
||||
|
||||
project.tasks.register(addTaskName, AddVersionTask::class.java) {
|
||||
group = "net.keinekekse"
|
||||
description = "Adds version information to the project."
|
||||
outputs.upToDateWhen { false }
|
||||
|
||||
mustRunAfter(project.tasks.named("classes"))
|
||||
|
||||
onlyIf {
|
||||
val cj by project.tasks.named("compileJava")
|
||||
val pr by project.tasks.named("processResources")
|
||||
// (cj.state.didWork || pr.didWork)
|
||||
((cj.state.didWork && !cj.state.upToDate) || (pr.didWork && !pr.state.upToDate))
|
||||
}
|
||||
|
||||
from(jarSource)
|
||||
into(targetDir)
|
||||
filesMatching("*") {
|
||||
val num = buildNumber(countFile)
|
||||
project.extra[buildNumProp] = num
|
||||
expand(project.properties)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment build number
|
||||
*/
|
||||
private fun buildNumber(propFile: RegularFile): String {
|
||||
val propName = "build.number"
|
||||
val props = Properties()
|
||||
|
||||
try {
|
||||
FileInputStream(propFile.asFile).use { props.load(it) }
|
||||
} catch (e: Exception) {
|
||||
// noop
|
||||
}
|
||||
val numStr: String? = props.getProperty(propName)
|
||||
|
||||
var num = numStr?.toLongOrNull() ?: 0
|
||||
num++
|
||||
println("build number ${num}")
|
||||
|
||||
props.setProperty(propName, num.toString())
|
||||
props.store(FileOutputStream(propFile.asFile, false), "auto generated")
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create version properties file if it doesn't exist
|
||||
*/
|
||||
private fun checkVersionFile(file: RegularFile) {
|
||||
if (!file.asFile.exists()) {
|
||||
val content = """
|
||||
|name=${'$'}{name}
|
||||
|displayname=${'$'}{displayname}
|
||||
|description=${'$'}{description}
|
||||
|copyright=${'$'}{copyright}
|
||||
|version=${'$'}{version}
|
||||
|build.number=${'$'}{${buildNumProp}}
|
||||
|build.timestamp=${'$'}{buildTimestamp}
|
||||
""".trimMargin()
|
||||
val outfile = OutputStreamWriter(FileOutputStream(file.asFile))
|
||||
outfile.write(content)
|
||||
outfile.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package net.keinekekse.version
|
||||
|
||||
import org.gradle.api.provider.Property
|
||||
|
||||
interface VersionPluginExtension {
|
||||
|
||||
/**
|
||||
* Name for the version properties file
|
||||
* (JAR: src/main/properties, WAR: src/main/webapp/WEB-INF/classes)
|
||||
* Will be copied to the classpath root.
|
||||
*
|
||||
* Default: <project.name>.properties
|
||||
*/
|
||||
val propertiesFilename: Property<String>
|
||||
}
|
||||
Reference in new issue
Block a user