-
Notifications
You must be signed in to change notification settings - Fork 12
/
build.gradle
913 lines (834 loc) · 30.9 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/**
* multiproject-git-gradle/build.gradle
*
* Gradle script automating git-clone/pull and compilation of multiple
* inter-connected projects.
*
* Written by Andrey Hihlovskiy (akhikhl AT gmail DOT com).
* Licensed under the MIT (http://opensource.org/licenses/MIT).
*
* @author Andrey Hihlovskiy
* @version 1.0.36
* @requires gradle 1.11+, git 1.8+
*
* Full documentation and sources at:
* https://github.com/akhikhl/multiproject-git-gradle
**/
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies { classpath 'org.ajoberstar:gradle-git:0.8.0' }
}
import org.ajoberstar.grgit.Grgit
import java.util.regex.Matcher
boolean isString(x) {
x instanceof String || x instanceof GString
}
def isCollection(obj) {
return [Collection, Object[]].any { it.isAssignableFrom(obj.getClass()) }
}
def toCollection(obj) {
return isCollection(obj) ? obj : [ obj ]
}
class Project extends Expando {
Project() {
}
Project(Map properties) {
super(properties)
}
void newVersion(String regex, Closure closure) {
if(newVersion == null)
newVersion = [:]
newVersion[regex] = closure
}
void releaseVersion(String regex, Closure closure) {
if(releaseVersion == null)
releaseVersion = [:]
releaseVersion[regex] = closure
}
}
class MultiProjectExtension {
String rootFolder
def projects = [:]
String gitBase
String gitNameSeparator = '/'
String gitNameSuffix = '.git'
def releaseVersion = [ /(\d+)([^\d]*$)/ : { m -> m.replaceAll(m[0][1]) } ]
def newVersion = [ /(\d+)([^\d]*$)/ : { m -> m.replaceAll("${ (m[0][1] as int) + 1 }${ m[0][2] }") } ]
Boolean releaseNoCommit
Boolean releaseNoPush
String branch = 'master'
Boolean failOnSnapshotDependencies
List deployTasks = []
Boolean skipTests
def git(Map options = [:], Closure closure) {
def savedGitBase = gitBase
gitBase = options.baseUrl ?: options.baseDir
def savedGitNameSeparator = gitNameSeparator
gitNameSeparator = options.nameSeparator ?: options.gitNameSeparator ?: gitNameSeparator
def savedGitNameSuffix = gitNameSuffix
gitNameSuffix = options.nameSuffix ?: options.gitNameSuffix ?: gitNameSuffix
closure.delegate = this
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure()
gitNameSuffix = savedGitNameSuffix
gitNameSeparator = savedGitNameSeparator
gitBase = savedGitBase
}
boolean isString(x) {
x instanceof String || x instanceof GString
}
void newVersion(String regex, Closure closure) {
newVersion[regex] = closure
}
def project(proj, Closure closure = null) {
String projName = isString(proj) ? proj : proj.name
if(projects.containsKey(projName))
throw new Exception("Duplicate project definition: $projName")
if(isString(proj))
proj = new Project([ name: proj as String ])
else
proj = new Project(proj)
if(closure) {
closure.delegate = proj
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure()
}
if(!proj.gitBase && !proj.gitSource)
proj.gitBase = gitBase
if(!proj.gitNameSeparator && !proj.gitSource)
proj.gitNameSeparator = gitNameSeparator
if(!proj.gitNameSuffix && !proj.gitSource)
proj.gitNameSuffix = gitNameSuffix
if(!proj.releaseVersion)
proj.releaseVersion = releaseVersion
else if(releaseVersion instanceof Map && proj.releaseVersion instanceof Map)
proj.releaseVersion = releaseVersion + proj.releaseVersion
if(!proj.newVersion)
proj.newVersion = newVersion
else if(newVersion instanceof Map && proj.newVersion instanceof Map)
proj.newVersion = newVersion + proj.newVersion
if(proj.releaseNoCommit == null)
proj.releaseNoCommit = releaseNoCommit
if(proj.releaseNoPush == null)
proj.releaseNoPush = releaseNoPush
if(proj.branch == null)
proj.branch = branch
if(proj.failOnSnapshotDependencies == null) {
proj.failOnSnapshotDependencies = failOnSnapshotDependencies
if(proj.failOnSnapshotDependencies == null)
proj.failOnSnapshotDependencies = true
}
if(!proj.deployTasks)
proj.deployTasks = deployTasks
else if(deployTasks instanceof List && proj.deployTasks instanceof List)
proj.deployTasks = deployTasks + proj.deployTasks
if(proj.skipTests == null)
proj.skipTests = skipTests
projects[projName] = proj
}
void releaseVersion(String regex, Closure closure) {
releaseVersion[regex] = closure
}
}
project.extensions.create 'multiproject', MultiProjectExtension
apply from: 'config.gradle'
if(project.ext.has('extraConfig')) {
def extraConfigs = project.ext.extraConfig.split(',')
for(def extraConfig in extraConfigs) {
if(!extraConfig.endsWith('.gradle'))
extraConfig += '.gradle'
apply from: extraConfig
}
}
def rootFolder = (project.multiproject.rootFolder ? new File(project.projectDir, project.multiproject.rootFolder).canonicalFile : project.projectDir)
task('nuke', group: 'cleanup') {
description = 'Deletes local git repositories listed in configuration.'
}
if(!tasks.findByName('clean'))
task('clean', group: 'cleanup') {
description = 'Cleans up compilation results and other temporary files.'
}
clean.mustRunAfter { update }
task('noupdate', group: 'git') {
description = 'Inhibits pulling or cloning git repositories from upstream.'
doLast {
tasks.each {
if(it.name =~ /(.+)_maybeClone/ || it.name =~ /(.+)_update/)
it.enabled = false
}
}
}
task('maybeClone', group: 'git') {
description = 'Clones repositories from upstream, if they do not exist.'
mustRunAfter nuke
mustRunAfter noupdate
}
task('update', group: 'git') {
description = 'Pulls every git repository from upstream.'
dependsOn maybeClone
mustRunAfter noupdate
}
task('gitBranchList', group: 'git') {
description = 'Prints a list of git branches in all git repositories.'
}
task('gitCommit', group: 'git') {
description = 'Commits changes (if any) in all git repositories.'
}
task('gitRevert', group: 'git') {
description = 'Reverts all changes in work directories of all git repositories.'
}
task('gitPush', group: 'git') {
description = 'Pushes current branches of all git repositories to upstream.'
}
task('gitStatus', group: 'git') {
description = 'Shows status of changed files in all git repositories.'
}
task('build', group: 'build') {
description = 'Builds projects in repositories.'
dependsOn maybeClone
mustRunAfter update
}
task('uploadArchives', group: 'deployment') {
description = 'Uploads archives of all git repositories to remote maven repository.'
dependsOn build
}
// automated release feature ++
task('releaseChecks', group: 'release') {
description = 'Checks all git repositories before release.'
dependsOn update
}
task('releaseCollectVersions', group: 'release') {
description = 'Collects version information on all git repositories.'
dependsOn releaseChecks
}
task('releaseResolveVersions', group: 'release') {
description = 'Resolves version information on all git repositories.'
dependsOn releaseCollectVersions
}
task('releasePrepareReleaseVersions', group: 'release') {
description = 'Assigns release versions to all git repositories.'
dependsOn releaseResolveVersions
}
task('releaseCheckSnapshotDependencies', group: 'release') {
description = 'Checks all git repositories for snapshot dependencies.'
dependsOn releasePrepareReleaseVersions
}
task('release', group: 'release') {
description = 'Releases the software from all git repositories.'
dependsOn releaseCheckSnapshotDependencies
finalizedBy { afterRelease }
}
Map versionMap = [:]
def getProjectKey = { String projName ->
projName.replaceAll(/[^0-9A-Za-z]/, '_')
}
def getVersionInfo = { String projName ->
versionMap[getProjectKey(projName)]
}
task ('afterRelease', group: 'release') {
description = 'Prints report after release is finished.'
doLast {
project.logger.warn 'Release is finished. The following repositories/projects were released:'
def pad = { String s, int length ->
(s ?: '').padRight(length, ' ')
}
project.logger.warn '{}| {} | {} | {}', pad('Repository', 16), pad('Branch', 16), pad('Release version', 16), pad('New version', 16)
versionMap.each { projKey, versionInfo ->
project.logger.warn '{}| {} | {} | {}', pad(versionInfo.originalProjectName, 16), pad(versionInfo.branch, 16), pad(versionInfo.releaseVersion, 16), pad(versionInfo.newVersion, 16)
}
}
}
project.gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure && task.name =~ /(.+)_release(.+)$/) {
project.logger.error 'Release process failed, reverting uncommitted changes:'
versionMap.each { projKey, versionInfo ->
def grgit = Grgit.open(versionInfo.projFolder.absolutePath)
try {
def status = grgit.status()
if(status.unstaged.added || status.unstaged.modified || status.unstaged.removed ||
status.staged.added || status.staged.modified || status.staged.removed) {
project.logger.warn '{} : reverting changes', versionInfo.originalProjectName
def cmd = grgit.repository.jgit.checkout()
cmd.setAllPaths(true)
cmd.call()
}
} finally {
grgit.close()
}
}
}
}
// automated release feature --
def clonedRepositories = []
// first pass, we create per-project tasks
project.multiproject.projects.each { projName, proj ->
File projFolder = new File(rootFolder, new File(projName).getName())
task("${projName}_nuke") {
nuke.dependsOn it
doLast {
project.logger.warn 'Deleting directory {}', projFolder
projFolder.deleteDir()
}
}
task("${projName}_maybeClone") {
maybeClone.dependsOn it
mustRunAfter nuke
mustRunAfter noupdate
doLast {
if(!projFolder.exists()) {
def gitSource
if(proj.gitSource)
gitSource = proj.gitSource
else {
def gitName = proj.gitName ?: projName
gitName += proj.gitNameSuffix
def gitBase
if(proj.gitBase)
gitBase = proj.gitBase
else if(project.multiproject.gitBase)
gitBase = project.multiproject.gitBase
else
throw new GradleException("Don't know from where to clone project '$projName'. Please specify gitBase or gitSource.")
gitSource = gitBase
gitSource += proj.gitNameSeparator
gitSource += gitName
}
if(new File(gitSource).exists())
gitSource = new File(gitSource).toURI().toString()
def cloneOptions = [ dir: projFolder, uri: gitSource ]
project.logger.warn '{} : cloning from {} to {}', projName, gitSource, projFolder
if(proj.branch) {
project.logger.warn '{} : checking out branch {}', projName, proj.branch
cloneOptions.refToCheckout = proj.branch
}
Grgit.clone(cloneOptions).close()
clonedRepositories.add(projName)
}
}
}
task("${projName}_update") {
dependsOn maybeClone
mustRunAfter noupdate
update.dependsOn it
doLast {
if(!clonedRepositories.contains(projName)) {
def grgit = Grgit.open(projFolder.absolutePath)
try {
if(proj.branch) {
project.logger.warn '{} : checking out branch {}', projName, proj.branch
if(grgit.branch.list().find { it.name == proj.branch })
grgit.checkout(branch: proj.branch)
else {
def cmd = grgit.repository.jgit.checkout()
cmd.name = proj.branch
cmd.createBranch = true
cmd.upstreamMode = org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM
cmd.startPoint = "origin/${proj.branch}"
cmd.call()
}
}
project.logger.warn '{} : pulling from origin', projName
grgit.pull()
} finally {
grgit.close()
}
}
}
}
boolean shouldBuild = proj.build == null || proj.build
boolean buildSelfDefined = proj.build == null || (proj.build instanceof Boolean && proj.build)
def projBuildTask = null
def projCleanTask = null
if(shouldBuild) {
projBuildTask = task("${projName}_build", type: GradleBuild) {
description = "Builds the project $projName"
dependsOn maybeClone
mustRunAfter update
build.dependsOn it
if(buildSelfDefined)
dir = projFolder
else {
proj.build = proj.build.toString()
if(proj.build.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.build}"
}
tasks = proj.buildTasks ?: [ 'build' ] + proj.deployTasks
if(proj.skipTests)
startParameter.excludedTaskNames = [ 'test' ]
}
projCleanTask = task("${projName}_clean", type: GradleBuild) {
description = "Cleans the project $projName"
dependsOn maybeClone
mustRunAfter update
clean.dependsOn it
dir = new File(projFolder, isString(proj.build) ? proj.build : '.')
tasks = [ 'clean' ]
}
}
if(proj.contribs) {
task("${projName}_installContribs", type: GradleBuild) {
description = "Installs contributions in project $projName"
dependsOn maybeClone
mustRunAfter update
projBuildTask.dependsOn it
if(isString(proj.contribs)) {
proj.contribs = proj.contribs as String
if(proj.contribs.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.contribs}"
}
else
dir = "$projFolder/contribs"
tasks = [ 'install' ]
}
task("${projName}_cleanContribs", type: GradleBuild) {
description = "Cleans contributions in project $projName"
dependsOn maybeClone
mustRunAfter update
dependsOn projCleanTask
clean.dependsOn it
if(isString(proj.contribs)) {
proj.contribs = proj.contribs as String
if(proj.contribs.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.contribs}"
}
else
dir = "$projFolder/contribs"
tasks = [ 'clean' ]
}
}
if(proj.examples) {
if(!tasks.findByName('buildExamples'))
task('buildExamples', group: 'build') {
description = 'Builds examples in repositories.'
dependsOn build
}
task("${projName}_buildExamples", type: GradleBuild) {
description = "Builds examples in project $projName"
dependsOn build
buildExamples.dependsOn it
dir = new File(projFolder, isString(proj.examples) ? proj.examples : 'examples')
tasks = [ 'build' ]
if(proj.skipTests)
startParameter.excludedTaskNames = [ 'test' ]
}
task("${projName}_cleanExamples", type: GradleBuild) {
description = "Cleans examples in project $projName"
dependsOn maybeClone
mustRunAfter update
clean.dependsOn it
if(projCleanTask)
projCleanTask.dependsOn it
dir = new File(projFolder, isString(proj.examples) ? proj.examples : 'examples')
tasks = [ 'clean' ]
}
}
if(proj.apps) {
if(!tasks.findByName('buildApps'))
task('buildApps', group: 'build') {
description = 'Builds applications in repositories.'
dependsOn build
}
task("${projName}_buildApps", type: GradleBuild) {
description = "Builds applications in project $projName"
dependsOn build
buildApps.dependsOn it
dir = new File(projFolder, isString(proj.apps) ? proj.apps : 'apps')
tasks = [ 'build' ]
if(proj.skipTests)
startParameter.excludedTaskNames = [ 'test' ]
}
task("${projName}_cleanApps", type: GradleBuild) {
description = "Cleans applications in project $projName"
dependsOn maybeClone
mustRunAfter update
clean.dependsOn it
if(projCleanTask)
projCleanTask.dependsOn it
dir = new File(projFolder, isString(proj.apps) ? proj.apps : 'apps')
tasks = [ 'clean' ]
}
}
if(proj.uploadArchives || buildSelfDefined) {
def uploadArchivesTask = task("${projName}_uploadArchives", type: GradleBuild) {
description = "Uploads archives of project $projName"
dependsOn build
uploadArchives.dependsOn it
dir = new File(projFolder, isString(proj.build) ? proj.build : '.')
tasks = [ 'uploadArchives' ]
}
if(proj.contribs)
task("${projName}_uploadContribs", type: GradleBuild) {
description = "Uploads contributions of project $projName"
dependsOn maybeClone
mustRunAfter update
uploadArchivesTask.dependsOn it
if(isString(proj.contribs)) {
proj.contribs = proj.contribs as String
if(proj.contribs.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.contribs}"
}
else
dir = "$projFolder/contribs"
tasks = [ 'uploadArchives' ]
}
}
task("${projName}_gitStatus") {
description = "git status on ${projName}"
dependsOn maybeClone
mustRunAfter update
gitStatus.dependsOn it
doLast {
def grgit = Grgit.open(projFolder.absolutePath)
try {
def status = grgit.status()
def unstaged = status.unstaged
if(unstaged.added || unstaged.modified || unstaged.removed)
println ' unstaged:'
for(def f in unstaged.added)
println " added $f"
for(def f in unstaged.modified)
println " modified $f"
for(def f in unstaged.removed)
println " removed $f"
def staged = status.staged
if(staged.added || staged.modified || staged.removed)
println ' staged:'
for(def f in staged.added)
println " added $f"
for(def f in staged.modified)
println " modified $f"
for(def f in staged.removed)
println " removed $f"
} finally {
grgit.close()
}
}
}
task("${projName}_gitBranchList") {
description = "git branches on ${projName}"
dependsOn maybeClone
mustRunAfter update
gitBranchList.dependsOn it
doLast {
def grgit = Grgit.open(projFolder.absolutePath)
try {
def branches = grgit.branch.list()
for(def branch in branches) {
print (branch == grgit.branch.current ? '* ' : ' ')
println branch.name
}
} finally {
grgit.close()
}
}
}
def gitCommitTask = task("${projName}_gitCommit") {
description = "git commit on ${projName}"
dependsOn maybeClone
mustRunAfter update
gitCommit.dependsOn it
doLast {
if(!project.ext.has('message'))
throw new GradleException('Cannot commit: message is not specified')
boolean amend = project.ext.has('amend') ? (project.ext.amend as boolean) : false
def grgit = Grgit.open(projFolder.absolutePath)
try {
def status = grgit.status()
if(status.unstaged.added || status.unstaged.modified || status.unstaged.removed ||
status.staged.added || status.staged.modified || status.staged.removed) {
project.logger.warn '{} : committing changes', projName
grgit.commit(all: true, message: project.ext.message, amend: amend)
}
} finally {
grgit.close()
}
}
}
task("${projName}_gitRevert") {
description = "git revert on ${projName}"
dependsOn maybeClone
mustRunAfter update
gitRevert.dependsOn it
doLast {
def grgit = Grgit.open(projFolder.absolutePath)
try {
project.logger.warn '{} : reverting changes', projName
def cmd = grgit.repository.jgit.checkout()
cmd.setAllPaths(true)
cmd.call()
} finally {
grgit.close()
}
}
}
task("${projName}_gitPush") {
description = "git push on ${projName}"
dependsOn maybeClone
mustRunAfter update
mustRunAfter gitCommitTask
gitPush.dependsOn it
doLast {
def grgit = Grgit.open(projFolder.absolutePath)
try {
project.logger.warn '{} : pushing {} to origin', projName, proj.branch
grgit.push()
} finally {
grgit.close()
}
}
}
if(shouldBuild) {
task("${projName}_releaseChecks", type: GradleBuild) {
description = "Do checks before release of ${projName}"
dependsOn maybeClone
mustRunAfter update
releaseChecks.dependsOn it
dir = projFolder
tasks = [ 'checkCommitNeeded', 'checkUpdateNeeded' ]
}
task("${projName}_releaseCollectVersions") {
description = "Collect versions before release of ${projName}"
dependsOn releaseChecks
releaseCollectVersions.dependsOn it
doLast {
File propertiesFile = new File(projFolder, 'gradle.properties')
if(!propertiesFile.exists())
throw new GradleException("File ${propertiesFile} does not exist. Could not release.")
Properties properties = new Properties()
propertiesFile.withInputStream { properties.load(it) }
if(!properties.version)
throw new GradleException("'version' property is not defined in ${propertiesFile}. Could not release.")
def releaseVersion
if(proj.releaseVersion instanceof String)
releaseVersion = proj.releaseVersion
else if(proj.releaseVersion instanceof Map)
for(def e in proj.releaseVersion) {
def m = properties.version =~ e.key
if(m) {
releaseVersion = e.value(m)
if(releaseVersion == null)
releaseVersion = properties.version
break
}
}
def newVersion
if(proj.newVersion instanceof String)
newVersion = proj.newVersion
else if(proj.newVersion instanceof Map)
for(def e in proj.newVersion) {
def m = properties.version =~ e.key
if(m) {
newVersion = e.value(m)
if(newVersion == null)
newVersion = properties.version
break
}
}
versionMap[getProjectKey(projName)] = [ originalProjectName: projName, projFolder: projFolder, branch: proj.branch, propertiesFile: propertiesFile, originalProperties: properties, releaseVersion: releaseVersion, newVersion: newVersion ]
}
}
task("${projName}_releaseResolveVersions") {
description = "Resolve versions before release of ${projName}"
dependsOn releaseCollectVersions
releaseResolveVersions.dependsOn it
doLast {
def thisVersion = getVersionInfo(projName)
def releaseProperties = [:] + thisVersion.originalProperties
releaseProperties.version = thisVersion.releaseVersion
def newProperties = [:] + thisVersion.originalProperties
newProperties.version = thisVersion.newVersion
thisVersion.originalProperties.each { key, value ->
def m = key =~ /(.+)_version$/
if(m) {
def otherProjName = m[0][1]
def otherProjVersion = getVersionInfo(otherProjName)
if(otherProjVersion) {
releaseProperties[key] = otherProjVersion.releaseVersion
newProperties[key] = otherProjVersion.newVersion
} else
project.logger.warn 'File {} contains property {} that refers to a non-existing project {}.', thisVersion.propertiesFile, key, otherProjName
}
}
thisVersion.releaseProperties = releaseProperties
thisVersion.newProperties = newProperties
}
}
task("${projName}_releasePrepareReleaseVersion") {
description = "Prepare release version for ${projName}"
dependsOn releaseResolveVersions
releasePrepareReleaseVersions.dependsOn it
doLast {
def thisVersion = getVersionInfo(projName)
project.logger.warn '{} : {} : setting release version', projName, thisVersion.releaseVersion
def releaseProperties = new Properties() + thisVersion.releaseProperties
thisVersion.propertiesFile.withOutputStream { releaseProperties.store(it, null) }
}
}
task("${projName}_releaseCheckSnapshotDependencies", type: GradleBuild) {
description = "Check for snapshot dependencies before release of ${projName}"
dependsOn releasePrepareReleaseVersions
releaseCheckSnapshotDependencies.dependsOn it
dir = projFolder
tasks = [ 'checkSnapshotDependencies' ]
enabled = proj.failOnSnapshotDependencies as boolean
}
task("${projName}_releaseBuildReleaseVersion", type: GradleBuild) {
description = "Builds release version of the project $projName"
dependsOn releaseCheckSnapshotDependencies
if(buildSelfDefined)
dir = projFolder
else {
proj.build = proj.build.toString()
if(proj.build.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.build}"
}
tasks = (proj.buildTasks ?: [ 'build' ]) + proj.deployTasks
if(proj.skipTests)
startParameter.excludedTaskNames = [ 'test' ]
}
task("${projName}_releaseCommitReleaseVersion") {
dependsOn "${projName}_releaseBuildReleaseVersion"
doLast {
def thisVersion = getVersionInfo(projName)
def grgit = Grgit.open(projFolder.absolutePath)
try {
if(proj.releaseNoCommit)
project.logger.warn '{} : {} : releaseNoCommit is set, will not be commited and/or pushed', projName, thisVersion.releaseVersion
else {
def status = grgit.status()
if(status.unstaged.added || status.unstaged.modified || status.unstaged.removed ||
status.staged.added || status.staged.modified || status.staged.removed) {
project.logger.warn '{} : {} : committing release version', projName, thisVersion.releaseVersion
grgit.commit(all: true, message: "Release ${thisVersion.releaseVersion}", amend: false)
}
project.logger.warn '{} : {} : creating release tag', projName, thisVersion.releaseVersion
grgit.tag.add(name: thisVersion.releaseVersion, message: "Release ${thisVersion.releaseVersion}", force: true)
if(proj.releaseNoPush)
project.logger.warn '{} : {} : releaseNoPush is set, will not be pushed to upstream repository', projName, thisVersion.releaseVersion
else {
project.logger.warn '{} : {} : pushing release version to upstream repository', projName, thisVersion.releaseVersion
grgit.push()
grgit.push(tags: true)
}
}
} finally {
grgit.close()
}
}
}
task("${projName}_releasePrepareNewVersion") {
description = "Prepare new version for ${projName}"
dependsOn "${projName}_releaseCommitReleaseVersion"
doLast {
def thisVersion = getVersionInfo(projName)
project.logger.warn '{} : {} : setting new version', projName, thisVersion.newVersion
def newProperties = new Properties() + thisVersion.newProperties
thisVersion.propertiesFile.withOutputStream { newProperties.store(it, null) }
}
}
task("${projName}_releaseBuildNewVersion", type: GradleBuild) {
description = "Builds new version of the project $projName"
dependsOn "${projName}_releasePrepareNewVersion"
if(buildSelfDefined)
dir = projFolder
else {
proj.build = proj.build.toString()
if(proj.build.endsWith('.gradle'))
buildFile = "$projFolder/${proj.build}"
else
dir = "$projFolder/${proj.build}"
}
tasks = (proj.buildTasks ?: [ 'build' ]) + proj.deployTasks
if(proj.skipTests)
startParameter.excludedTaskNames = [ 'test' ]
}
task("${projName}_releaseCommitNewVersion") {
dependsOn "${projName}_releaseBuildNewVersion"
release.dependsOn it
doLast {
def thisVersion = getVersionInfo(projName)
def grgit = Grgit.open(projFolder.absolutePath)
try {
if(proj.releaseNoCommit)
project.logger.warn '{} : {} : releaseNoCommit is set, will not be commited and/or pushed', projName, thisVersion.newVersion
else {
def status = grgit.status()
if(status.unstaged.added || status.unstaged.modified || status.unstaged.removed ||
status.staged.added || status.staged.modified || status.staged.removed) {
project.logger.warn '{} : {} : committing new version', projName, thisVersion.newVersion
grgit.commit(all: true, message: "New version ${thisVersion.newVersion}", amend: false)
if(proj.releaseNoPush)
project.logger.warn '{} : {} : releaseNoPush is set, will not be pushed to upstream repository', projName, thisVersion.newVersion
else {
project.logger.warn '{} : {} : pushing new version to upstream repository', projName, thisVersion.newVersion
grgit.push()
}
}
}
} finally {
grgit.close()
}
}
}
} // shouldBuild
} // first pass is over
// second pass, now we handle inter-project dependencies
project.multiproject.projects.each { projName, proj ->
if(proj.dependsOn) {
def thisProjUpdate = tasks.findByName("${projName}_update")
def thisProjBuild = tasks.findByName("${projName}_build")
def thisProjBuildExamples = tasks.findByName("${projName}_buildExamples")
def thisProjBuildApps = tasks.findByName("${projName}_buildApps")
def thisProjClean = tasks.findByName("${projName}_clean")
def thisProjGitStatus = tasks.findByName("${projName}_gitStatus")
def thisProjReleaseBuildReleaseVersion = tasks.findByName("${projName}_releaseBuildReleaseVersion")
toCollection(proj.dependsOn).each { otherProj ->
def otherProjUpdate = "${otherProj}_update"
thisProjUpdate.dependsOn otherProjUpdate
if(thisProjBuild) {
def otherProjBuild = tasks.findByName("${otherProj}_build")
if(otherProjBuild)
thisProjBuild.dependsOn otherProjBuild
else if (!file(otherProj).exists())
thisProjBuild.dependsOn otherProjUpdate
}
if(thisProjBuildExamples) {
def otherProjBuild = tasks.findByName("${otherProj}_build")
if(otherProjBuild)
thisProjBuildExamples.dependsOn otherProjBuild
else if (!file(otherProj).exists())
thisProjBuildExamples.dependsOn otherProjUpdate
}
if(thisProjBuildApps) {
def otherProjBuild = tasks.findByName("${otherProj}_build")
if(otherProjBuild)
thisProjBuildApps.dependsOn otherProjBuild
else if (!file(otherProj).exists())
thisProjBuildApps.dependsOn otherProjUpdate
}
if(thisProjClean) {
def otherProjClean = tasks.findByName("${otherProj}_clean")
if(otherProjClean)
thisProjClean.dependsOn otherProjClean
else if (!file(otherProj).exists())
thisProjClean.dependsOn otherProjUpdate
}
thisProjGitStatus.dependsOn "${otherProj}_gitStatus"
thisProjReleaseBuildReleaseVersion.dependsOn "${otherProj}_releaseCommitNewVersion"
}
}
}
defaultTasks 'buildApps'