diff --git a/build.gradle.kts b/build.gradle.kts index d2ad8cd..614a26f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,37 +22,30 @@ fun getProps(f: File): Properties { return props } -val folder = project.file("src/main/resources"); -if (!folder.exists()) { - folder.mkdirs() -} - +// we handle cases without .git directory val props = project.file("src/main/resources/version.properties") -try { - // apply this plugin in a try-catch block so that we can handle cases without .git directory +val dotgit = project.file(".git") +if (dotgit.exists()) { apply(plugin = "com.palantir.git-version") val versionDetails: groovy.lang.Closure by extra val details = versionDetails() - if (details.isCleanTag) { - version = details.lastTag.substring(1) - } else { - version = details.lastTag.substring(1) + "-" + details.commitDistance + "-" + details.gitHash + "-SNAPSHOT" - } - tasks.register("writeVersionFile") { - props.delete() - props.appendText("version=" + project.version + "\n") - props.appendText("commit=" + details.gitHashFull + "\n") - props.appendText("branch=" + details.branchName) + val baseVersion = details.lastTag.substring(1) + if (details.isCleanTag) { // release version + version = baseVersion + } else { // snapshot version + version = baseVersion + "-" + details.commitDistance + "-" + details.gitHash + "-SNAPSHOT" } -} catch (e:Exception) { - project.logger.error(e.message) - if (props.exists()) { - val versionProperties = getProps(props) - // versionProperties.forEach { (k, v) -> if (k is String) project.extra.set(k, v) } - version = versionProperties.getProperty("version") - } else { - version = "1.0.0-SNAPSHOT" +} else if (props.exists()) { // when version.properties already exist, just use it. + version = getProps(props).getProperty("version") +} + +tasks.register("writeVersionFile") { + val folder = project.file("src/main/resources"); + if (!folder.exists()) { + folder.mkdirs() } + props.delete() + props.appendText("version=" + project.version) } tasks.getByName("jar") { diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..5727f7d --- /dev/null +++ b/gradle.properties @@ -0,0 +1,3 @@ +## When you don't use git for source tree and download source not from source distribution +## such as github zip download, you need to set version manually here. +# version = "0.1-SNAPSHOT" diff --git a/src/main/java/io/github/eb4j/ebview/utils/VersionString.java b/src/main/java/io/github/eb4j/ebview/utils/VersionString.java index 05842c7..625e2d9 100644 --- a/src/main/java/io/github/eb4j/ebview/utils/VersionString.java +++ b/src/main/java/io/github/eb4j/ebview/utils/VersionString.java @@ -10,17 +10,8 @@ private VersionString() { /** Full version, e.g. "1.0.0-0-123456-SNAPSHOT" */ public static final String VERSION; - /** commit id. */ - public static final String COMMIT; - - /** branch name. */ - public static final String BRANCH; - static { ResourceBundle b = ResourceBundle.getBundle("version"); VERSION = b.getString("version"); - COMMIT = b.getString("commit"); - BRANCH = b.getString("branch"); } - }