Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Improve project.version handling
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Sep 26, 2021
1 parent f903e9d commit 3dfb1ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
43 changes: 18 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<com.palantir.gradle.gitversion.VersionDetails> 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") {
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 0 additions & 9 deletions src/main/java/io/github/eb4j/ebview/utils/VersionString.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

0 comments on commit 3dfb1ee

Please sign in to comment.