-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
274 lines (227 loc) · 11.4 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
274 lines (227 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* ------------------------------------------------------------------------------
* Ren2Date - Rename the provided file with a current date timestamp
*
* Copyright (c) 2004-2026 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ------------------------------------------------------------------------------*/
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.security.MessageDigest
import java.text.SimpleDateFormat
import java.util.Date
plugins {
java
application
id("com.github.ben-manes.versions") version "0.53.0"
id("com.gradleup.shadow") version "9.3.2"
}
group = "org.fross"
val javaVersion = 21
application {
mainClass.set("org.fross.ren2date.Main")
}
// --------------------------------------------------------------------------------------------------------
// JavaCompile Tasks: Tell Gradle to output the right java version's bytecode
// --------------------------------------------------------------------------------------------------------
tasks.withType<JavaCompile> {
options.release.set(javaVersion)
}
// --------------------------------------------------------------------------------------------------------
// Test Tasks: Ensure we use JUnit when testing
// --------------------------------------------------------------------------------------------------------
tasks.withType<Test> {
useJUnitPlatform()
}
// --------------------------------------------------------------------------------------------------------
// Define repositories used in the app
// --------------------------------------------------------------------------------------------------------
repositories {
mavenCentral()
}
// --------------------------------------------------------------------------------------------------------
// dependencies list
// --------------------------------------------------------------------------------------------------------
dependencies {
implementation("com.beust:jcommander:1.82")
implementation("commons-io:commons-io:2.21.0")
// --- JLine Terminal Access ---
implementation("org.jline:jline-reader:3.30.7")
implementation("org.jline:jline-terminal:3.30.7")
implementation("org.jline:jline-native:3.30.7") // Native support for Linux/Mac/Win
implementation("org.jline:jline-terminal-ffm:3.30.7")
// --- JUnit Testing ---
testImplementation("org.junit.jupiter:junit-jupiter-api:6.1.0-M1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:6.1.0-M1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.1.0-M1")
}
// --------------------------------------------------------------------------------------------------------
// Let Gradle know that to not try and cached the Versions plugin and prevent warnings
// Hopefully this won't be needed with future versions of the plugin
// --------------------------------------------------------------------------------------------------------
tasks.named<com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask>("dependencyUpdates") {
notCompatibleWithConfigurationCache("The versions plugin is not yet compatible with the configuration cache.")
}
// --------------------------------------------------------------------------------------------------------
// Clean Tasks: Define what custom tasks are run with the clean task
// --------------------------------------------------------------------------------------------------------
tasks.clean {
}
// --------------------------------------------------------------------------------------------------------
// ProcessResources Tasks: Update the Java resources with project version and inception date
// --------------------------------------------------------------------------------------------------------
tasks.processResources {
// Before you process resources, update the snapcraft version
dependsOn("updateSnapVersion")
val tokens = mapOf(
"project.version" to project.version.toString(),
// If it can't get the property, default to 2011
"project.inceptionYear" to (project.findProperty("inceptionYear")?.toString() ?: "2011")
)
inputs.properties(tokens)
filesMatching("**/app.properties") {
filter(org.apache.tools.ant.filters.ReplaceTokens::class, "tokens" to tokens)
}
}
// --------------------------------------------------------------------------------------------------------
// shadowJar: Create the fully executable shadowJar (FatJar)
// --------------------------------------------------------------------------------------------------------
tasks.named<ShadowJar>("shadowJar") {
group = "build"
description = "Creates a 'Fat Jar' file containing all dependencies"
archiveFileName.set("${project.name}.jar")
// Ensure we run a test cycle before creating the Shadow Jar
dependsOn("test")
// Merge ServiceLoader files so JLine can find its Terminal providers
mergeServiceFiles()
// Shrink the Shadow Jar by remove unused classes
minimize {
// Don't let the "minifier" remove JLine classes used via reflection
exclude(dependency("org.jline:.*:.*"))
}
// Standard excludes to keep the JAR clean
exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
exclude("META-INF/LICENSE*", "META-INF/NOTICE*", "META-INF/maven/**")
// Always generate the checksums after building the shadowJar
finalizedBy(generateChecksums)
}
// --------------------------------------------------------------------------------------------------------
// Test Tasks: Execute JUnit Tests
// --------------------------------------------------------------------------------------------------------
tasks.test {
useJUnitPlatform()
// This makes the console output much more useful
testLogging {
events("passed", "skipped", "failed")
showStandardStreams = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
// --------------------------------------------------------------------------------------------------------
// install: Copies the Shadow Jar file to the C:\Utils directory after building and testing it
// --------------------------------------------------------------------------------------------------------
tasks.register<Copy>("install") {
group = "distribution"
description = "Builds, tests, and copies the shadowJar to the install directory"
val installDirectory = "C:/Utils"
// This install task depends on shadowJar
val shadowTask = tasks.named<ShadowJar>("shadowJar")
dependsOn(shadowTask)
from(tasks.named("shadowJar"))
into(installDirectory)
// Force Gradle to ignore the cache and copy the file every time
outputs.upToDateWhen { false }
// Capture these here so they are available during execution and Gradle won't throw an error
val progName = project.name
val progVersion = project.version.toString()
doLast {
// Get the file details AFTER the copy has completed
val installedFile = File("$installDirectory/$progName.jar")
val sizeInBytes = installedFile.length()
val lastModifiedTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(installedFile.lastModified()))
println("\n-------------------- RELEASE COMPLETE --------------------")
println("Installed: $progName.jar -> $installDirectory")
println("Version: $progVersion")
println("File Size: ${"%,d".format(sizeInBytes)} bytes")
println("File Date: $lastModifiedTime")
println("----------------------------------------------------------")
}
}
// --------------------------------------------------------------------------------------------------------
// updateSnapVersion: Update application version field in the snap/snapcraft.yaml file
// --------------------------------------------------------------------------------------------------------
tasks.register("updateSnapVersion") {
group = "versioning"
description = "Updates the version in snapcraft.yaml to match the project version"
// Capture the values into local variables OUTSIDE doLast to avoid Gradle warnings
val newVersion = project.version.toString()
val snapFileLocation = layout.projectDirectory.file("snap/snapcraft.yaml").asFile
doLast {
if (!snapFileLocation.exists()) {
throw GradleException("FATAL: snapcraft.yaml not found at ${snapFileLocation.path}. Build aborted.")
}
println("Updating Snapcraft file: ${snapFileLocation.path}")
val content = snapFileLocation.readText()
if (!content.contains(Regex("""(?m)^version:"""))) {
throw GradleException("FATAL: 'version:' key not found in snapcraft.yaml. Check file formatting.")
}
val updatedContent = content.replace(
Regex("""(?m)^version:\s*['"]?.*['"]?"""), "version: '$newVersion'"
)
snapFileLocation.writeText(updatedContent)
println("Successfully updated snapcraft.yaml to version: $newVersion")
}
}
// --------------------------------------------------------------------------------------------------------
// generateChecksums: Generate Checksums automatically during builds in the build/libs directory
// --------------------------------------------------------------------------------------------------------
val generateChecksums by tasks.registering {
group = "distribution"
description = "Generates MD5, SHA-1, and SHA-256 checksums for the shadow JAR"
// Link this task to the shadowJar task by having that as a dependency
val shadowJarTask = tasks.named<ShadowJar>("shadowJar")
dependsOn(shadowJarTask)
// Define Inputs/Outputs for Gradle's "Up-To-Date" check
val archiveFile = shadowJarTask.get().archiveFile.get().asFile
val outputDir = archiveFile.parentFile
inputs.file(archiveFile)
// List the specific files we will create as outputs
outputs.files(
outputDir.resolve("CHECKSUM.MD5"),
outputDir.resolve("CHECKSUM.SHA1"),
outputDir.resolve("CHECKSUM.SHA256")
)
doLast {
val fileName = archiveFile.name
mapOf(
"MD5" to "CHECKSUM.MD5",
"SHA-1" to "CHECKSUM.SHA1",
"SHA-256" to "CHECKSUM.SHA256"
).forEach { (algorithm, outName) ->
val digest = MessageDigest.getInstance(algorithm)
// Efficiently read file and generate hex string
val hash = archiveFile.readBytes().let { bytes ->
digest.digest(bytes).joinToString("") { "%02x".format(it) }
}
val outFile = File(outputDir, outName)
outFile.writeText("$hash $fileName\n")
println("Generated $outName in build/libs")
}
}
}