-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
232 lines (200 loc) Β· 9.08 KB
/
build.gradle.kts
File metadata and controls
232 lines (200 loc) Β· 9.08 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
/**
* To build Robocode, you need to run this command:
* ./gradlew build
*
* To clean the build, to run this command:
* ./gradlew clean
*
* To run Robocode you need to build Robocode and do a `cd .sandbox` and write `robocode.bat` or `robocode.sh`
*
* The distribution file for Robocode is put into /build and named `robocode.x.x.x.x-setup.jar`
* The x.x.x.x is a version number like e.g. 1.9.5.4
*
* You can run the distribution file with hava like this:
* java -jar robocode.x.x.x.x-setup.jar
*/
plugins {
`java-library`
idea
alias(libs.plugins.nexus.publish)
alias(libs.plugins.ben.manes.versions)
}
description = "Robocode - Build the best - destroy the rest!"
val ossrhUsername: String by project
val ossrhPassword: String by project
subprojects {
apply(plugin = "java")
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8)) // Java 8
}
}
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
}
tasks {
named("clean") {
delete(".sandbox")
}
val validateReleaseCredentials by registering {
group = "publishing"
description = "Pre-flight credential checks required for release"
doLast {
val signingKey = project.findProperty("signingKey") as String?
val signingPassword = project.findProperty("signingPassword") as String?
val ossrhUser = project.findProperty("ossrhUsername") as String?
val ossrhPass = project.findProperty("ossrhPassword") as String?
val chocoKey = System.getenv("CHOCOLATEY_API_KEY")
val onWindows = System.getProperty("os.name").lowercase().contains("windows")
val labels = mutableListOf<String>()
val results = mutableListOf<Boolean>()
fun check(label: String, value: String?, rejectDummy: Boolean = true) {
labels.add(label)
results.add(!value.isNullOrBlank() && (!rejectDummy || value != "dummy"))
}
val ghOk = ProcessBuilder("gh", "auth", "status")
.redirectErrorStream(true).start().waitFor() == 0
val expectedTag = "v${project.version}"
val gitTagProc = ProcessBuilder("git", "tag", "--points-at", "HEAD")
.redirectErrorStream(true).start()
val tagOk = gitTagProc.inputStream.bufferedReader().readLines()
.any { it.trim() == expectedTag }
.also { gitTagProc.waitFor() }
check("signingKey", signingKey)
check("signingPassword", signingPassword, rejectDummy = false)
check("ossrhUsername", ossrhUser)
check("ossrhPassword", ossrhPass)
if (onWindows) check("CHOCOLATEY_API_KEY (env var)", chocoKey, rejectDummy = false)
println("\nπ Credential check:")
labels.zip(results).forEach { (label, ok) -> println(" ${if (ok) "β
" else "β"} $label") }
if (!onWindows) println(" βοΈ CHOCOLATEY_API_KEY (skipped β Chocolatey publish requires Windows)")
println(" ${if (ghOk) "β
" else "β"} GitHub CLI (gh auth status)")
println(" ${if (tagOk) "β
" else "β"} git tag $expectedTag on HEAD")
println()
val failures = labels.zip(results).filter { !it.second }.map { it.first } +
listOfNotNull(
if (!ghOk) "GitHub CLI (gh auth status)" else null,
if (!tagOk) "git tag $expectedTag not found on HEAD β run: git tag $expectedTag" else null
)
if (failures.isNotEmpty()) {
throw GradleException(
"Release pre-flight failed β missing or invalid credentials:\n" +
failures.joinToString("\n") { " β $it" } +
"\n\nSee release.md β Prerequisites for setup instructions."
)
}
println("β
All credentials present. Starting release...\n")
}
}
val createGitHubRelease by registering(Exec::class) {
group = "publishing"
description = "Creates a GitHub release and attaches the setup JAR"
val version = project.version.toString()
commandLine(
"gh", "release", "create", "v$version",
"--title", "Robocode $version",
"--notes", "See [versions.md](https://github.com/robo-code/robocode/blob/main/versions.md) for release notes.",
"build/robocode-$version-setup.jar"
)
}
register("release") {
group = "publishing"
description = "Full release: pre-flight checks, build, Maven Central, Chocolatey (Windows), GitHub Release"
dependsOn(validateReleaseCredentials)
dependsOn("build")
dependsOn(createGitHubRelease)
// Per-subproject publish tasks, closeAndRelease, and ordering are wired in projectsEvaluated below
}
}
// Use projectsEvaluated (runs after ALL afterEvaluate callbacks, including the nexus plugin's)
// so that per-subproject publishAllPublicationsToSonatypeRepository tasks are already registered.
gradle.projectsEvaluated {
val releaseTask = tasks.named("release")
val validateCreds = tasks.named("validateReleaseCredentials")
tasks.named("build") { mustRunAfter(validateCreds) }
val publishTasks = subprojects
.mapNotNull { it.tasks.findByName("publishAllPublicationsToSonatypeRepository") }
publishTasks.forEach { publishTask ->
releaseTask.configure { dependsOn(publishTask) }
publishTask.mustRunAfter("build")
}
tasks.findByName("closeAndReleaseSonatypeStagingRepository")?.let { closeTask ->
releaseTask.configure { dependsOn(closeTask) }
closeTask.mustRunAfter(*publishTasks.toTypedArray())
}
val onWindows = System.getProperty("os.name").lowercase().contains("windows")
val chocoKey = System.getenv("CHOCOLATEY_API_KEY")
val closeTask = tasks.findByName("closeAndReleaseSonatypeStagingRepository")
val ghRelease = tasks.named("createGitHubRelease")
var lastBeforeGhRelease: Any = closeTask ?: "build"
if (onWindows && !chocoKey.isNullOrBlank()) {
project(":robocode.installer").tasks.findByName("chocoPush")?.let { chocoPush ->
releaseTask.configure { dependsOn(chocoPush) }
if (closeTask != null) chocoPush.mustRunAfter(closeTask)
lastBeforeGhRelease = chocoPush
}
}
ghRelease.configure { mustRunAfter(lastBeforeGhRelease) }
}
nexusPublishing {
repositories {
sonatype {
// Publishing By Using the Portal OSSRH Staging API:
// https://central.sonatype.org/publish/publish-portal-ossrh-staging-api/
nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
stagingProfileId.set("c7f511545ccf8")
username.set(ossrhUsername)
password.set(ossrhPassword)
}
}
}
val initializeSonatypeStagingRepository by tasks.existing
subprojects {
initializeSonatypeStagingRepository {
shouldRunAfter(tasks.withType<Sign>())
}
// Configure signing for all subprojects with both signing and maven-publish plugins
plugins.withId("maven-publish") {
plugins.withId("signing") {
the<SigningExtension>().apply {
val signingKey: String? by project
val signingPassword: String? by project
if (!signingKey.isNullOrBlank()) {
useInMemoryPgpKeys(signingKey, signingPassword)
}
// Make signing required only when a key is provided
isRequired = !signingKey.isNullOrBlank()
if (isRequired) {
sign(the<PublishingExtension>().publications)
}
}
}
}
// Include robocode.ico in the published artifacts without cross-project output conflicts
// We copy the icon into a subproject-local build directory, so each module signs its own copy.
plugins.withId("maven-publish") {
val prepareRobocodeIcon = tasks.register<Copy>("prepareRobocodeIcon") {
val srcIcon = file("${rootProject.projectDir}/robocode.content/src/main/resources/robocode.ico")
val destDir = layout.buildDirectory.dir("publication-resources/icon").get().asFile
from(srcIcon)
into(destDir)
outputs.file(file("${destDir}/robocode.ico"))
}
configure<PublishingExtension> {
publications.withType<MavenPublication> {
val copiedIcon = layout.buildDirectory.file("publication-resources/icon/robocode.ico").get().asFile
artifact(copiedIcon) {
builtBy(prepareRobocodeIcon)
classifier = "icon"
extension = "ico"
}
}
}
// Ensure sign tasks depend on icon preparation
tasks.withType<Sign>().configureEach {
dependsOn(prepareRobocodeIcon)
}
}
}