Skip to content

Commit

Permalink
Generate version/datetime string at build-time
Browse files Browse the repository at this point in the history
Since we can now release several jar versions a day, it's useful to have the
version number update automatically. We place the version string in a file,
add the file to the resources, and then load the resource file when the
`AboutVersionPanel` is loaded.
  • Loading branch information
garfieldnate committed Aug 15, 2024
1 parent cb11fec commit 1452a44
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 43 deletions.
116 changes: 75 additions & 41 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.util.*

version = "4.6.22"

plugins {
java
application
java
application
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
testImplementation(libs.junit.jupiter)
testImplementation("org.mockito:mockito-core:5.12.0")

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// load all the jars in the lib/ folder
implementation(fileTree("lib") {
include("*.jar")
})
testImplementation(libs.junit.jupiter)
testImplementation("org.mockito:mockito-core:5.12.0")

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// load all the jars in the lib/ folder
implementation(fileTree("lib") {
include("*.jar")
})
}

// For loading the SML JNI library
Expand All @@ -27,54 +31,84 @@ tasks.withType<JavaExec> {
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
options.encoding = "UTF-8"
}

// make an executable jar that will depend on other jars being available in the classpath
tasks.jar {
manifest {
attributes(
mapOf(
"Title" to "${project.name} ${project.version}",
"Vendor" to "University of Michigan",
"Date" to LocalDate.now().toString(),
"Version" to "${project.version}",
"Copyright" to "(c) The Regents of the University of Michigan, 2024",
"Main-Class" to application.mainClass.get(),
"Class-Path" to ". java/sml.jar bin/java/sml.jar lib/sml.jar"
)
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
manifest {
attributes(
mapOf(
"Title" to "${project.name} ${project.version}",
"Vendor" to "University of Michigan",
"Date" to LocalDate.now().toString(),
"Version" to "${project.version}",
"Copyright" to "(c) The Regents of the University of Michigan, 2024",
"Main-Class" to application.mainClass.get(),
"Class-Path" to ". java/sml.jar bin/java/sml.jar lib/sml.jar"
)
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
// Do not include runtime dependencies in the jar (we want a slim jar, not a fat one):
// from({
// configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
// })
}

tasks.test {
useJUnitPlatform()
useJUnitPlatform()

reports {
junitXml.required.set(true)
junitXml.outputLocation.set(file("${layout.buildDirectory.get()}/reports/test-results"))
}
reports {
junitXml.required.set(true)
junitXml.outputLocation.set(file("${layout.buildDirectory.get()}/reports/test-results"))
}
}

application {
mainClass = "edu.umich.soar.visualsoar.VisualSoar"
mainClass = "edu.umich.soar.visualsoar.VisualSoar"
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

/////////////////////////////////////////////////////////////////
// Generate version/datetime string to show in the application //
/////////////////////////////////////////////////////////////////

val generateVersionFile by tasks.registering {
val dateFormat = SimpleDateFormat("dd MMM yyyy HH:mm:ss 'UTC'")
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
val date = dateFormat.format(Date())
val versionString = "${project.version} ($date)"
val templateContext = mapOf("versionString" to versionString)
// When versionString changes, the task is out of date
inputs.properties(templateContext)
// without doLast, the file is not consistently generated
doLast {
val versionFile = layout.buildDirectory.file("generated/resources/versionString.txt").get().asFile
versionFile.parentFile.mkdirs()
versionFile.writeText(versionString)
logger.lifecycle("Generating version file at: ${versionFile.absolutePath}")
}
}

tasks.withType<JavaCompile> {
dependsOn(generateVersionFile)
}

// Add the extra resource directory to the main source set
sourceSets.main {
resources.srcDir(layout.buildDirectory.dir("generated/resources"))
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ The slim jar we distribute with Soar:

./gradlew jar

There is also an Ant script that does the same (we will likely remove it in the future):
There is also an Ant script that does the same; it is outdated, however, and should not be used for distributing.
We will likely remove it in the future:

ant build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* Panel that displays the Version number of Visual Soar for the 'About' dialog
Expand All @@ -15,13 +19,36 @@ class AboutVersionPanel extends JPanel {

JLabel versionLabel =
new JLabel("Visual Soar");

String version = loadVersionString();
JLabel versionLabel2 =
new JLabel(" Version 4.6.22 (14 Aug 2024)");
new JLabel(" " + version);

public AboutVersionPanel() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(new EmptyBorder(10, 10, 10, 10));
add(versionLabel);
add(versionLabel2);
}

private String loadVersionString() {
// this file is generated at build-time
String resourcePath = "versionString.txt";
getClass().getClassLoader();
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(resourcePath)) {
if (inputStream == null) {
throw new IOException("Could not find resource file 'versionString.txt'");
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line = reader.readLine();
if (line == null) {
throw new IOException("No lines found in version.txt");
}
return line;
}
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
}

0 comments on commit 1452a44

Please sign in to comment.