4 Commits
9 changed files with 370 additions and 125 deletions

No files matched your search

+10 -1
View File
@@ -13,9 +13,14 @@ group = "net.keinekekse"
version = masterLibs.versions.versionPluginVersion.get()
description = "Adds version information to the project."
extra["displayname"] = "Version-Plugin"
extra["copyright"] = "Copyright (c) 2024-2025 Hartmut Bauer"
extra["copyright"] = "Copyright (c) 2024-2026 Hartmut Bauer"
extra["buildTimestamp"] = Instant.now().toString()
dependencies {
implementation(masterLibs.semver)
compileOnly(masterLibs.kotlinGradlePlugin)
}
publishing {
repositories {
maven {
@@ -36,6 +41,10 @@ gradlePlugin {
}
}
kotlin {
jvmToolchain(masterLibs.versions.javaVersion.get().toInt())
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(masterLibs.versions.javaVersion.get()))
Binary file not shown.
+1 -1
View File
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Vendored
+7 -8
View File
@@ -1,7 +1,7 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
# Copyright © 2015 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
@@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -84,7 +86,7 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
@@ -112,7 +114,6 @@ case "$( uname )" in #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
@@ -170,7 +171,6 @@ fi
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
@@ -203,15 +203,14 @@ fi
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
Vendored
+3 -2
View File
@@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@@ -68,11 +70,10 @@ goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
+1 -1
View File
@@ -7,7 +7,7 @@ dependencyResolutionManagement {
versionCatalogs {
create("masterLibs") {
from("net.keinekekse:version-catalog:56")
from("net.keinekekse:version-catalog:141")
}
}
}
@@ -1,12 +1,191 @@
package net.keinekekse.version
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.TaskAction
import io.github.z4kn4fein.semver.Version
import io.github.z4kn4fein.semver.withoutSuffixes
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import java.util.*
abstract class AddVersionTask : Copy() {
@CacheableTask
abstract class AddVersionTask : DefaultTask() {
// 1. Define what triggers the task (Source Files)
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val sourceFiles: ConfigurableFileCollection
// 2. Define the output (The properties file)
@get:OutputFile
abstract val buildNumberFile: RegularFileProperty
@get:OutputFile
abstract val versionClassFile: RegularFileProperty
@get:Input
abstract val projectVersion: Property<String>
@get:Input
abstract val projectName: Property<String>
@get:Input
abstract val projectDescription: Property<String>
@get:Input
abstract val displayNameExtra: Property<String>
@get:Input
abstract val copyrightExtra: Property<String>
@get:Input
abstract val versionPackageName: Property<String>
@get:Input
abstract val modules: MapProperty<String, Map<String, String>>
@TaskAction
fun updateVersion() {
// action is defined in plugin
fun execute() {
val buildNumber = incrementAndGetBuildNumber()
writeClassFile(buildNumber)
}
private fun writeClassFile(buildNumber: String) {
var fileContent =
"""
package ${versionPackageName.get()}
import net.keinekekse.version.Version
""".trimIndent() +
buildVersionObject(
"",
projectVersion.get(),
buildNumber,
displayNameExtra.get(),
copyrightExtra.get(),
projectName.get(),
projectDescription.get()
)
if (modules.isPresent) {
modules.get().forEach { (moduleName, moduleInfo) ->
fileContent +=
buildVersionObject(
moduleName,
moduleInfo["version"] ?: "",
buildNumber,
moduleInfo["displayName"] ?: moduleName,
moduleInfo["copyright"] ?: copyrightExtra.get(),
moduleName,
moduleInfo["description"] ?: ""
)
}
}
val file = versionClassFile.get().asFile
file.parentFile.mkdirs()
file.writeText(fileContent)
}
private fun buildVersionObject(
moduleName: String,
versionString: String,
buildNumber: String,
longName: String,
copy: String,
shortName: String,
desc: String,
): String {
val ver = getVersionInfo(versionString, buildNumber, longName, copy, shortName, desc)
val module = if (moduleName.isBlank()) {
""
} else {
"_$moduleName"
}
return """
object VERSION$module: Version {
override val version = "${ver.version}"
override val fullVersion = "${ver.fullVersion}"
override val major = ${ver.major}
override val minor = ${ver.minor}
override val patch = ${ver.patch}
override val preRelease = "${ver.preRelease}"
override val buildMetadata = "${ver.buildMetadata}"
override val buildDate = "${ver.buildDate}"
override val displayName = "${ver.displayName}"
override val copyright = "${ver.copyright}"
override val name = "${ver.name}"
override val description = "${ver.description}"
}
""".trimIndent()
}
private fun getVersionInfo(
versionString: String,
buildNumber: String,
longName: String,
copy: String,
shortName: String,
desc: String,
): VersionInfo {
val version =
if (Version.parse(versionString, strict = false).buildMetadata.isNullOrEmpty())
Version.parse("$versionString+build.$buildNumber", strict = false)
else Version.parse(versionString, strict = false)
println("Source files changed. Setting version to $version")
return VersionInfo(
major = version.major,
minor = version.minor,
patch = version.patch,
preRelease = version.preRelease ?: "",
buildMetadata = version.buildMetadata ?: "",
fullVersion = version.toString(),
version = if (version.isPreRelease)
"${version.major}.${version.minor}.${version.patch}-${version.preRelease}" else
version.withoutSuffixes().toString(),
buildDate = java.time.Instant.now().toString(),
displayName = longName,
copyright = copy,
name = shortName,
description = desc
)
}
private fun incrementAndGetBuildNumber(): String {
val file = buildNumberFile.get().asFile
val props = Properties()
if (file.exists()) {
file.inputStream().use { props.load(it) }
}
val current = props.getProperty("build.number")?.toLongOrNull() ?: 0
val actual = (current + 1).toString()
props.setProperty("build.number", actual)
file.outputStream().use {
props.store(it, "auto generated")
}
return actual
}
data class VersionInfo(
val version: String,
val fullVersion: String,
val major: Int,
val minor: Int,
val patch: Int,
val preRelease: String,
val buildMetadata: String,
val buildDate: String,
val displayName: String,
val copyright: String,
val name: String,
val description: String,
)
}
@@ -1,131 +1,180 @@
package net.keinekekse.version
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.java.archives.Manifest
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.named
import java.io.FileInputStream
import java.io.FileOutputStream
import java.time.Instant
import java.util.*
import org.gradle.api.tasks.SourceSetContainer
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
/**
* Versions-Resource im Projekt anlegen und bei jedem Build eine Build-Nummer hochzählen
*
* Ohne weitere Konfiguration wird die Datei Version.kt mit der class VERSION im Verzeichnis
* src/main/kotlin/<project.group>/<project.name> erzeugt, für KMP in src/commonMain/kotlin.
* Die class wird mit Daten aus build.gradle.kts befüllt (s.u.).
*
* Für die Build-Nummer wird eine Datei buildnumber.properties im Projektverzeichnis erzeugt.
*
* Die class-Datei und der Package-Name können ggf. in build.gradle.kts konfiguriert werden:
*
* ```
* plugins {
* // Plugin einbinden
* id("net.keinekekse.version") version "3.0.0"
* }
*
* // Werte, die für die Version-Datei verwendet werden
* // in settings.gradle.kt: rootProject.name = "program-name"
* group = "org.example"
* version = "0.0.1-beta.1"
* description = "Program description"
* extra["displayname"] = "Program"
* extra["copyright"] = "Copyright (c) 2025 Hartmut Bauer"
*
*
* // Optional: Konfiguration der Version-Datei und des Packages
* versionConfig {
* versionClassFile.set(layout.projectDirectory.file("src/main/kotlin/Version.kt"))
* versionPackageName.set("net.keinekekse")
*
* // Optional Versionen von Teilbereichen des Programms
* modules.set(
* mapOf(
* "books" to mapOf( // -> object VERSION_books
* "version" to libs.versions.books.get(),
* "displayName" to "Books-DB",
* "copyright" to "Copyright (c) 2026 Hartmut Bauer", // null => extra["copyright]
* "description" to "Books + Comics Database" // null => ""
* )
* )
* )
* }
* ```
*
* VERSION-class:
*
* ```
* import net.keinekekse.version.Version // -> util-Lib
*
* object VERSION: Version {
* override val version = "1.0.0-beta.1 // für Release-Ver: major.minor.patch
* override val fullVersion = "1.0.0-beta.1+build.13"
* override val major = 1
* override val minor = 0
* override val patch = 0
* override val preRelease = "beta.1"
* override val buildMetadata = "build.13"
* override val buildDate = "2026-01-10T18:54:42.227942Z"
* override val displayName = "Program"
* override val copyright = "Copyright (c) 2026 Hartmut Bauer"
* override val name = "program-name"
* override val description = "Program description"
* }
* object VERSION_books: Version {
* override val version = "0.1.0-alpha.1"
* override val fullVersion = "0.1.0-alpha.1+build.11"
* override val major = 0
* override val minor = 1
* override val patch = 0
* override val preRelease = "alpha.1"
* override val buildMetadata = "build.11"
* override val buildDate = "2026-09-19T00:18:40.584742Z"
* override val displayName = "Books-DB"
* override val copyright = "Copyright (c) 2026 Hartmut Bauer"
* override val name = "books"
* override val description = "Books + Comics Database"
* }
* ```
*
*
*/
class VersionPlugin : Plugin<Project> {
private var isWar = false
companion object {
private const val BUILDNUMBER_FILE = "buildnumber.properties"
private const val BUILD_NUMBER_KEY = "build.number"
private const val GENERATED_COMMENT = "auto generated"
}
override fun apply(project: Project) {
validateNoSubprojects(project)
val extension =
project.extensions.create("versionConfig", VersionPluginExtension::class.java)
project.afterEvaluate {
val projectInfo = ProjectInfo(
version = project.version.toString(),
displayName = getOptionalExtra(project, "displayname"),
copyright = getOptionalExtra(project, "copyright"),
name = project.name,
description = project.description ?: ""
)
val versionTask =
project.tasks.register("incrementBuildNumber", AddVersionTask::class.java) {
if (!extension.versionClassFile.isPresent) {
val packageName =
project.group.toString().replace("-", ".") +
"." + project.name.replace("-", ".")
val versionFileName = packageName.replace(".", "/") + "/Version.kt"
isWar = isWarProject(project)
if (isWar) {
addManifestTask(project, "war", projectInfo)
} else {
addManifestTask(project, "jar", projectInfo)
}
}
}
var targ = "src/main/kotlin/"
if (!project.layout.projectDirectory.file(targ).asFile.exists()) {
targ = "src/commonMain/kotlin/"
if (!project.layout.projectDirectory.file(targ).asFile.exists()) {
throw InvalidUserDataException(
"Version-Plugin: 'versionClassFile' and 'versionPackageName' must " +
"be set in a versionConfig-block in build.gradle.kts, " +
"as both 'src/main/kotlin' and 'src/commonMain/kotlin' do not exist!"
)
}
}
extension.versionClassFile.convention(project.layout.projectDirectory.file(targ + versionFileName))
extension.versionPackageName.convention(packageName)
}
private fun validateNoSubprojects(project: Project) {
if (project.parent != null) {
throw Exception("This plugin cannot handle subprojects!")
}
}
// Point to the source sets
val files = project.files(
"src/main/kotlin", "src/main/java",
"src/commonMain/kotlin", "src/commonMain/java",
"src/jvmMain/kotlin", "src/jvmMain/java"
)
// Filter lazily using a provider
sourceFiles.from(extension.versionClassFile.map { classFile ->
files.asFileTree.filter { it.name != classFile.asFile.name }
})
private fun isWarProject(project: Project): Boolean {
return project.layout.projectDirectory.file("src/main/webapp/WEB-INF").asFile.exists()
}
// Point to the properties file
buildNumberFile.set(project.layout.projectDirectory.file("buildnumber.properties"))
private fun getOptionalExtra(project: Project, key: String): String {
return try {
project.extra[key]?.toString() ?: ""
} catch (e: Exception) {
""
}
}
// Link properties directly (this is lazy)
versionClassFile.set(extension.versionClassFile)
versionPackageName.set(extension.versionPackageName)
private fun addManifestTask(project: Project, taskType: String, projectInfo: ProjectInfo) {
val countFile = project.layout.projectDirectory.file(BUILDNUMBER_FILE)
projectVersion.set(project.provider { project.version.toString() })
projectName.set(project.provider { project.name })
projectDescription.set(project.provider { project.description ?: "" })
if (taskType == "war") {
try {
project.tasks.named<War>(taskType) {
doFirst {
applyManifestAttributes(manifest, countFile, projectInfo)
displayNameExtra.set(project.provider {
project.extensions.extraProperties.let {
if (it.has("displayname")) it.get("displayname")?.toString() else ""
}
})
copyrightExtra.set(project.provider {
project.extensions.extraProperties.let {
if (it.has("copyright")) it.get("copyright")?.toString() else ""
}
})
if (extension.modules.isPresent) {
modules.set(extension.modules)
extension.modules.get().forEach { (moduleName, moduleInfo) ->
// project.extensions.create("${moduleName}Version", VersionPluginExtension::class.java).apply {
// versionClassFile.set(project.layout.buildDirectory.file("generated/sources/version/${moduleName}/kotlin/${project.name}-version.kt"))
// modules.set(moduleInfo)
// }
}
}
} catch (e: Exception) {
println("\n*** Falsche Reihenfolge ***\n\nwar-Plugin muss vor dem Versions-Plugin eingetragen werden!\n")
throw e
}
} else if (taskType == "jar") {
project.tasks.named<Jar>(taskType) {
doFirst {
applyManifestAttributes(manifest, countFile, projectInfo)
}
// 1. Handle Kotlin/JVM (Standard Project)
project.plugins.withId("org.jetbrains.kotlin.jvm") {
project.extensions.getByType(SourceSetContainer::class.java).named("main") {
// This tells Gradle that this source set includes the directory containing the generated file
this.java.srcDir(versionTask.map { it.versionClassFile.get().asFile.parentFile })
}
}
}
private fun applyManifestAttributes(
manifest: Manifest,
countFile: RegularFile,
projectInfo: ProjectInfo,
) {
val buildNumber = incrementBuildNumber(countFile)
with(manifest.attributes) {
this["module-name"] = projectInfo.name
this["displayname"] = projectInfo.displayName
this["description"] = projectInfo.description
this["copyright"] = projectInfo.copyright
this["version"] = projectInfo.version
this["build-number"] = buildNumber
this["build-timestamp"] = Instant.now().toString()
// 2. Handle Kotlin Multiplatform
project.plugins.withId("org.jetbrains.kotlin.multiplatform") {
val kotlinExt = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
kotlinExt.sourceSets.named("commonMain") {
this.kotlin.srcDir(versionTask.map { it.versionClassFile.get().asFile.parentFile })
}
}
println("build ${projectInfo.version}-$buildNumber")
}
private fun incrementBuildNumber(propFile: RegularFile): String {
val props = Properties()
try {
FileInputStream(propFile.asFile).use { props.load(it) }
} catch (e: Exception) {
// noop
}
val currentBuildNumber = props.getProperty(BUILD_NUMBER_KEY)?.toLongOrNull() ?: 0
val newBuildNumber = currentBuildNumber + 1
props.setProperty(BUILD_NUMBER_KEY, newBuildNumber.toString())
props.store(FileOutputStream(propFile.asFile, false), GENERATED_COMMENT)
return newBuildNumber.toString()
}
data class ProjectInfo(
val version: String,
val displayName: String,
val copyright: String,
val name: String,
val description: String,
)
}
@@ -1,5 +1,13 @@
package net.keinekekse.version
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
interface VersionPluginExtension
interface VersionPluginExtension {
val versionPackageName: Property<String>
val versionClassFile: RegularFileProperty
val modules: MapProperty<String, Map<String, String>>
}