forked from MovingBlocks/TerasologyLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
620 lines (505 loc) · 21.4 KB
/
build.gradle
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
* Copyright 2019 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This is a Gradle build file:
* - Gradle Homepage: http://gradle.org/
* - Gradle Documentation: http://gradle.org/documentation
* - View tasks for this project: $ gradlew tasks
*/
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath group: "com.alexkasko.delta", name: "delta-diff", version: "1.1.3"
classpath group: 'de.undercouch', name: 'gradle-download-task', version: '2.1.0'
}
}
plugins {
id 'java'
id 'project-report'
id 'idea'
id 'eclipse'
id 'application'
id 'checkstyle'
id 'pmd'
id 'com.github.spotbugs' version '1.6.9'
id 'de.undercouch.download' version '3.4.3'
}
apply from: 'bundles.gradle'
// Test for right version of Java in use for running this script
assert org.gradle.api.JavaVersion.current().isJava8Compatible()
def javaVersion = System.properties['java.version']
// make sure at least 8u40 is used for the build (required for JavaFX)
if (javaVersion.startsWith("1.8") && javaVersion.split("_")[1].toInteger() < 40) {
throw new GradleException("Java version 1.8.0_40 or later required, but was $javaVersion")
}
// gradle wrapper version
wrapper {
gradleVersion '5.4.1'
}
import org.apache.tools.ant.filters.FixCrLfFilter
import java.text.SimpleDateFormat
def dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
dateTimeFormat.timeZone = TimeZone.getTimeZone("UTC")
// Declare "extra properties" (variables) for the project - a Gradle thing that makes them special.
ext {
// Read environment variables, including variables passed by jenkins continuous integration server
env = System.getenv()
// Stuff for our automatic version file setup
startDateTimeString = dateTimeFormat.format(new Date())
versionInfoFileDir = new File(sourceSets.main.output.resourcesDir, 'org/terasology/launcher/version')
versionInfoFile = new File(versionInfoFileDir, 'versionInfo.properties')
versionBase = rootProject.file("version.txt").text.trim()
displayVersion = (env.JOB_NAME == 'TerasologyLauncherStable') ? versionBase : ((env.JOB_NAME != null && env.BUILD_NUMBER != null) ? versionBase + '.' + env.BUILD_NUMBER + '+' + env.JOB_NAME : versionBase + ' local development ' + startDateTimeString)
// Splash image for the JAR
splashImage = "org/terasology/launcher/images/splash.jpg"
}
// Declare remote repositories we're interested in - library files will be fetched from here
repositories {
// Main Maven repo
mavenCentral()
// MovingBlocks Artifactory instance for libs not readily available elsewhere plus our own libs
maven {
url "http://artifactory.terasology.org/artifactory/virtual-repo-live"
}
}
// Primary dependencies definition
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.7'
compile group: 'net.java.dev.jna', name: 'jna', version: '4.1.0'
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.1.0'
compile group: 'org.terasology', name: 'CrashReporter', version: '1.2.+'
compile group: 'com.github.rjeschke', name: 'txtmark', version: '0.11'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2'
// These dependencies are only needed for running tests
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
// Don't upgrade either of these until https://github.com/powermock/powermock/issues/717 is resolved
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'
}
// Set the expected module Java level (can use a higher Java to run, but should not use features from a higher Java)
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = 'org.terasology.launcher.TerasologyLauncher'
// Bundle JRE (Use gradle argument: -PbundleJre=/path/to/jre)
if (project.hasProperty('bundleJre')) {
applicationDistribution.from("$bundleJre") {
into 'jre'
}
startScripts {
doLast {
unixScript.text = unixScript.text.replaceFirst(/(?s)# Determine the Java.*(?=\R# Increase)/,
'# Use bundled JRE\nJAVACMD=\\$APP_HOME/jre/bin/java\n')
windowsScript.text = windowsScript.text.replaceFirst(/(?s)@rem Find java.*:init/,
'@rem Use bundled JRE\nset JAVA_EXE=' + /%APP_HOME%\\jre\\bin\\java.exe/ + '\n')
}
}
}
checkstyle {
toolVersion = '6.14.1'
ignoreFailures = true
configProperties.samedir = checkstyle.configFile.parentFile
}
pmd {
toolVersion = '5.5.2'
ignoreFailures = true
ruleSetFiles = files("config/pmd/pmd.xml")
}
spotbugs {
toolVersion = '3.1.11'
ignoreFailures = true
effort = 'max'
reportLevel = 'medium'
}
test {
ignoreFailures = true
}
def convertGitBranch = { gitBranch ->
if (gitBranch != null) {
// Remove "origin/" from "origin/develop"
gitBranch.substring(gitBranch.lastIndexOf("/") + 1)
} else {
""
}
}
task createVersionInfoFile {
inputs.property('dateTime', startDateTimeString)
onlyIf { env.BUILD_URL != null }
doLast {
versionInfoFileDir.mkdirs()
ant.propertyfile(file: versionInfoFile) {
ant.entry(key: 'buildNumber', value: env.BUILD_NUMBER)
ant.entry(key: 'buildId', value: env.BUILD_ID)
ant.entry(key: 'buildTag', value: env.BUILD_TAG)
ant.entry(key: 'buildUrl', value: env.BUILD_URL)
ant.entry(key: 'jobName', value: env.JOB_NAME)
ant.entry(key: 'gitBranch', value: convertGitBranch(env.GIT_BRANCH))
ant.entry(key: 'gitCommit', value: env.GIT_COMMIT)
ant.entry(key: 'dateTime', value: startDateTimeString)
ant.entry(key: 'displayVersion', value: displayVersion)
}
}
}
createVersionInfoFile.dependsOn processResources
jar.dependsOn createVersionInfoFile
distZip.dependsOn createVersionInfoFile
distTar.dependsOn createVersionInfoFile
jar {
// replace development "logback.xml" with productive "logback_jar.xml"
exclude "logback.xml"
rename('logback_jar.xml', 'logback.xml')
manifest {
def manifestClasspath = configurations.runtime.collect { it.getName() }.join(" ")
attributes("Main-Class": mainClassName)
attributes("Class-Path": manifestClasspath)
attributes("Implementation-Title": project.name)
attributes("Implementation-Version": versionBase) // Use base version only to avoid re-creating identical jar
attributes("SplashScreen-Image": splashImage)
// allow everything
attributes('Permissions': 'all-permissions')
attributes('Codebase': '*')
attributes('Application-Name': project.name)
attributes('Application-Library-Allowable-Codebase': '*')
attributes('Caller-Allowable-Codebase': '*')
attributes('Trusted-Only': 'false')
}
}
task copyLibs(type: IncrementalCopy) {
description = "Copy all files to the distributions folder"
dependsOn jar
from configurations.runtime // these are the runtime-time dependencies
from jar // this is the launcher jar file
into('icons') { // splash screen, etc
from 'icons'
}
into "$buildDir/distributions"
}
class IncrementalCopy extends Copy {
@TaskAction
void execute(IncrementalTaskInputs inputs) {
inputs.outOfDate { change ->
println "Copying: ${change.file.name}"
}
inputs.removed { change ->
println "Removed: ${change.file.name}"
}
}
}
// Sign the jar files so they can get full rights in a browser
task signLibs(type: IncrementalSigningTask, dependsOn: copyLibs) {
FileTree tree = fileTree(dir: "$buildDir/distributions")
tree.include '*.jar'
inputs.files tree
// assign only if the property exists
// --> missing properties do not affect other tasks if not needed
certFile = project.hasProperty('certificateFile') ? certificateFile : 'MISSING'
certAlias = project.hasProperty('certificateAlias') ? certificateAlias : 'MISSING'
certStorePass = project.hasProperty('certificateStorePass') ? certificateStorePass : 'MISSING'
certKeyPass = project.hasProperty('certificateKeyPass') ? certificateKeyPass : 'MISSING'
doFirst {
if (!project.hasProperty('certificateFile')) {
throw new GradleException('Please set certificate properties in your ~/.gradle/gradle.properties file first')
}
}
outputDir = file("$buildDir/distributions/signed")
}
class IncrementalSigningTask extends DefaultTask {
@Input
String certFile
@Input
String certAlias
@Input
String certStorePass
@Input
String certKeyPass
@OutputDirectory
File outputDir
@TaskAction
void execute(IncrementalTaskInputs inputs) {
outputDir.mkdirs()
inputs.outOfDate { change ->
logger.lifecycle("Signing ${change.file.name}")
def target = new File(outputDir, change.file.name)
ant.signjar(
jar: change.file,
signedjar: target,
keystore: certFile,
alias: certAlias,
storepass: certStorePass,
keypass: certKeyPass,
tsaurl: 'http://time.certum.pl/')
}
inputs.removed { change ->
println "removed: ${change.file.name}"
}
}
}
task updateJnlp(dependsOn: signLibs) {
inputs.file 'webstart-template.jnlp'
outputs.file "$buildDir/distributions/webstart.jnlp"
doFirst {
def mainJar = 'TerasologyLauncher.jar'
def root = new XmlParser().parse('webstart-template.jnlp')
root.@codebase = (env.BUILD_URL != null) ? 'http://jenkins.terasology.org/job/' + env.JOB_NAME + '/lastSuccessfulBuild/artifact/build/distributions/' : '.'
// Adding signed jars to resources
def resources = root.resources[0]
def collection = files { file("$buildDir/distributions/signed").listFiles() }
collection.each {
def resource = [href: "signed/$it.name"]
if (mainJar == it.name) {
resource = [href: "signed/$it.name", main: 'true']
}
resources.appendNode('jar', resource)
}
// Writing the jnlp file filled
File jnlpFile = new File("$buildDir/distributions/webstart.jnlp")
new XmlNodePrinter(new PrintWriter(jnlpFile)).print(root)
}
}
task copyExtra(type: Copy) {
filter(FixCrLfFilter, eol: FixCrLfFilter.CrLf.newInstance("crlf"))
from('README.md') {
rename('README.md', 'README.txt')
}
from('CHANGELOG.md') {
rename('CHANGELOG.md', 'CHANGELOG.txt')
}
from('CONTRIBUTING.md') {
rename('CONTRIBUTING.md', 'CONTRIBUTING.txt')
}
from('LICENSE')
from('NOTICE')
from('src/template/VERSION.md') {
expand(buildNumber: env.BUILD_NUMBER, buildUrl: env.BUILD_URL, gitBranch: convertGitBranch(env.GIT_BRANCH),
dateTime: startDateTimeString, displayVersion: displayVersion)
rename('VERSION.md', 'VERSION.txt')
}
into "$buildDir/distributions"
}
task copyExtraIntoResources(type: Copy) {
from('README.md')
from('CHANGELOG.md')
from('CONTRIBUTING.md')
from('LICENSE')
from('NOTICE')
into('src/main/resources/org/terasology/launcher/about')
}
processResources.dependsOn copyExtraIntoResources
task copyIconsIntoResources(type: Copy) {
from('icons')
into('src/main/resources/org/terasology/launcher/icons')
}
processResources.dependsOn copyIconsIntoResources
task copyHtml(type: Copy) {
from('index.html')
into "$buildDir/distributions"
}
// TODO: Temporary workaround for new exes / JRE bundling not quite being officially ready yet
distZip.exclude("*.x*.exe")
task createRelease() {
dependsOn distZip
doLast {
println 'Created release: ' + displayVersion
}
}
task webstart {
description "Prepare WebStart launcher files"
dependsOn signLibs
dependsOn copyExtra
dependsOn createVersionInfoFile
dependsOn updateJnlp
dependsOn copyHtml
doLast {
println 'Created webstart release: ' + displayVersion
}
}
task createPatch(dependsOn: distZip) {
description = 'Create a delta patch file w.r.t. last successful build of given JOB_NAME (must be set)'
if (env.JOB_NAME == null) {
logger.info("No JOB_NAME given, skipping ...")
return
}
doLast {
File patchDir = new File(buildDir, 'patch/')
File distDir = new File(buildDir, 'distributions/')
String lastSuccessfulBuildNumber = "http://jenkins.terasology.org/job/$env.JOB_NAME/lastSuccessfulBuild/buildNumber".toURL().getText()
String url = "http://jenkins.terasology.org/job/$env.JOB_NAME/lastSuccessfulBuild/artifact/build/distributions/TerasologyLauncher.zip"
File oldZip = new File(patchDir, 'old.zip')
File newZip = new File(distDir, 'TerasologyLauncher.zip')
// Download the last successful build artifact for the current job
download {
onlyIfNewer true
src url
dest oldZip
}
// Unzip the last successful build into the 'old' directory
copy {
from zipTree(oldZip)
into new File(patchDir, 'old/')
}
// Unzip the result of this build into the 'new' directory
copy {
from zipTree(newZip)
into new File(patchDir, 'new/')
}
File oldUnzipped = new File(patchDir, 'old/TerasologyLauncher/')
File newUnzipped = new File(patchDir, 'new/TerasologyLauncher/')
String patchFileName = "$project.name-patch-$env.JOB_NAME-$lastSuccessfulBuildNumber-$env.BUILD_NUMBER"+".zip"
File patchZip = new File(patchDir, patchFileName)
logger.info("Creating patch file with: \n\told : $oldUnzipped\n\tnew : $newUnzipped\n\tpatch: $patchZip")
new com.alexkasko.delta.DirDeltaCreator().create(oldUnzipped, newUnzipped, patchZip)
// Copy the patch file artifact to the 'distributions' directory
copy {
from patchZip
into distDir
}
println "Created patch: $patchFileName"
}
}
// Setup IntelliJ-IDEA
idea {
project {
jdkName = '1.8'
languageLevel = 'JDK_1_8'
ipr {
withXml { xmlProvider ->
def iprNode = xmlProvider.asNode()
ideaActivateGradle(iprNode)
ideaActivateGit(iprNode)
ideaActivateCheckstyle(iprNode)
ideaActivateCopyright(iprNode)
}
}
}
module {
// Exclude Gradle dir
excludeDirs += file('gradle')
// Exclude Eclipse dirs
excludeDirs += file('bin')
excludeDirs += file('.settings')
}
workspace {
iws {
withXml { xmlProvider ->
def iwsNode = xmlProvider.asNode()
ideaMakeAutomatically(iwsNode)
ideaRunConfig(iwsNode)
}
}
}
}
ext {
// Activate 'Gradle' plugin
ideaActivateGradle = { Node iprNode ->
def gradleSettings = iprNode.component.find { it.'@name' == 'GradleSettings' }
if (gradleSettings == null) {
gradleSettings = iprNode.appendNode('component', [name: 'GradleSettings'])
gradleSettings.appendNode('option', [name: 'linkedProjectPath', value: '$PROJECT_DIR$/build.gradle'])
}
}
// Activate 'git' as VCS
ideaActivateGit = { Node iprNode ->
def vcsMappings = iprNode.component.find { it.'@name' == 'VcsDirectoryMappings' }
vcsMappings.mapping.@vcs = 'Git'
}
// Activate and config 'Checkstyle' plugin
ideaActivateCheckstyle = { Node iprNode ->
def checkstyle = iprNode.component.find { it.'@name' == 'CheckStyle-IDEA' }
if (checkstyle == null) {
// Create new CheckStyle component
checkstyle = iprNode.appendNode('component', [name: 'CheckStyle-IDEA'])
// use NodeBuilder to create the config block in the xml structure
def builder = new NodeBuilder()
def option = builder.option(name: 'configuration') {
map {
entry(key: 'active-configuration',
value: 'PROJECT_RELATIVE:$PROJECT_DIR$/config/checkstyle/checkstyle.xml:TerasologyLauncher CheckStyle')
entry(key: 'check-nonjava-files', value: false)
entry(key: 'check-test-classes', value: true)
entry(key: 'location-0',
value: 'CLASSPATH:/sun_checks.xml:The default CheckStyle rules')
entry(key: 'location-1',
value: 'PROJECT_RELATIVE:$PROJECT_DIR$/config/checkstyle/checkstyle.xml:TerasologyLauncher CheckStyle')
entry(key: 'property-1.samedir', value: 'config/checkstyle')
entry(key: 'suppress-errors', value: false)
entry(key: 'thirdparty-classpath', value: '')
}
}
// Add result from NodeBuilder
checkstyle.append option
}
}
// Activate copyright conventions
ideaActivateCopyright = { Node iprNode ->
def copyrightManager = iprNode.component.find { it.'@name' == 'CopyrightManager' }
copyrightManager.'@default' = "TerasologyLauncher"
def copyright = copyrightManager.copyright.find { it.option.find { it.'@name' == "myName" }?.'@value' == "TerasologyLauncher" }
if (copyright == null) {
copyrightManager.append(new XmlParser().parseText('''
<copyright>
<option name="notice" value="Copyright 2016 MovingBlocks Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." />
<option name="keyword" value="Copyright" />
<option name="allowReplaceKeyword" value="" />
<option name="myName" value="TerasologyLauncher" />
<option name="myLocal" value="true" />
</copyright>
'''))
}
}
// Enable "make project automatically"
ideaMakeAutomatically = { Node iwsNode ->
def compilerWsConf = iwsNode.find { it.'@name' == 'CompilerWorkspaceConfiguration' }
if (compilerWsConf == null) {
compilerWsConf = iwsNode.appendNode('component', [name: 'CompilerWorkspaceConfiguration'])
compilerWsConf.appendNode("option", [name: "MAKE_PROJECT_ON_SAVE", value: "true"])
}
}
// Generate a run configuration for the primary PC facade way of running the game
ideaRunConfig = { Node iwsNode ->
// Adds our run config
def runManager = iwsNode.component.find { it.'@name' == 'RunManager' }
runManager.append(new XmlParser().parseText('''
<configuration default="false" name="TerasologyLauncher" type="Application" factoryName="Application" singleton="true">
<extension name="coverage" enabled="false" merge="false" />
<option name="MAIN_CLASS_NAME" value="org.terasology.launcher.TerasologyLauncher" />
<option name="VM_PARAMETERS" value="-splash:src/main/resources/org/terasology/launcher/images/splash.jpg" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name='WORKING_DIRECTORY' value='file://\$PROJECT_DIR\$' />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" value="" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="TerasologyLauncher" />
<envs />
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Run" />
<method />
</configuration>
'''))
// This part puts the added run config into the actual "Run" drop-down
runManager.append(new XmlParser().parseText('''
<list size="1">
<item index="0" class="java.lang.String" itemvalue="Application.TerasologyLauncher"/>
</list>
'''))
// Makes the added run config the selected item
runManager.'@selected' = 'Application.TerasologyLauncher'
}
}