JAL-3541 track own jalview.gui.Desktop clover source parsing error
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 //import org.apache.tools.ant.filters.ReplaceRegexp
3 import org.gradle.internal.os.OperatingSystem
4 import org.gradle.plugins.ide.eclipse.model.*
5
6
7 import groovy.transform.ExternalizeMethods
8
9 buildscript {
10   dependencies {
11     classpath 'org.openclover:clover:4.4.1'
12     classpath 'org.apache.commons:commons-compress:1.18'
13   }
14 }
15
16 plugins {
17   id 'java'
18   id 'application'
19   id 'eclipse'
20   id 'com.github.johnrengelman.shadow' version '4.0.3'
21   id 'com.install4j.gradle' version '7.0.9'
22 }
23
24 repositories {
25   jcenter()
26   mavenCentral()
27   mavenLocal()
28   flatDir {
29     dirs gradlePluginsDir
30   }
31 }
32
33 mainClassName = launcherClass
34 def cloverInstrDir = file("$buildDir/$cloverSourcesInstrDir")
35 def classes = "$jalviewDir/$classesDir"
36
37 if (clover.equals("true")) {
38   use_clover = true
39   classes = "$buildDir/$cloverClassesDir"
40 } else {
41   use_clover = false
42   classes = "$jalviewDir/$classesDir"
43 }
44
45 // configure classpath/args for j8/j11 compilation
46
47 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
48 def libDir
49 def libDistDir
50 def compile_source_compatibility
51 def compile_target_compatibility
52
53 ext {
54   getdownWebsiteDir = jalviewDir + '/' + getdown_website_dir + '/' + JAVA_VERSION
55   getdownDir = ""
56   reportRsyncCmd = false
57   buildDist = true
58   buildProperties = buildPropertiesFile
59   getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher
60   switch (CHANNEL) {
61
62     case "BUILD":
63     // TODO: get bamboo build artifact URL for getdown artifacts
64     getdown_channel_base = bamboo_channelbase
65     getdown_channel_name = bamboo_planKey + '/'+JAVA_VERSION
66     getdown_app_base = bamboo_channelbase + '/'+ bamboo_planKey + bamboo_getdown_channel_suffix + '/'+JAVA_VERSION
67     getdown_app_dir = getdown_app_dir_alt
68     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
69     break
70
71     case "RELEASE":
72     getdown_channel_name = CHANNEL.toLowerCase()
73     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
74     getdown_app_base = getdown_channel_base + "/" + getdownDir
75     getdown_app_dir = getdown_app_dir_release
76     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
77     reportRsyncCommand = true
78     break
79
80     case "ARCHIVE":
81     getdown_channel_name = CHANNEL.toLowerCase()+"/"+JALVIEW_VERSION
82     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
83     getdown_app_base = getdown_channel_base + "/" + getdownDir
84     getdown_app_dir = getdown_app_dir_alt
85     if (!file(ARCHIVEDIR+"/"+packageDir).exists()) {
86       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
87       exit
88     } else {
89       packageDir = ARCHIVEDIR + "/" + packageDir
90       buildProperties = ARCHIVEDIR +"/" + classesDir + "/" + buildPropertiesFile
91       buildDist = false
92     }
93     reportRsyncCommand = true
94     break
95
96     case "ARCHIVELOCAL":
97     getdown_channel_name = "archive" + "/" + JALVIEW_VERSION
98     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
99     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
100     getdown_app_dir = getdown_app_dir_alt
101     if (!file(ARCHIVEDIR+"/"+packageDir).exists()) {
102       print "Must provide an ARCHIVEDIR value to produce an archive distribution"
103       exit
104     } else {
105       packageDir = ARCHIVEDIR + "/" + packageDir
106       buildProperties = ARCHIVEDIR +"/" + classesDir + "/" + buildPropertiesFile
107       buildDist = false
108     }
109     reportRsyncCommand = true
110     getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher_local
111     break
112
113     case "DEVELOP":
114     getdown_channel_name = CHANNEL.toLowerCase()
115     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
116     getdown_app_base = getdown_channel_base + "/" + getdownDir
117     getdown_app_dir = getdown_app_dir_alt
118     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
119     reportRsyncCommand = true
120     break
121
122     case "TEST-RELEASE":
123     getdown_channel_name = CHANNEL.toLowerCase()
124     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
125     getdown_app_base = getdown_channel_base + "/" + getdownDir
126     getdown_app_dir = getdown_app_dir_alt
127     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
128     reportRsyncCommand = true
129     break
130
131     case ~/^SCRATCH(|-[-\w]*)$/:
132     getdown_channel_name = CHANNEL
133     getdownDir = getdown_channel_name + "/" + JAVA_VERSION
134     getdown_app_base = getdown_channel_base + "/" + getdownDir
135     getdown_app_dir = getdown_app_dir_alt
136     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
137     reportRsyncCommand = true
138     break
139
140     case "LOCAL":
141     getdown_app_base = file(getdownWebsiteDir).toURI().toString()
142     getdown_app_dir = getdown_app_dir_alt
143     buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
144     getdownLauncher = jalviewDir + '/' + getdown_lib_dir + '/' + getdown_launcher_local
145     break
146
147     default: // something wrong specified
148     print("CHANNEL must be one of BUILD, RELEASE, ARCHIVE, DEVELOP, TEST-RELEASE, SCRATCH-..., LOCAL [default]")
149     exit
150     break
151
152   }
153
154   println("Using a "+CHANNEL+" profile. appbase="+getdown_app_base)
155   getdownAppDir = getdownWebsiteDir + '/' + getdown_app_dir
156   //getdownJ11libDir = getdownWebsiteDir + '/' + getdown_j11lib_dir
157   getdownResourceDir = getdownWebsiteDir + '/' + getdown_resource_dir
158   getdownInstallDir = getdownWebsiteDir + '/' + getdown_install_dir
159   getdownFilesDir = jalviewDir + '/' + getdown_files_dir + '/' + JAVA_VERSION + '/'
160   getdownFilesInstallDir = getdownFilesDir+"/"+getdown_install_dir
161   /* compile without modules -- using classpath libraries
162   modules_compileClasspath = fileTree(dir: "$jalviewDir/$j11modDir", include: ["*.jar"])
163   modules_runtimeClasspath = modules_compileClasspath
164   */
165   gitHash = ""
166   gitBranch = ""
167 }
168
169 def JAVA_INTEGER_VERSION
170 def additional_compiler_args = []
171 // these are getdown.txt properties defined dependent on the JAVA_VERSION
172 def getdown_alt_java_min_version
173 def getdown_alt_java_max_version
174 // this property is assigned below and expanded to multiple lines in the getdown task
175 def getdown_alt_multi_java_location
176 // this property is for the Java library used in eclipse
177 def eclipse_java_runtime_name
178 if (JAVA_VERSION.equals("1.8")) {
179   JAVA_INTEGER_VERSION = "8"
180   //libDir = j8libDir
181   libDir = j11libDir
182   libDistDir = j8libDir
183   compile_source_compatibility = 1.8
184   compile_target_compatibility = 1.8
185   getdown_alt_java_min_version = getdown_alt_java8_min_version
186   getdown_alt_java_max_version = getdown_alt_java8_max_version
187   getdown_alt_multi_java_location = getdown_alt_java8_txt_multi_java_location
188   eclipse_java_runtime_name = "JavaSE-1.8"
189 } else if (JAVA_VERSION.equals("11")) {
190   JAVA_INTEGER_VERSION = "11"
191   libDir = j11libDir
192   libDistDir = j11libDir
193   compile_source_compatibility = 11
194   compile_target_compatibility = 11
195   getdown_alt_java_min_version = getdown_alt_java11_min_version
196   getdown_alt_java_max_version = getdown_alt_java11_max_version
197   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
198   eclipse_java_runtime_name = "JavaSE-11"
199   /* compile without modules -- using classpath libraries
200   additional_compiler_args += [
201   '--module-path', ext.modules_compileClasspath.asPath,
202   '--add-modules', j11modules
203   ]
204   */
205 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
206   JAVA_INTEGER_VERSION = JAVA_VERSION
207   libDir = j11libDir
208   libDistDir = j11libDir
209   compile_source_compatibility = JAVA_VERSION
210   compile_target_compatibility = JAVA_VERSION
211   getdown_alt_java_min_version = getdown_alt_java11_min_version
212   getdown_alt_java_max_version = getdown_alt_java11_max_version
213   getdown_alt_multi_java_location = getdown_alt_java11_txt_multi_java_location
214   eclipse_java_runtime_name = "JavaSE-11"
215   /* compile without modules -- using classpath libraries
216   additional_compiler_args += [
217   '--module-path', ext.modules_compileClasspath.asPath,
218   '--add-modules', j11modules
219   ]
220   */
221 } else {
222   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
223 }
224
225 sourceSets {
226
227   main {
228     java {
229       srcDirs "$jalviewDir/$sourceDir"
230       outputDir = file("$classes")
231     }
232
233     resources {
234       srcDirs "$jalviewDir/$resourceDir"
235     }
236
237     jar.destinationDir = file("$jalviewDir/$packageDir")
238
239     compileClasspath = files(sourceSets.main.java.outputDir)
240     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
241
242     runtimeClasspath = compileClasspath
243   }
244   clover {
245     java {
246       srcDirs = [ cloverInstrDir ]
247       outputDir = file("${buildDir}/${cloverClassesDir}")
248     }
249
250     resources {
251       srcDirs = sourceSets.main.resources.srcDirs
252     }
253     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
254     compileClasspath += files(sourceSets.main.java.outputDir)
255     compileClasspath += sourceSets.main.compileClasspath
256     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
257     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
258
259     runtimeClasspath = compileClasspath
260   }
261
262   test {
263     java {
264       srcDirs "$jalviewDir/$testSourceDir"
265       outputDir = file("$jalviewDir/$testOutputDir")
266     }
267
268     resources {
269       srcDirs = sourceSets.main.resources.srcDirs
270     }
271
272     compileClasspath = files( sourceSets.test.java.outputDir )
273
274     if (use_clover) {
275       compileClasspath = sourceSets.clover.compileClasspath
276     } else {
277       compileClasspath += files(sourceSets.main.java.outputDir)
278     }
279
280     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
281     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
282
283     runtimeClasspath = compileClasspath
284   }
285 }
286
287 // clover bits
288 dependencies {
289   if (use_clover) {
290     cloverCompile 'org.openclover:clover:4.4.1'
291     testCompile 'org.openclover:clover:4.4.1'
292   }
293 }
294
295 configurations {
296   cloverRuntime
297   cloverRuntime.extendsFrom cloverCompile
298 }
299
300 eclipse {
301   project {
302     name = "Jalview with gradle build"
303
304     natures 'org.eclipse.jdt.core.javanature',
305     'org.eclipse.jdt.groovy.core.groovyNature',
306     'org.eclipse.buildship.core.gradleprojectnature'
307
308     buildCommand 'org.eclipse.jdt.core.javabuilder'
309     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
310   }
311
312   classpath {
313     //defaultOutputDir = sourceSets.main.java.outputDir
314     def removeThese = []
315     configurations.each{ if (it.isCanBeResolved()) {
316       removeThese += it
317     }
318   }
319
320   minusConfigurations += removeThese
321   plusConfigurations = [ ]
322   file {
323
324     whenMerged { cp ->
325       def removeTheseToo = []
326       HashMap<String, Boolean> addedSrcPath = new HashMap<>();
327       cp.entries.each { entry ->
328         if (entry.kind == 'src') {
329           if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
330             removeTheseToo += entry
331           } else {
332             addedSrcPath.putAt(entry.path, true)
333           }
334         }
335       }
336       cp.entries.removeAll(removeTheseToo)
337
338       print ("CP="+cp.inspect())
339
340       cp.entries += new Output("bin/main")
341       cp.entries += new Library(fileReference(helpParentDir))
342       cp.entries += new Library(fileReference(resourceDir))
343
344       HashMap<String, Boolean> addedLibPath = new HashMap<>();
345
346       // changing from sourcesets.main.classpath to specific Java version lib
347       //sourceSets.main.compileClasspath.each{
348       fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
349         //don't want to add outputDir as eclipse is using its own output dir in bin/main
350         if (it.isDirectory() || ! it.exists()) {
351           // don't add dirs to classpath
352           return
353         }
354         def itPath = it.toString()
355         if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
356           itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
357         }
358         if (addedLibPath.get(itPath)) {
359           //println("Not adding duplicate entry "+itPath)
360         } else {
361           //println("Adding entry "+itPath)
362           cp.entries += new Library(fileReference(itPath))
363           addedLibPath.put(itPath, true)
364         }
365       }
366
367       // changing from sourcesets.main.classpath to specific Java version lib
368       //sourceSets.test.compileClasspath.each{
369       fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"]).each {
370         //if ((it.isDirectory() || ! it.exists()) && ! (it.equals(sourceSets.main.java.outputDir))) {
371         //no longer want to add outputDir as eclipse is using its own output dir in bin/main
372         if (it.isDirectory() || ! it.exists()) {
373           // don't add dirs to classpath
374           return false // groovy "break" in .each loop
375         }
376         def itPath = it.toString()
377         if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
378           itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
379         }
380         if (addedLibPath.get(itPath)) {
381           // don't duplicate
382         } else {
383           def lib = new Library(fileReference(itPath))
384           // this doesn't work... yet.  Adding test=true attribute using withXml below
385           //def attrs = new Node(null, 'attributes', ["test":"true"])
386           //lib.appendNode(attrs) //
387           cp.entries += lib
388           addedLibPath.put(itPath, true)
389         }
390         }
391       }
392
393       // withXml changes ignored by buildship, these add the "test=true" attribute
394       withXml {
395         def node = it.asNode()
396
397         def srcTestAttributes
398         node.children().each{ cpe ->
399           def attributes = cpe.attributes()
400           if (attributes.get("kind") == "src" && attributes.get("path") == "test") {
401             srcTestAttributes = cpe.find { a -> a.name() == "attributes" }
402             return
403           }
404         }
405         def addTestAttribute = true
406         srcTestAttributes.each{a ->
407           if (a.name() == "attribute" && a.attributes().getAt("name") == "test") {
408             addTestAttribute = false
409           }
410         }
411         if (addTestAttribute) {
412           srcTestAttributes.append(new Node(null, "attribute", [name:"test", value:"true"]))
413         }
414
415         node.children().each{ cpe ->
416           def attributes = cpe.attributes()
417           if (attributes.get("kind") == "lib" && attributes.get("path").startsWith("utils/")) {
418             cpe.appendNode('attributes')
419             .appendNode('attribute', [name:"test", value:"true"])
420           }
421         }
422       } // withXML
423     } // file
424
425     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
426   } // classpath
427
428   jdt {
429     // for the IDE, use java 11 compatibility
430     sourceCompatibility = compile_source_compatibility
431     targetCompatibility = compile_target_compatibility
432     javaRuntimeName = eclipse_java_runtime_name
433
434     file {
435       withProperties { props ->
436         def jalview_prefs = new Properties()
437         def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
438         jalview_prefs.load(ins)
439         ins.close()
440         jalview_prefs.forEach { t, v ->
441           if (props.getAt(t) == null) {
442             props.putAt(t, v)
443           }
444         }
445       }
446     }
447   }
448
449   //synchronizationTasks eclipseClasspath
450   //autoBuildTasks eclipseClasspath
451 }
452
453 task cloverInstr() {
454   // only instrument source, we build test classes as normal
455   inputs.files files (sourceSets.main.allJava,sourceSets.test.allJava) // , fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
456   outputs.dir cloverInstrDir
457
458   doFirst {
459     delete cloverInstrDir
460     def argsList = ["--initstring", "${buildDir}/clover/clover.db",
461     "-d", "${buildDir}/${cloverSourcesInstrDir}"]
462     argsList.addAll(inputs.files.files.collect({ file ->
463       file.absolutePath
464     }))
465     String[] args = argsList.toArray()
466     println("About to instrument "+args.length +" files")
467     com.atlassian.clover.CloverInstr.mainImpl(args)
468   }
469 }
470
471
472 task cloverReportHTML (type: JavaExec) {
473     inputs.dir "${buildDir}/clover"
474     outputs.dir "${reportsDir}/clover"
475
476     classpath configurations.cloverRuntime
477     maxHeapSize "2g"
478     main = "com.atlassian.clover.reporters.html.HtmlReporter"
479     args "-v", "--initstring", "${buildDir}/clover/clover.db",
480     "-o", "${reportsDir}/clover"
481 }
482
483 task cloverReportXML (type: JavaExec) {
484     inputs.dir "${buildDir}/clover"
485     outputs.dir "${reportsDir}/clover"
486     maxHeapSize "2g"
487     classpath configurations.cloverRuntime
488     main = "com.atlassian.clover.reporters.xml.XMLReporter"
489     args "--initstring", "${buildDir}/clover/clover.db", "-o", "${reportsDir}/clover/clover.xml"
490 }
491 task cloverReport {
492   group = "Verification"
493     description = "Creates the Clover report"
494     inputs.dir "${buildDir}/clover"
495     outputs.dir "${reportsDir}/clover"
496     onlyIf {
497       file("${buildDir}/clover/clover.db").exists()
498     }
499     dependsOn cloverReportXML
500     dependsOn cloverReportHTML
501 }
502
503 // end clover bits
504
505
506 compileJava {
507
508   doFirst {
509     sourceCompatibility = compile_source_compatibility
510     targetCompatibility = compile_target_compatibility
511     options.compilerArgs = additional_compiler_args
512     print ("Setting target compatibility to "+targetCompatibility+"\n")
513   }
514
515 }
516
517 compileTestJava {
518   if (use_clover) {
519     dependsOn compileCloverJava
520     classpath += configurations.cloverRuntime
521   } else {
522     classpath += sourceSets.main.runtimeClasspath
523   }
524   doFirst {
525     sourceCompatibility = compile_source_compatibility
526     targetCompatibility = compile_target_compatibility
527     options.compilerArgs = additional_compiler_args
528     print ("Setting target compatibility to "+targetCompatibility+"\n")
529   }
530 }
531
532
533 compileCloverJava {
534
535   doFirst {
536     sourceCompatibility = compile_source_compatibility
537     targetCompatibility = compile_target_compatibility
538     options.compilerArgs += additional_compiler_args
539     print ("Setting target compatibility to "+targetCompatibility+"\n")
540   }
541   classpath += configurations.cloverRuntime
542 }
543
544 clean {
545   delete sourceSets.main.java.outputDir
546 }
547
548 cleanTest {
549   delete sourceSets.test.java.outputDir
550   delete cloverInstrDir
551 }
552
553 // format is a string like date.format("dd MMMM yyyy")
554 def getDate(format) {
555   def date = new Date()
556   return date.format(format)
557 }
558
559 task setGitVals {
560   def hashStdOut = new ByteArrayOutputStream()
561   exec {
562     commandLine "git", "rev-parse", "--short", "HEAD"
563     standardOutput = hashStdOut
564     ignoreExitValue true
565   }
566
567   def branchStdOut = new ByteArrayOutputStream()
568   exec {
569     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
570     standardOutput = branchStdOut
571     ignoreExitValue true
572   }
573
574   project.ext.gitHash = hashStdOut.toString().trim()
575   project.ext.gitBranch = branchStdOut.toString().trim()
576
577   outputs.upToDateWhen { false }
578 }
579
580 task createBuildProperties(type: WriteProperties) {
581   dependsOn setGitVals
582   inputs.dir("$jalviewDir/$sourceDir")
583   inputs.dir("$classes")
584   inputs.dir("$jalviewDir/$resourceDir")
585   outputFile (buildProperties)
586   // taking time specific comment out to allow better incremental builds
587   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
588   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
589   property "BUILD_DATE", getDate("HH:mm:ss dd MMMM yyyy")
590   property "VERSION", JALVIEW_VERSION
591   property "INSTALLATION", INSTALLATION+" git-commit:"+project.ext.gitHash+" ["+project.ext.gitBranch+"]"
592   outputs.file(outputFile)
593 }
594
595 def buildingHTML = "$jalviewDir/$docDir/building.html"
596 task deleteBuildingHTML(type: Delete) {
597   delete buildingHTML
598 }
599
600 task convertBuildingMD(type: Exec) {
601   dependsOn deleteBuildingHTML
602   def buildingMD = "$jalviewDir/$docDir/building.md"
603   def css = "$jalviewDir/$docDir/github.css"
604
605   def pandoc = null
606   pandoc_exec.split(",").each {
607     if (file(it.trim()).exists()) {
608       pandoc = it.trim()
609       return true
610     }
611   }
612
613   def hostname = "hostname".execute().text.trim()
614   if ((pandoc == null || ! file(pandoc).exists()) && hostname.equals("jv-bamboo")) {
615     pandoc = System.getProperty("user.home")+"/buildtools/pandoc/bin/pandoc"
616   }
617
618   doFirst {
619     if (pandoc != null && file(pandoc).exists()) {
620         commandLine pandoc, '-s', '-o', buildingHTML, '--metadata', 'pagetitle="Building Jalview from Source"', '--toc', '-H', css, buildingMD
621     } else {
622         println("Cannot find pandoc. Skipping convert building.md to HTML")
623         throw new StopExecutionException()
624     }
625   }
626
627   ignoreExitValue true
628
629   inputs.file(buildingMD)
630   inputs.file(css)
631   outputs.file(buildingHTML)
632 }
633 clean {
634   delete buildingHTML
635 }
636
637 task syncDocs(type: Sync) {
638   dependsOn convertBuildingMD
639   def syncDir = "$classes/$docDir"
640   from fileTree("$jalviewDir/$docDir")
641   into syncDir
642
643 }
644
645 def helpFile = "$classes/$helpDir/help.jhm"
646
647 task copyHelp(type: Copy) {
648   def inputDir = "$jalviewDir/$helpParentDir/$helpDir"
649   def outputDir = "$classes/$helpDir"
650   from(inputDir) {
651     exclude '**/*.gif'
652       exclude '**/*.jpg'
653       exclude '**/*.png'
654       filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
655   }
656   from(inputDir) {
657     include '**/*.gif'
658       include '**/*.jpg'
659       include '**/*.png'
660   }
661   into outputDir
662
663   inputs.dir(inputDir)
664   outputs.files(helpFile)
665   outputs.dir(outputDir)
666 }
667
668 task syncLib(type: Sync) {
669   def syncDir = "$classes/$libDistDir"
670   from fileTree("$jalviewDir/$libDistDir")
671   into syncDir
672 }
673
674 task syncResources(type: Sync) {
675   from "$jalviewDir/$resourceDir"
676   include "**/*.*"
677   exclude "install4j"
678   into "$classes"
679   preserve {
680     include "**"
681   }
682 }
683
684 task prepare {
685   dependsOn syncResources
686   dependsOn syncDocs
687   dependsOn copyHelp
688 }
689
690
691 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
692 test {
693   dependsOn prepare
694   dependsOn compileJava
695   if (use_clover) {
696     dependsOn cloverInstr
697   }
698
699   if (use_clover) {
700     print("Running tests " + (use_clover?"WITH":"WITHOUT") + " clover [clover="+use_clover+"]\n")
701   }
702
703   useTestNG() {
704     includeGroups testngGroups
705     preserveOrder true
706     useDefaultListeners=true
707   }
708
709   workingDir = jalviewDir
710   //systemProperties 'clover.jar' System.properties.clover.jar
711   sourceCompatibility = compile_source_compatibility
712   targetCompatibility = compile_target_compatibility
713   jvmArgs += additional_compiler_args
714   print ("Setting target compatibility to "+targetCompatibility+"\n")
715 }
716
717 task buildIndices(type: JavaExec) {
718   dependsOn copyHelp
719   classpath = sourceSets.main.compileClasspath
720   main = "com.sun.java.help.search.Indexer"
721   workingDir = "$classes/$helpDir"
722   def argDir = "html"
723   args = [ argDir ]
724   inputs.dir("$workingDir/$argDir")
725
726   outputs.dir("$classes/doc")
727   outputs.dir("$classes/help")
728   outputs.file("$workingDir/JavaHelpSearch/DOCS")
729   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
730   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
731   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
732   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
733   outputs.file("$workingDir/JavaHelpSearch/TMAP")
734 }
735
736 task compileLinkCheck(type: JavaCompile) {
737   options.fork = true
738   classpath = files("$jalviewDir/$utilsDir")
739   destinationDir = file("$jalviewDir/$utilsDir")
740   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
741
742   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
743   inputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.java")
744   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
745   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
746 }
747
748 def helplinkscheckertouchfile = file("$jalviewDir/$utilsDir/HelpLinksChecker.touch")
749 task linkCheck(type: JavaExec) {
750   dependsOn prepare, compileLinkCheck
751   classpath = files("$jalviewDir/$utilsDir")
752   main = "HelpLinksChecker"
753   workingDir = jalviewDir
754   def help = "$classes/$helpDir"
755   args = [ "$classes/$helpDir", "-nointernet" ]
756
757   doLast {
758     helplinkscheckertouchfile.createNewFile()
759   }
760
761   inputs.dir("$classes/$helpDir")
762   outputs.file(helplinkscheckertouchfile)
763 }
764
765 // import the pubhtmlhelp target
766 ant.properties.basedir = "$jalviewDir"
767 ant.properties.helpBuildDir = jalviewDirAbsolutePath+"/$classes/$helpDir"
768 ant.importBuild "$utilsDir/publishHelp.xml"
769
770
771 task cleanPackageDir(type: Delete) {
772   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
773 }
774
775 jar {
776   dependsOn linkCheck
777   dependsOn buildIndices
778   dependsOn createBuildProperties
779
780   manifest {
781     attributes "Main-Class": mainClass,
782     "Permissions": "all-permissions",
783     "Application-Name": "Jalview Desktop",
784     "Codebase": application_codebase
785   }
786
787   destinationDir = file("$jalviewDir/$packageDir")
788   archiveName = rootProject.name+".jar"
789
790   exclude "cache*/**"
791   exclude "*.jar"
792   exclude "*.jar.*"
793   exclude "**/*.jar"
794   exclude "**/*.jar.*"
795
796   inputs.dir("$classes")
797   outputs.file("$jalviewDir/$packageDir/$archiveName")
798 }
799
800 task copyJars(type: Copy) {
801   from fileTree("$classes").include("**/*.jar").include("*.jar").files
802   into "$jalviewDir/$packageDir"
803 }
804
805 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
806 task syncJars(type: Sync) {
807   from fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").files
808   into "$jalviewDir/$packageDir"
809   preserve {
810     include jar.archiveName
811   }
812 }
813
814 task makeDist {
815   group = "build"
816   description = "Put all required libraries in dist"
817   // order of "cleanPackageDir", "copyJars", "jar" important!
818   jar.mustRunAfter cleanPackageDir
819   syncJars.mustRunAfter cleanPackageDir
820   dependsOn cleanPackageDir
821   dependsOn syncJars
822   dependsOn jar
823   outputs.dir("$jalviewDir/$packageDir")
824 }
825
826 task cleanDist {
827   dependsOn cleanPackageDir
828   dependsOn cleanTest
829   dependsOn clean
830 }
831
832 shadowJar {
833   group = "distribution"
834   if (buildDist) {
835     dependsOn makeDist
836   }
837   from ("$jalviewDir/$libDistDir") {
838     include("*.jar")
839   }
840   manifest {
841     attributes 'Implementation-Version': JALVIEW_VERSION
842   }
843   mainClassName = shadowJarMainClass
844   mergeServiceFiles()
845   classifier = "all-"+JALVIEW_VERSION+"-j"+JAVA_VERSION
846   minimize()
847 }
848
849 task getdownWebsite() {
850   group = "distribution"
851   description = "Create the getdown minimal app folder, and website folder for this version of jalview. Website folder also used for offline app installer"
852   if (buildDist) {
853     dependsOn makeDist
854   }
855
856         // clean the getdown website and files dir before creating getdown folders
857         delete project.ext.getdownWebsiteDir
858         delete project.ext.getdownFilesDir
859
860   def getdownWebsiteResourceFilenames = []
861   def getdownTextString = ""
862   def getdownResourceDir = project.ext.getdownResourceDir
863   def getdownAppDir = project.ext.getdownAppDir
864   def getdownResourceFilenames = []
865
866   doFirst {
867     copy {
868       from buildProperties
869       rename(buildPropertiesFile, getdown_build_properties)
870       into project.ext.getdownAppDir
871     }
872     getdownWebsiteResourceFilenames += getdown_app_dir+"/"+getdown_build_properties
873
874     // go through properties looking for getdown_txt_...
875     def props = project.properties.sort { it.key }
876         if (getdown_alt_java_min_version.length() > 0) {
877                 props.put("getdown_txt_java_min_version", getdown_alt_java_min_version)
878         }
879         if (getdown_alt_java_max_version.length() > 0) {
880                 props.put("getdown_txt_java_max_version", getdown_alt_java_max_version)
881         }
882         props.put("getdown_txt_multi_java_location", getdown_alt_multi_java_location)
883
884     props.put("getdown_txt_appbase", getdown_app_base)
885     props.each{ prop, val ->
886       if (prop.startsWith("getdown_txt_") && val != null) {
887         if (prop.startsWith("getdown_txt_multi_")) {
888           def key = prop.substring(18)
889           val.split(",").each{ v ->
890             def line = key + " = " + v + "\n"
891             getdownTextString += line
892           }
893         } else {
894           // file values rationalised
895           if (val.indexOf('/') > -1 || prop.startsWith("getdown_txt_resource")) {
896             def r = null
897             if (val.indexOf('/') == 0) {
898               // absolute path
899               r = file(val)
900             } else if (val.indexOf('/') > 0) {
901               // relative path (relative to jalviewDir)
902               r = file( jalviewDir + '/' + val )
903             }
904             if (r.exists()) {
905               val = getdown_resource_dir + '/' + r.getName()
906               getdownWebsiteResourceFilenames += val
907               getdownResourceFilenames += r.getPath()
908             }
909           }
910           if (! prop.startsWith("getdown_txt_resource")) {
911             def line = prop.substring(12) + " = " + val + "\n"
912             getdownTextString += line
913           }
914         }
915       }
916     }
917
918     getdownWebsiteResourceFilenames.each{ filename ->
919       getdownTextString += "resource = "+filename+"\n"
920     }
921     getdownResourceFilenames.each{ filename ->
922       copy {
923         from filename
924         into project.ext.getdownResourceDir
925       }
926     }
927
928     def codeFiles = []
929     fileTree(file(packageDir)).each{ f ->
930       if (f.isDirectory()) {
931         def files = fileTree(dir: f, include: ["*"]).getFiles()
932         codeFiles += files
933       } else if (f.exists()) {
934         codeFiles += f
935       }
936     }
937     codeFiles.sort().each{f ->
938       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
939       getdownTextString += line
940       copy {
941         from f.getPath()
942         into project.ext.getdownAppDir
943       }
944     }
945
946     // NOT USING MODULES YET, EVERYTHING SHOULD BE IN dist
947     /*
948     if (JAVA_VERSION.equals("11")) {
949     def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
950     j11libFiles.sort().each{f ->
951     def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
952     getdownTextString += line
953     copy {
954     from f.getPath()
955     into project.ext.getdownJ11libDir
956     }
957     }
958     }
959      */
960
961     // getdown-launcher.jar should not be in main application class path so the main application can move it when updated.  Listed as a resource so it gets updated.
962     //getdownTextString += "class = " + file(getdownLauncher).getName() + "\n"
963     getdownTextString += "resource = " + getdown_launcher_new + "\n"
964     getdownTextString += "class = " + mainClass + "\n"
965
966     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
967     getdown_txt.write(getdownTextString)
968
969     def launch_jvl = file(project.ext.getdownWebsiteDir + "/" + getdown_launch_jvl)
970     launch_jvl.write("appbase="+props.get("getdown_txt_appbase"))
971
972     copy {
973       from getdownLauncher
974       rename(file(getdownLauncher).getName(), getdown_launcher_new)
975       into project.ext.getdownWebsiteDir
976     }
977
978     copy {
979       from getdownLauncher
980       if (file(getdownLauncher).getName() != getdown_launcher) {
981         rename(file(getdownLauncher).getName(), getdown_launcher)
982       }
983       into project.ext.getdownWebsiteDir
984     }
985
986     if (! (CHANNEL.startsWith("ARCHIVE") || CHANNEL.startsWith("DEVELOP"))) {
987       copy {
988         from getdown_txt
989         from getdownLauncher
990         from getdownWebsiteDir+"/"+getdown_build_properties
991         if (file(getdownLauncher).getName() != getdown_launcher) {
992           rename(file(getdownLauncher).getName(), getdown_launcher)
993         }
994         into getdownInstallDir
995       }
996
997       copy {
998         from getdownInstallDir
999         into getdownFilesInstallDir
1000       }
1001     }
1002
1003     copy {
1004       from getdown_txt
1005       from launch_jvl
1006       from getdownLauncher
1007       from getdownWebsiteDir+"/"+getdown_build_properties
1008       if (file(getdownLauncher).getName() != getdown_launcher) {
1009         rename(file(getdownLauncher).getName(), getdown_launcher)
1010       }
1011       into getdownFilesDir
1012     }
1013
1014     copy {
1015           from getdownResourceDir
1016       into project.ext.getdownFilesDir + '/' + getdown_resource_dir
1017     }
1018   }
1019
1020   if (buildDist) {
1021     inputs.dir(jalviewDir + '/' + packageDir)
1022   }
1023   outputs.dir(project.ext.getdownWebsiteDir)
1024   outputs.dir(project.ext.getdownFilesDir)
1025 }
1026
1027 task getdownDigest(type: JavaExec) {
1028   group = "distribution"
1029   description = "Digest the getdown website folder"
1030   dependsOn getdownWebsite
1031   doFirst {
1032     classpath = files(getdownWebsiteDir + '/' + getdown_launcher)
1033   }
1034   main = "com.threerings.getdown.tools.Digester"
1035   args project.ext.getdownWebsiteDir
1036   inputs.dir(project.ext.getdownWebsiteDir)
1037   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
1038 }
1039
1040 task getdown() {
1041   group = "distribution"
1042   description = "Create the minimal and full getdown app folder for installers and website and create digest file"
1043   dependsOn getdownDigest
1044   doLast {
1045     if (reportRsyncCommand) {
1046       def fromDir = getdownWebsiteDir + (getdownWebsiteDir.endsWith("/")?"":"/")
1047       def toDir = getdown_rsync_dest + "/" + getdownDir + (getdownDir.endsWith("/")?"":"/")
1048       println "LIKELY RSYNC COMMAND:"
1049       println "mkdir -p '$toDir'\nrsync -avh --delete '$fromDir' '$toDir'"
1050       if (RUNRSYNC == "true") {
1051         exec {
1052           commandLine "mkdir", "-p", toDir
1053         }
1054         exec {
1055           commandLine "rsync", "-avh", "--delete", fromDir, toDir
1056         }
1057       }
1058     }
1059   }
1060 }
1061
1062 clean {
1063   delete project.ext.getdownWebsiteDir
1064   delete project.ext.getdownFilesDir
1065 }
1066
1067 install4j {
1068   def install4jHomeDir = "/opt/install4j"
1069   def hostname = "hostname".execute().text.trim()
1070   if (hostname.equals("jv-bamboo")) {
1071     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1072   } else if (OperatingSystem.current().isMacOsX()) {
1073     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
1074     if (! file(install4jHomeDir).exists()) {
1075       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
1076     }
1077   } else if (OperatingSystem.current().isLinux()) {
1078     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
1079   }
1080   installDir = file(install4jHomeDir)
1081   mediaTypes = Arrays.asList(install4jMediaTypes.split(","))
1082   if (install4jFaster.equals("true")) {
1083     faster = true
1084   }
1085 }
1086
1087 def install4jConf
1088 def macosJavaVMDir
1089 def macosJavaVMTgz
1090 def windowsJavaVMDir
1091 def windowsJavaVMTgz
1092 def install4jDir = "$jalviewDir/$install4jResourceDir"
1093 def install4jConfFile = "jalview-installers-java"+JAVA_VERSION+".install4j"
1094 install4jConf = "$install4jDir/$install4jConfFile"
1095
1096 task copyInstall4jTemplate(type: Copy) {
1097   macosJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/macos-jre"+JAVA_VERSION+"/jre"
1098   macosJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/macos-jre"+JAVA_VERSION+".tar.gz"
1099   windowsJavaVMDir = System.env.HOME+"/buildtools/jre/openjdk-java_vm/getdown/windows-jre"+JAVA_VERSION+"/jre"
1100   windowsJavaVMTgz = System.env.HOME+"/buildtools/jre/openjdk-java_vm/install4j/tgz/windows-jre"+JAVA_VERSION+".tar.gz"
1101   from (install4jDir) {
1102     include install4jTemplate
1103     rename (install4jTemplate, install4jConfFile)
1104     filter(ReplaceTokens, beginToken: '', endToken: '', tokens: ['9999999999': JAVA_VERSION])
1105     filter(ReplaceTokens, beginToken: '$$', endToken: '$$',
1106     tokens: [
1107     'JAVA_VERSION': JAVA_VERSION,
1108     'JAVA_INTEGER_VERSION': JAVA_INTEGER_VERSION,
1109     'VERSION': JALVIEW_VERSION,
1110     'MACOS_JAVA_VM_DIR': macosJavaVMDir,
1111     'MACOS_JAVA_VM_TGZ': macosJavaVMTgz,
1112     'WINDOWS_JAVA_VM_DIR': windowsJavaVMDir,
1113     'WINDOWS_JAVA_VM_TGZ': windowsJavaVMTgz,
1114     'INSTALL4JINFOPLISTFILEASSOCIATIONS': install4jInfoPlistFileAssociations,
1115     'COPYRIGHT_MESSAGE': install4jCopyrightMessage,
1116     'MACOS_BUNDLE_ID': install4jMacOSBundleId,
1117     'GETDOWN_RESOURCE_DIR': getdown_resource_dir,
1118     'GETDOWN_DIST_DIR': getdown_app_dir,
1119     'GETDOWN_ALT_DIR': getdown_app_dir_alt,
1120     'GETDOWN_INSTALL_DIR': getdown_install_dir
1121     ]
1122     )
1123     if (OSX_KEYPASS=="") {
1124       filter(ReplaceTokens, beginToken: 'codeSigning macEnabled="', endToken: '"', tokens: ['true':'codeSigning macEnabled="false"'])
1125       filter(ReplaceTokens, beginToken: 'runPostProcessor="true" ',endToken: 'Processor', tokens: ['post':'runPostProcessor="false" postProcessor'])
1126     }
1127   }
1128   into install4jDir
1129   outputs.files(install4jConf)
1130
1131   doLast {
1132     // include file associations in installer
1133     def installerFileAssociationsXml = file("$install4jDir/$install4jInstallerFileAssociations").text
1134     ant.replaceregexp(
1135       byline: false,
1136       flags: "s",
1137       match: '<action name="EXTENSIONS_REPLACED_BY_GRADLE".*?</action>',
1138       replace: installerFileAssociationsXml,
1139       file: install4jConf
1140     )
1141     /*
1142     // include uninstaller applescript app files in dmg
1143     def installerDMGUninstallerXml = file("$install4jDir/$install4jDMGUninstallerAppFiles").text
1144     ant.replaceregexp(
1145     byline: false,
1146     flags: "s",
1147     match: '<file name="UNINSTALL_OLD_JALVIEW_APP_REPLACED_IN_GRADLE" file=.*?>',
1148     replace: installerDMGUninstallerXml,
1149     file: install4jConf
1150     )
1151      */
1152   }
1153 }
1154
1155 task installers(type: com.install4j.gradle.Install4jTask) {
1156   group = "distribution"
1157   description = "Create the install4j installers"
1158   dependsOn getdown
1159   dependsOn copyInstall4jTemplate
1160   projectFile = file(install4jConf)
1161   println("Using projectFile "+projectFile)
1162   variables = [majorVersion: version.substring(2, 11), build: 001, OSX_KEYSTORE: OSX_KEYSTORE, JSIGN_SH: JSIGN_SH]
1163   destination = "$jalviewDir/$install4jBuildDir/$JAVA_VERSION"
1164   buildSelected = true
1165
1166   if (OSX_KEYPASS) {
1167     macKeystorePassword=OSX_KEYPASS
1168
1169   }
1170
1171   inputs.dir(project.ext.getdownWebsiteDir)
1172   inputs.file(install4jConf)
1173   inputs.dir(macosJavaVMDir)
1174   inputs.dir(windowsJavaVMDir)
1175   outputs.dir("$jalviewDir/$install4jBuildDir/$JAVA_VERSION")
1176
1177 }
1178
1179 clean {
1180   delete install4jConf
1181 }
1182
1183 task sourceDist (type: Tar) {
1184   
1185   def VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
1186   def outputFileName = project.name + "_" + VERSION_UNDERSCORES + ".tar.gz"
1187   // cater for buildship < 3.1 [3.0.1 is max version in eclipse 2018-09]
1188   try {
1189     archiveFileName = outputFileName
1190   } catch (Exception e) {
1191     archiveName = outputFileName
1192   }
1193   
1194   compression Compression.GZIP
1195   
1196   into project.name
1197
1198   def EXCLUDE_FILES=[
1199     "build/*",
1200     "bin/*",
1201     "test-output/",
1202     "test-reports",
1203     "tests",
1204     "clover*/*",
1205     ".*",
1206     "benchmarking/*",
1207     "**/.*",
1208     "*.class",
1209     "**/*.class","$j11modDir/**/*.jar","appletlib","**/*locales",
1210     "*locales/**",
1211     "utils/InstallAnywhere",
1212     "**/*.log",
1213   ] 
1214   def PROCESS_FILES=[
1215     "AUTHORS",
1216     "CITATION",
1217     "FEATURETODO",
1218     "JAVA-11-README",
1219     "FEATURETODO",
1220     "LICENSE",
1221     "**/README",
1222     "RELEASE",
1223     "THIRDPARTYLIBS",
1224     "TESTNG",
1225     "build.gradle",
1226     "gradle.properties",
1227     "**/*.java",
1228     "**/*.html",
1229     "**/*.xml",
1230     "**/*.gradle",
1231     "**/*.groovy",
1232     "**/*.properties",
1233     "**/*.perl",
1234     "**/*.sh",
1235   ]
1236   def INCLUDE_FILES=[
1237     ".settings/org.eclipse.jdt.core.jalview.prefs",
1238   ]
1239
1240   from(jalviewDir) {
1241     exclude (EXCLUDE_FILES)
1242     include (PROCESS_FILES)
1243     filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': JALVIEW_VERSION,'Year-Rel': getDate("yyyy")])
1244   }
1245   from(jalviewDir) {
1246     exclude (EXCLUDE_FILES)
1247     exclude (PROCESS_FILES)
1248     exclude ("appletlib")
1249     exclude ("**/*locales")
1250     exclude ("*locales/**")
1251     exclude ("utils/InstallAnywhere")
1252
1253     exclude (getdown_files_dir)
1254     exclude (getdown_website_dir)
1255
1256     // exluding these as not using jars as modules yet
1257     exclude ("$j11modDir/**/*.jar")
1258   }
1259   from(jalviewDir) {
1260     include(INCLUDE_FILES)
1261   }
1262 //  from (jalviewDir) {
1263 //    // explicit includes for stuff that seemed to not get included
1264 //    include(fileTree("test/**/*."))
1265 //    exclude(EXCLUDE_FILES)
1266 //    exclude(PROCESS_FILES)
1267 //  }
1268 }
1269
1270 task helppages  {
1271   dependsOn copyHelp
1272   dependsOn pubhtmlhelp
1273   
1274   inputs.dir("$classes/$helpDir")
1275   outputs.dir("$helpOutputDir")
1276 }