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