Skip to content

Commit b376708

Browse files
authored
DRY out build files (#197)
* create root build.gradle file for handling: * group * repositories * version calc * pmd and spotless * java version 17 * jar manifest entries
1 parent 3401bce commit b376708

5 files changed

Lines changed: 134 additions & 156 deletions

File tree

build.gradle.kts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
plugins {
2+
id("pmd")
3+
id("com.diffplug.spotless") version "8.0.0"
4+
}
5+
6+
fun runCommand(vararg args: String): String {
7+
val process = ProcessBuilder(*args)
8+
.directory(rootDir)
9+
.redirectErrorStream(true)
10+
.start()
11+
12+
val output = process.inputStream.bufferedReader().readText()
13+
val exitCode = process.waitFor()
14+
15+
if (exitCode != 0) {
16+
throw GradleException("Command failed with exit code $exitCode: ${args.joinToString(" ")}")
17+
}
18+
19+
return output.trim()
20+
}
21+
22+
val gitHash: String by lazy {
23+
runCommand("git", "rev-parse", "--short", "HEAD")
24+
}
25+
26+
val gitTag: String? by lazy {
27+
runCatching {
28+
runCommand("git", "describe", "--abbrev=0", "--tags")
29+
}.getOrNull()
30+
}
31+
32+
val commitCount: Int by lazy {
33+
val range = if (gitTag.isNullOrEmpty()) "HEAD" else "$gitTag..HEAD"
34+
runCommand("git", "rev-list", "--count", range).toInt()
35+
}
36+
37+
val branch: String by lazy {
38+
// First, try GitHub Actions environment variable
39+
val githubBranch = System.getenv("GITHUB_REF_NAME")
40+
if (!githubBranch.isNullOrBlank()) githubBranch
41+
42+
// Fallback to local git command
43+
else {
44+
runCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
45+
}
46+
}
47+
48+
fun parseTag(tag: String): Triple<Int, Int, Int>? {
49+
val regex = Regex("""v?(\d+)\.(\d+)\.(\d+)""")
50+
val match = regex.matchEntire(tag.trim()) ?: return null
51+
val (major, minor, patch) = match.destructured
52+
return Triple(major.toInt(), minor.toInt(), patch.toInt())
53+
}
54+
55+
fun calcVersion(): String {
56+
var (major, minor, patch) = parseTag(gitTag ?: "") ?: Triple(0, 1, 0)
57+
58+
if (branch == "main") {
59+
return "$major.${minor + 1}.$patch-m$commitCount"
60+
}
61+
62+
if (branch.startsWith("release/v")) {
63+
if (commitCount == 0) {
64+
return "$major.$minor.$patch"
65+
} else {
66+
return "$major.$minor.${patch + 1}-rc$commitCount"
67+
}
68+
}
69+
70+
return "$major.${minor + 1}.$patch-a$commitCount-g$gitHash"
71+
}
72+
73+
val calculatedVersion: String by lazy {
74+
calcVersion()
75+
}
76+
77+
// prints when Gradle evaluates the build
78+
println("DBOS Transact version: $calculatedVersion")
79+
80+
allprojects {
81+
group = "dev.dbos"
82+
version = calculatedVersion
83+
84+
repositories {
85+
mavenCentral()
86+
gradlePluginPortal()
87+
}
88+
}
89+
90+
subprojects {
91+
apply(plugin = "java")
92+
apply(plugin = "pmd")
93+
apply(plugin = "com.diffplug.spotless") // Spotless plugin
94+
95+
// PMD configuration
96+
extensions.configure<org.gradle.api.plugins.quality.PmdExtension> {
97+
toolVersion = "7.16.0"
98+
ruleSets = listOf() // disable defaults
99+
ruleSetFiles = files("${rootDir}/config/pmd/ruleset.xml")
100+
isConsoleOutput = true
101+
}
102+
103+
// Spotless configuration
104+
extensions.configure<com.diffplug.gradle.spotless.SpotlessExtension> {
105+
java {
106+
googleJavaFormat()
107+
importOrder("dev.dbos", "java", "javax", "")
108+
removeUnusedImports()
109+
trimTrailingWhitespace()
110+
endWithNewline()
111+
}
112+
}
113+
114+
plugins.withId("java") {
115+
extensions.configure<JavaPluginExtension> {
116+
toolchain {
117+
languageVersion.set(JavaLanguageVersion.of(17))
118+
}
119+
}
120+
121+
tasks.named<Jar>("jar") {
122+
manifest {
123+
attributes["Implementation-Version"] = project.version
124+
attributes["Implementation-Title"] = project.name
125+
attributes["Implementation-Vendor"] = "DBOS, Inc"
126+
attributes["Implementation-Vendor-Id"] = project.group
127+
attributes["SCM-Revision"] = gitHash
128+
}
129+
}
130+
}
131+
}

settings.gradle.kts

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,3 @@ toolchainManagement {
1414
}
1515
}
1616
}
17-
18-
fun runCommand(vararg args: String): String {
19-
val process = ProcessBuilder(*args)
20-
.directory(settingsDir)
21-
.redirectErrorStream(true)
22-
.start()
23-
24-
val output = process.inputStream.bufferedReader().readText()
25-
val exitCode = process.waitFor()
26-
27-
if (exitCode != 0) {
28-
throw GradleException("Command failed with exit code $exitCode: ${args.joinToString(" ")}")
29-
}
30-
31-
return output.trim()
32-
}
33-
34-
val gitHash: String by lazy {
35-
runCommand("git", "rev-parse", "--short", "HEAD")
36-
}
37-
38-
val gitTag: String? by lazy {
39-
runCatching {
40-
runCommand("git", "describe", "--abbrev=0", "--tags")
41-
}.getOrNull()
42-
}
43-
44-
val commitCount: Int by lazy {
45-
val range = if (gitTag.isNullOrEmpty()) "HEAD" else "$gitTag..HEAD"
46-
runCommand("git", "rev-list", "--count", range).toInt()
47-
}
48-
49-
val branch: String by lazy {
50-
// First, try GitHub Actions environment variable
51-
val githubBranch = System.getenv("GITHUB_REF_NAME")
52-
if (!githubBranch.isNullOrBlank()) githubBranch
53-
54-
// Fallback to local git command
55-
else {
56-
runCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
57-
}
58-
}
59-
60-
fun parseTag(tag: String): Triple<Int, Int, Int>? {
61-
val regex = Regex("""v?(\d+)\.(\d+)\.(\d+)""")
62-
val match = regex.matchEntire(tag.trim()) ?: return null
63-
val (major, minor, patch) = match.destructured
64-
return Triple(major.toInt(), minor.toInt(), patch.toInt())
65-
}
66-
67-
fun calcVersion(): String {
68-
var (major, minor, patch) = parseTag(gitTag ?: "") ?: Triple(0, 1, 0)
69-
70-
if (branch == "main") {
71-
return "$major.${minor + 1}.$patch-m$commitCount"
72-
}
73-
74-
if (branch.startsWith("release/v")) {
75-
if (commitCount == 0) {
76-
return "$major.$minor.$patch"
77-
} else {
78-
return "$major.$minor.${patch + 1}-rc$commitCount"
79-
}
80-
}
81-
82-
return "$major.${minor + 1}.$patch-a$commitCount-g$gitHash"
83-
}
84-
85-
val calculatedVersion: String by lazy {
86-
calcVersion()
87-
}
88-
89-
println("Transact version: $calculatedVersion") // prints when Gradle evaluates the build
90-
91-
gradle.rootProject {
92-
extra["calculatedVersion"] = calculatedVersion
93-
extra["gitHash"] = gitHash
94-
}

transact-cli/build.gradle.kts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
group = "dev.dbos"
2-
version = rootProject.extra["calculatedVersion"] as String
3-
41
plugins {
52
application
63
}
@@ -9,27 +6,6 @@ application {
96
mainClass.set("dev.dbos.transact.cli.Main")
107
}
118

12-
repositories {
13-
gradlePluginPortal()
14-
mavenCentral()
15-
}
16-
179
dependencies {
1810
implementation(project(":transact"))
1911
}
20-
21-
java {
22-
toolchain {
23-
languageVersion.set(JavaLanguageVersion.of(17))
24-
}
25-
}
26-
27-
tasks.jar {
28-
manifest {
29-
attributes["Implementation-Version"] = project.version
30-
attributes["Implementation-Title"] = project.name
31-
attributes["Implementation-Vendor"] = "DBOS, Inc"
32-
attributes["Implementation-Vendor-Id"] = project.group
33-
attributes["SCM-Revision"] = rootProject.extra["gitHash"] as String
34-
}
35-
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package dev.dbos.transact.cli;
22

33
public class Main {
4-
public static void main(String[] args) {
5-
System.out.println("Hello World");
6-
}
4+
public static void main(String[] args) {
5+
System.out.println("Hello World");
6+
}
77
}
8-

transact/build.gradle.kts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
1-
group = "dev.dbos"
2-
version = rootProject.extra["calculatedVersion"] as String
3-
41
plugins {
52
id("java")
63
id("java-library")
7-
id("pmd")
8-
id("com.diffplug.spotless") version "8.0.0"
94
id("com.vanniktech.maven.publish") version "0.34.0"
105
}
116

12-
java {
13-
toolchain {
14-
languageVersion.set(JavaLanguageVersion.of(17))
15-
}
16-
}
17-
18-
spotless {
19-
java {
20-
googleJavaFormat()
21-
importOrder("dev.dbos", "java", "javax", "")
22-
removeUnusedImports()
23-
trimTrailingWhitespace()
24-
endWithNewline()
25-
}
26-
}
27-
28-
pmd {
29-
ruleSets = listOf() // disable defaults
30-
ruleSetFiles = files("config/pmd/ruleset.xml")
31-
isConsoleOutput = true
32-
toolVersion = "7.16.0"
33-
}
34-
35-
tasks.withType<Pmd> {
36-
reports {
37-
xml.required.set(true)
38-
html.required.set(true)
39-
}
40-
}
41-
427
tasks.withType<JavaCompile> {
438
options.compilerArgs.add("-Xlint:unchecked") // warn about unchecked operations
449
options.compilerArgs.add("-Xlint:deprecation") // warn about deprecated APIs
@@ -57,11 +22,6 @@ tasks.named("build") {
5722
dependsOn("javadoc")
5823
}
5924

60-
repositories {
61-
gradlePluginPortal()
62-
mavenCentral()
63-
}
64-
6525
dependencies {
6626
api("org.slf4j:slf4j-api:2.0.13") // logging api
6727

@@ -85,16 +45,6 @@ dependencies {
8545
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
8646
}
8747

88-
tasks.jar {
89-
manifest {
90-
attributes["Implementation-Version"] = project.version
91-
attributes["Implementation-Title"] = project.name
92-
attributes["Implementation-Vendor"] = "DBOS, Inc"
93-
attributes["Implementation-Vendor-Id"] = project.group
94-
attributes["SCM-Revision"] = rootProject.extra["gitHash"] as String
95-
}
96-
}
97-
9848
tasks.test {
9949
useJUnitPlatform()
10050
testLogging {

0 commit comments

Comments
 (0)