-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.gradle
More file actions
456 lines (391 loc) · 14 KB
/
build.gradle
File metadata and controls
456 lines (391 loc) · 14 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* PerlOnJava Build Configuration
* This Gradle build script configures the build process for the PerlOnJava project
*/
buildscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
}
// Core plugins configuration
plugins {
id 'java'
// Plugin for updating version catalog with latest versions (configuration cache compatible!)
alias(libs.plugins.version.catalog.update)
id 'application'
// Plugin for creating OS packages (deb)
alias(libs.plugins.ospackage)
// Plugin for creating fat/uber JARs
alias(libs.plugins.shadow)
// Plugin for generating CycloneDX SBOM (Software Bill of Materials)
alias(libs.plugins.cyclonedx)
}
// Main application class configuration
application {
mainClass = 'org.perlonjava.app.cli.Main'
}
// Debian package build dependency
tasks.buildDeb {
dependsOn installDist
}
// Copy custom wrapper scripts to installDist bin directory
tasks.register('copyWrapperScripts', Copy) {
dependsOn installDist
from(projectDir) {
include 'jperl'
include 'jperl.bat'
include 'jcpan'
include 'jcpan.bat'
include 'jperldoc'
include 'jperldoc.bat'
include 'jprove'
include 'jprove.bat'
}
into "${buildDir}/install/perlonjava/bin"
}
// Copy Perl bin scripts (cpan, perldoc, prove) to installDist bin directory
tasks.register('copyPerlBinScripts', Copy) {
dependsOn installDist
from('src/main/perl/bin') {
include 'cpan'
include 'perldoc'
include 'prove'
}
into "${buildDir}/install/perlonjava/bin"
}
// Make buildDeb depend on both copy tasks
tasks.buildDeb {
dependsOn copyWrapperScripts
dependsOn copyPerlBinScripts
}
// Project metadata
group = 'org.perlonjava'
version = '5.42.0'
// CycloneDX SBOM generation configuration
cyclonedxBom {
projectType = "application"
schemaVersion = "1.5"
includeLicenseText = false
includeBomSerialNumber = true
outputName = "bom"
outputFormat = "all" // Generate both JSON and XML
componentName = "perlonjava"
componentVersion = project.version
organizationalEntity { oe ->
oe.name = "PerlOnJava Project"
oe.urls = ["https://github.com/fglock/PerlOnJava"]
}
}
// Git info injection - injects commit ID and date into Configuration.java before compilation
// This ensures the built JAR contains accurate version information for -v output
def configFilePath = layout.projectDirectory.file('src/main/java/org/perlonjava/core/Configuration.java')
tasks.register('injectGitInfo') {
description = 'Injects git commit info into Configuration.java'
group = 'build'
// Declare inputs/outputs for configuration cache compatibility
def configFile = configFilePath.asFile
doLast {
if (!configFile.exists()) {
logger.warn("Configuration.java not found, skipping git info injection")
return
}
// Get git commit info using Runtime.exec
def gitCommitId = 'dev'
def gitCommitDate = 'unknown'
try {
def commitIdProcess = ['git', 'rev-parse', '--short', 'HEAD'].execute()
commitIdProcess.waitFor()
if (commitIdProcess.exitValue() == 0) {
gitCommitId = commitIdProcess.text.trim()
}
def commitDateProcess = ['git', 'log', '-1', '--format=%cs', 'HEAD'].execute()
commitDateProcess.waitFor()
if (commitDateProcess.exitValue() == 0) {
gitCommitDate = commitDateProcess.text.trim()
}
} catch (Exception e) {
logger.warn("Could not get git info: ${e.message}")
}
// Only update if we got valid values
if (gitCommitId && gitCommitId != 'dev') {
def content = configFile.text
// Use safe pattern matching for quoted string values
content = content.replaceAll(
/(gitCommitId\s*=\s*)"[^"]*"/,
"\$1\"${gitCommitId}\""
)
content = content.replaceAll(
/(gitCommitDate\s*=\s*)"[^"]*"/,
"\$1\"${gitCommitDate}\""
)
// Generate build timestamp in Perl 5 "Compiled at" format: "Mon DD YYYY HH:MM:SS"
def now = new java.util.Date()
def buildTimestamp = new java.text.SimpleDateFormat("MMM dd yyyy HH:mm:ss", java.util.Locale.ENGLISH).format(now)
// Perl uses single-digit day with leading space (e.g., "Apr 7" not "Apr 07")
buildTimestamp = buildTimestamp.replaceAll(/^(\w{3}) 0/, '$1 ')
content = content.replaceAll(
/(buildTimestamp\s*=\s*)"[^"]*"/,
"\$1\"${buildTimestamp}\""
)
configFile.text = content
logger.lifecycle("Injected git info: ${gitCommitId} (${gitCommitDate})")
}
}
}
// Make compilation depend on git info injection
tasks.named('compileJava') {
dependsOn 'injectGitInfo'
}
// OS package configuration for Debian packaging
ospackage {
packageName = 'perlonjava'
version = project.version
maintainer = 'Flavio Soibelmann Glock <fglock@gmail.com>'
// Java 22+ is required at runtime (any distribution: Oracle, Azul, Temurin, OpenJDK, etc.)
into '/opt/perlonjava'
from('build/install/perlonjava') {
into '/opt/perlonjava'
}
// Include combined SBOM in the package
from('build/reports') {
into '/opt/perlonjava/share/sbom'
include 'sbom.json'
}
link('/usr/local/bin/jperl', '/opt/perlonjava/bin/jperl')
link('/usr/local/bin/jcpan', '/opt/perlonjava/bin/jcpan')
link('/usr/local/bin/jperldoc', '/opt/perlonjava/bin/jperldoc')
link('/usr/local/bin/jprove', '/opt/perlonjava/bin/jprove')
}
// Java toolchain configuration - requires Java 22 (for FFM API)
java {
toolchain {
languageVersion = JavaLanguageVersion.of(22)
}
}
// Repository configuration
repositories {
mavenCentral()
}
// Project dependencies
dependencies {
// Core dependencies
implementation libs.asm // ByteCode manipulation
implementation libs.asm.util // ASM utilities
implementation libs.icu4j // Unicode support
implementation libs.fastjson2 // JSON processing
implementation libs.snakeyaml.engine // YAML processing
implementation libs.tomlj // TOML processing
implementation libs.commons.csv // CSV processing
implementation libs.sqlite.jdbc // SQLite JDBC driver
// JNR-POSIX removed - using Java FFM API for native access (Java 22+)
// Testing dependencies
testImplementation libs.junit.jupiter.api
testImplementation libs.junit.jupiter.engine
testImplementation libs.junit.jupiter.params
}
// JUnit configuration
testing {
suites {
test {
useJUnitJupiter()
}
}
}
// Java compilation settings
tasks.withType(JavaCompile).configureEach {
options.compilerArgs << '-Xlint:-options'
options.compilerArgs << '-Xlint:deprecation'
}
// Test execution configuration with native access
tasks.withType(Test).configureEach {
jvmArgs += '--enable-native-access=ALL-UNNAMED'
}
// Enable native access for all Java execution tasks
allprojects {
tasks.withType(JavaExec).configureEach {
jvmArgs += '--enable-native-access=ALL-UNNAMED'
}
}
// JUnit platform configuration - default test task runs only unit tests
test {
useJUnitPlatform {
includeTags 'unit'
excludeTags 'full'
}
}
// Fast unit tests only (tests in unit/ directory)
tasks.register('testUnit', Test) {
description = 'Runs fast unit tests only (from unit/ directory)'
group = 'verification'
useJUnitPlatform {
includeTags 'unit'
excludeTags 'full'
}
shouldRunAfter test
}
// All tests including comprehensive module tests
tasks.register('testAll', Test) {
description = 'Runs all tests including comprehensive module tests'
group = 'verification'
useJUnitPlatform {
includeTags 'full'
}
shouldRunAfter testUnit
}
// Bundled module tests (XML::Parser, etc.)
// Tests live under src/test/resources/module/{ModuleName}/t/
tasks.register('testModule', Test) {
description = 'Runs bundled CPAN module tests (e.g. XML::Parser)'
group = 'verification'
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
useJUnitPlatform {
includeTags 'module'
}
shouldRunAfter testUnit
}
// Shadow JAR configuration for creating standalone executable
shadowJar {
archiveClassifier.set('')
destinationDirectory = file("$buildDir/../target")
manifest {
attributes 'Main-Class': 'org.perlonjava.app.cli.Main'
}
exclude 'module-info.class'
exclude 'META-INF/MANIFEST.MF'
// Include combined SBOM in JAR's META-INF/sbom/ directory
from("$buildDir/reports/sbom.json") {
into 'META-INF/sbom'
}
}
// Task to generate Perl SBOM
tasks.register('generatePerlSbom', Exec) {
description = 'Generate SBOM for bundled Perl modules'
group = 'sbom'
workingDir = projectDir
commandLine 'bash', '-c',
"mkdir -p build/reports && perl dev/tools/generate-perl-sbom.pl > build/reports/perl-bom.json"
// Declare output so Gradle waits for completion
outputs.file("build/reports/perl-bom.json")
}
// Task to merge Java and Perl SBOMs
tasks.register('mergeSbom', Exec) {
description = 'Merge Java and Perl SBOMs into combined SBOM'
group = 'sbom'
dependsOn cyclonedxBom, generatePerlSbom
// Declare inputs so Gradle knows dependencies
inputs.file("build/reports/bom.json")
inputs.file("build/reports/perl-bom.json")
workingDir = projectDir
commandLine 'bash', '-c',
"perl dev/tools/merge-sbom.pl build/reports/bom.json build/reports/perl-bom.json > build/reports/sbom.json"
// Declare output
outputs.file("build/reports/sbom.json")
}
// Ensure combined SBOM is generated before shadowJar
shadowJar.dependsOn mergeSbom
// Make shadowJar part of the build process
tasks.named('build') {
dependsOn shadowJar
}
// Source sets configuration for including Perl resources
sourceSets {
main {
resources {
srcDir 'src/main/perl'
srcDir 'src/main/resources'
include '**/*.pm'
include '**/*.pl'
include '**/*.ph'
include '**/*.pod'
include '**/*.dd'
include '**/*.yml'
include '**/media.types'
include 'lib/ExtUtils/xsubpp'
include 'bin/**'
include 'META-INF/services/**'
}
}
test {
resources {
srcDir 'src/test/resources'
}
}
}
// Resource processing configuration
tasks.named('processResources', Copy) {
from(sourceSets.main.resources.srcDirs) {
include '**/*.pm'
include '**/*.ph'
include '**/*.pod'
include '**/*.dd'
include '**/*.yml'
include '**/media.types'
include 'bin/**'
include 'META-INF/services/**'
}
into("$buildDir/resources/main")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
// Test resource processing configuration
tasks.named('processTestResources', Copy) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
// Parallel test execution tasks
// Run with: ./gradlew testUnitParallel --parallel
def parallelShards = 4
(0..<parallelShards).each { index ->
tasks.register("testUnitShard${index}", Test) {
group = 'verification'
description = "Runs shard ${index} of ${parallelShards} of unit tests"
useJUnitPlatform {
includeTags 'unit'
excludeTags 'full'
}
systemProperty 'test.shard.index', index
systemProperty 'test.shard.total', parallelShards
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
}
}
tasks.register('testUnitParallel') {
group = 'verification'
description = 'Runs unit tests in parallel across multiple JVMs. Usage: gradle testUnitParallel --parallel'
dependsOn 'testUnitShard0', 'testUnitShard1', 'testUnitShard2', 'testUnitShard3'
}
// Version catalog update configuration
// The nl.littlerobots.version-catalog-update plugin is configuration cache compatible!
// Use: ./gradlew versionCatalogUpdate to check and update dependencies
// Use: ./gradlew versionCatalogUpdate --interactive to review changes interactively
versionCatalogUpdate {
// Sort the catalog keys
sortByKey = true
// Keep versions not used in the project
keep {
keepUnusedVersions = true
}
// Pin cyclonedx plugin to current 2.x version.
//
// cyclonedx 3.x is NOT compatible with this build for four reasons:
// 1. schemaVersion changed from string "1.5" to enum "VERSION_15"
// 2. outputName/outputFormat properties were removed; output is now
// configured via jsonOutput/xmlOutput file properties, and the
// default path moved from build/reports/bom.json to
// build/reports/cyclonedx/bom.json (breaks the mergeSbom task)
// 3. organizationalEntity closure API was removed from the aggregate
// task (CyclonedxAggregateTask)
// 4. Gradle 9.x triggers "No XmlService implementation found" at
// runtime due to a missing Maven API dependency in the plugin
//
// To unpin: upgrade Gradle first, then adapt the cyclonedxBom block
// and mergeSbom input path above to the 3.x API. See:
// https://github.com/CycloneDX/cyclonedx-gradle-plugin (v3 README)
//
// Pin shadow to 9.3.x - shadow 9.4.x introduces a classpath conflict
// that triggers "No XmlService implementation found" from the cyclonedx
// plugin. Unpin after cyclonedx is upgraded to 3.x.
pin {
plugins = [libs.plugins.cyclonedx, libs.plugins.shadow]
}
}