JAL-3130 gradle test now runs open clover 4.3.1 and new cloverReport task
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 import org.gradle.internal.os.OperatingSystem
3
4
5 buildscript {
6   dependencies {
7         classpath 'org.openclover:clover:4.3.1'
8   }
9 }
10
11 plugins {
12   id 'java'
13   id 'application'
14   id 'com.github.johnrengelman.shadow' version '4.0.3'
15   id 'com.install4j.gradle' version '7.0.9'
16 }
17
18 repositories {
19   jcenter()
20   mavenCentral()
21   mavenLocal()
22   flatDir {
23     dirs gradlePluginsDir
24   }
25 }
26
27 mainClassName = launcherClass
28 def cloverInstrDir = file("$buildDir/sources-instr")
29
30 sourceSets {
31
32   main {
33     java {
34       srcDirs "$jalviewDir/$sourceDir"
35       outputDir = file("$jalviewDir/$classesDir")
36     }
37
38     resources {
39       srcDirs "$jalviewDir/$resourceDir"
40       srcDirs "$jalviewDir/$libDir"
41     }
42
43     jar.destinationDir = file("$jalviewDir/$packageDir")
44
45     compileClasspath = files(sourceSets.main.java.outputDir)
46     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
47     if (JAVA_VERSION.equals("1.8")) {
48       print("ADDING J11LIBS TO CLASSPATH")
49       compileClasspath += fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"])
50     }
51
52     runtimeClasspath = compileClasspath
53   }
54
55   modules {
56     compileClasspath = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"])
57
58     runtimeClasspath = compileClasspath
59   }
60   
61   clover {
62     java {
63         srcDirs = [ cloverInstrDir ]
64         outputDir = file("$jalviewDir/$testOutputDir")
65     }
66     
67     resources {
68       srcDirs = sourceSets.main.resources.srcDirs
69     }
70     compileClasspath = configurations.cloverRuntime + files( sourceSets.clover.java.outputDir )
71     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
72     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
73     if (JAVA_VERSION.equals("1.8")) {
74       print("ADDING J11LIBS TO CLASSPATH")
75       compileClasspath += fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"])
76     }
77
78     runtimeClasspath = compileClasspath
79   }
80   
81   test {
82     java {
83       srcDirs = [ cloverInstrDir ]
84       outputDir = file("$jalviewDir/$testOutputDir")
85     }
86
87     resources {
88       srcDirs = sourceSets.main.resources.srcDirs
89     }
90
91
92     compileClasspath += configurations.cloverRuntime + files( sourceSets.test.java.outputDir )
93     compileClasspath += fileTree(dir: "$jalviewDir/$utilsDir", include: ["**/*.jar"])
94     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
95     if (JAVA_VERSION.equals("1.8")) {
96       print("ADDING J11LIBS TO CLASSPATH")
97       compileClasspath += fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"])
98     }
99
100     runtimeClasspath = compileClasspath
101   }
102 }
103
104 // clover bits
105 dependencies {
106   cloverCompile 'org.openclover:clover:4.3.1'
107   testCompile 'org.openclover:clover:4.3.1'
108 }
109
110 configurations {
111     cloverRuntime
112     cloverRuntime.extendsFrom cloverCompile
113 }
114  
115 task cloverInstr() {
116     inputs.files files (sourceSets.main.allJava, fileTree(dir:"$jalviewDir/$testSourceDir", include: ["**/*.java"]))
117     outputs.dir cloverInstrDir
118
119     doFirst {
120         delete cloverInstrDir
121         def argsList = ["--initstring", "${buildDir}/clover/clover.db",
122                         "-d", "${buildDir}/sources-instr"]
123         argsList.addAll(inputs.files.files.collect({ file ->
124             file.absolutePath
125         }))
126         String[] args = argsList.toArray()
127         println("About to instrument "+args.length +" files")
128         com.atlassian.clover.CloverInstr.mainImpl(args)
129     }
130 }
131
132 test.dependsOn cloverInstr
133  
134 task cloverReport {
135     inputs.dir "${buildDir}/clover"
136     outputs.dir "${reportsDir}/clover"
137     onlyIf {
138         file("${buildDir}/clover/clover.db").exists()
139     }
140     doFirst {
141         def argsList = ["--initstring", "${buildDir}/clover/clover.db",
142                         "-o", "${reportsDir}/clover"]
143         String[] args = argsList.toArray()
144         com.atlassian.clover.reporters.html.HtmlReporter.runReport(args)
145     }
146 }
147
148 // end clover bits
149
150
151 compileJava {
152
153   doFirst {
154     if (JAVA_VERSION.equals("1.8")) {
155       sourceCompatibility = 1.8
156       targetCompatibility = 1.8
157     } else if (JAVA_VERSION.equals("11")) {
158       sourceCompatibility = 11
159       targetCompatibility = 11
160
161       options.compilerArgs = [
162         '--module-path', sourceSets.modules.compileClasspath.asPath,
163         '--add-modules', j11modules
164       ]
165     }
166     print ("Setting target compatibility to "+targetCompatibility+"\n")
167   }
168
169 }
170
171 compileTestJava {
172
173   doFirst {
174     if (JAVA_VERSION.equals("1.8")) {
175       sourceCompatibility = 1.8
176       targetCompatibility = 1.8
177     } else if (JAVA_VERSION.equals("11")) {
178       sourceCompatibility = 11
179       targetCompatibility = 11
180
181       options.compilerArgs = [
182         '--module-path', sourceSets.modules.compileClasspath.asPath,
183         '--add-modules', j11modules
184       ]
185     }
186     print ("Setting target compatibility to "+targetCompatibility+"\n")
187   }
188   classpath += configurations.cloverRuntime
189 }
190
191
192 //compileCloverJava {
193 //
194 //  doFirst {
195 //    if (JAVA_VERSION.equals("1.8")) {
196 //      sourceCompatibility = 1.8
197 //      targetCompatibility = 1.8
198 //    } else if (JAVA_VERSION.equals("11")) {
199 //      sourceCompatibility = 11
200 //      targetCompatibility = 11
201 //
202 //      options.compilerArgs += [
203 //        '--module-path', sourceSets.modules.compileClasspath.asPath,
204 //        '--add-modules', j11modules
205 //      ]
206 //    }
207 //    print ("Setting target compatibility to "+targetCompatibility+"\n")
208 //  }
209 //  classpath += configurations.cloverRuntime
210 //}
211
212 clean {
213   delete sourceSets.main.java.outputDir
214 }
215
216 cleanTest {
217   delete sourceSets.test.java.outputDir
218   delete cloverInstrDir
219 }
220
221 def getDate(format) {
222   def date = new Date()
223   //return date.format("dd MMMM yyyy")
224   return date.format(format)
225 }
226
227 def getGitHash() {
228   def stdout = new ByteArrayOutputStream()
229   exec {
230     commandLine "git", "rev-parse", "--short", "HEAD"
231     standardOutput = stdout
232     workingDir = jalviewDir
233   }
234   return stdout.toString().trim()
235 }
236
237 def getGitBranch() {
238   def stdout = new ByteArrayOutputStream()
239   exec {
240     commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
241     standardOutput = stdout
242     workingDir = jalviewDir
243   }
244   return stdout.toString().trim()
245 }
246
247 task createBuildProperties(type: WriteProperties) {
248   inputs.dir("$jalviewDir/$sourceDir")
249   inputs.dir("$jalviewDir/$resourceDir")
250   outputFile "$jalviewDir/$classesDir/$buildPropertiesFile"
251   /* taking time/date specific comment out to allow better incremental builds */
252   //comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd HH:mm:ss")
253   comment "--Jalview Build Details--\n"+getDate("yyyy-MM-dd")
254   property "BUILD_DATE", getDate("dd MMMM yyyy")
255   property "VERSION", JALVIEW_VERSION
256   property "INSTALLATION", INSTALLATION+" git-commit:"+getGitHash()+" ["+getGitBranch()+"]"
257   outputs.file(outputFile)
258   outputs.dir("$jalviewDir/$classesDir")
259 }
260
261 task syncDocs(type: Sync) {
262   def syncDir = "$jalviewDir/$classesDir/$docDir"
263   from fileTree("$jalviewDir/$docDir")
264   into syncDir
265
266 }
267
268 def helpFile = "$jalviewDir/$classesDir/$helpDir/help.jhm"
269 task syncHelp(type: Sync) {
270   inputs.files("$jalviewDir/$helpDir")
271   outputs.files(helpFile)
272
273   def syncDir = "$jalviewDir/$classesDir/$helpDir"
274   from fileTree("$jalviewDir/$helpDir")
275   into syncDir
276 }
277
278 task copyHelp(type: Copy) {
279   def inputDir = "$jalviewDir/$helpDir"
280   def outputDir = "$jalviewDir/$classesDir/$helpDir"
281   from inputDir
282   into outputDir
283   filter(ReplaceTokens, beginToken: '$$', endToken: '$$', tokens: ['Version-Rel': "USING_FILTER"])
284   inputs.dir(inputDir)
285   outputs.files(helpFile)
286   outputs.dir(outputDir)
287 }
288
289 task syncLib(type: Sync) {
290   def syncDir = "$jalviewDir/$classesDir/$libDir"
291   from fileTree("$jalviewDir/$libDir")
292   into syncDir
293 }
294
295 task syncResources(type: Sync) {
296   from "$jalviewDir/$resourceDir"
297   include "**/*.*"
298   exclude "install4j"
299   into "$jalviewDir/$classesDir"
300   preserve {
301     include "**"
302   }
303 }
304
305 task prepare {
306   dependsOn syncResources
307   dependsOn syncDocs
308   dependsOn copyHelp
309 }
310
311
312 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
313 test {
314   dependsOn prepare
315   
316   
317   useTestNG()
318   options {
319     includeGroups testngGroups
320     preserveOrder true
321   }
322   
323   workingDir = jalviewDir
324   //systemProperties 'clover.jar' System.properties.clover.jar
325   if (JAVA_VERSION.equals("1.8")) {
326     sourceCompatibility = 1.8
327     targetCompatibility = 1.8
328   } else if (JAVA_VERSION.equals("11")) {
329     sourceCompatibility = 11
330     targetCompatibility = 11
331
332     jvmArgs += [
333       '--module-path',
334       sourceSets.modules.compileClasspath.asPath,
335       '--add-modules',
336       j11modules
337     ]
338   }
339   print ("Setting target compatibility to "+targetCompatibility+"\n")
340 }
341
342 task buildIndices(type: JavaExec) {
343   dependsOn copyHelp
344   classpath = sourceSets.main.compileClasspath
345   main = "com.sun.java.help.search.Indexer"
346   workingDir = "$jalviewDir/$classesDir/$helpDir"
347   def argDir = "html"
348   args = [ argDir ]
349   inputs.dir("$workingDir/$argDir")
350
351   outputs.dir("$jalviewDir/$classesDir/doc")
352   outputs.dir("$jalviewDir/$classesDir/help")
353   outputs.file("$workingDir/JavaHelpSearch/DOCS")
354   outputs.file("$workingDir/JavaHelpSearch/DOCS.TAB")
355   outputs.file("$workingDir/JavaHelpSearch/OFFSETS")
356   outputs.file("$workingDir/JavaHelpSearch/POSITIONS")
357   outputs.file("$workingDir/JavaHelpSearch/SCHEMA")
358   outputs.file("$workingDir/JavaHelpSearch/TMAP")
359 }
360
361 task compileLinkCheck(type: JavaCompile) {
362   options.fork = true
363   classpath = files("$jalviewDir/$utilsDir")
364   destinationDir = file("$jalviewDir/$utilsDir")
365   source = fileTree(dir: "$jalviewDir/$utilsDir", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
366
367   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.class")
368   outputs.file("$jalviewDir/$utilsDir/BufferedLineReader.class")
369 }
370
371 task linkCheck(type: JavaExec) {
372   dependsOn prepare, compileLinkCheck
373   classpath = files("$jalviewDir/$utilsDir")
374   main = "HelpLinksChecker"
375   workingDir = "$jalviewDir"
376   args = [ "$classesDir/$helpDir", "-nointernet" ]
377
378   doFirst {
379     standardOutput new FileOutputStream("$jalviewDir/$utilsDir/HelpLinksChecker.out")
380   }
381
382   outputs.file("$jalviewDir/$utilsDir/HelpLinksChecker.out")
383 }
384
385 task cleanPackageDir(type: Delete) {
386   delete fileTree("$jalviewDir/$packageDir").include("*.jar")
387 }
388
389 jar {
390   dependsOn linkCheck
391   dependsOn buildIndices
392   dependsOn createBuildProperties
393
394   manifest {
395     attributes "Main-Class": mainClass,
396     "Permissions": "all-permissions",
397     "Application-Name": "Jalview Desktop",
398     "Codebase": application_codebase
399   }
400
401   destinationDir = file("$jalviewDir/$packageDir")
402   archiveName = rootProject.name+".jar"
403
404   exclude "cache*/**"
405   exclude "*.jar"
406   exclude "*.jar.*"
407   exclude "**/*.jar"
408   exclude "**/*.jar.*"
409
410   inputs.dir("$jalviewDir/$classesDir")
411   outputs.file("$jalviewDir/$packageDir/$archiveName")
412 }
413
414 task copyJars(type: Copy) {
415   from fileTree("$jalviewDir/$classesDir").include("**/*.jar").include("*.jar").files
416   into "$jalviewDir/$packageDir"
417 }
418
419 // doing a Sync instead of Copy as Copy doesn't deal with "outputs" very well
420 task syncJars(type: Sync) {
421   from fileTree("$jalviewDir/$libDir").include("**/*.jar").include("*.jar").files
422   into "$jalviewDir/$packageDir"
423   preserve {
424     include jar.archiveName
425   }
426 }
427
428 task makeDist {
429   // order of "cleanPackageDir", "copyJars", "jar" important!
430   jar.mustRunAfter cleanPackageDir
431   syncJars.mustRunAfter cleanPackageDir
432   dependsOn cleanPackageDir
433   dependsOn syncJars
434   dependsOn jar
435   outputs.dir("$jalviewDir/$packageDir")
436 }
437
438 task cleanDist {
439   dependsOn cleanPackageDir
440   dependsOn cleanTest
441   dependsOn clean
442 }
443
444 shadowJar {
445   dependsOn makeDist
446   if (JAVA_VERSION.equals("11")) {
447     from ("$jalviewDir/$j11libDir") {
448       include("*.jar")
449     }
450   }
451   mainClassName = shadowJarMainClass
452   mergeServiceFiles()
453   classifier = "all"
454   minimize()
455 }
456
457 ext {
458   getdownWebsiteDir = jalviewDir + '/' + getdown_website_dir
459   getdownAppDir = getdownWebsiteDir + '/' + getdown_app_dir
460   getdownJ11libDir = getdownWebsiteDir + '/' + getdown_j11lib_dir
461   getdownResourceDir = getdownWebsiteDir + '/' + getdown_resource_dir
462   getdownLauncher = jalviewDir + '/' + getdown_launcher
463   getdownFilesDir = jalviewDir + '/' + getdown_files_dir
464 }
465
466 task getdownWebsite() {
467   dependsOn makeDist
468   def getdownWebsiteResourceFilenames = []
469   def getdownTextString = ""
470   def getdownResourceDir = project.ext.getdownResourceDir
471   def getdownAppDir = project.ext.getdownAppDir
472   def getdownResourceFilenames = []
473   doFirst {
474     // go through properties looking for getdown_txt_...
475     def props = project.properties.sort { it.key }
476     if (JAVA_VERSION.equals("11")) {
477       props.put("getdown_txt_java_min_version", getdown_alt_java11_min_version)
478     } else {
479       props.put("getdown_txt_java_min_version", getdown_alt_java8_min_version)
480     }
481     props.each{ prop, val ->
482       if (prop.startsWith("getdown_txt_") && val != null) {
483         if (prop.startsWith("getdown_txt_multi_")) {
484           def key = prop.substring(18)
485           val.split(",").each{ v ->
486             def line = key + " = " + v + "\n"
487             getdownTextString += line
488           }
489         } else {
490           // file values rationalised
491           if (val.indexOf('/') > -1) {
492             def r = null
493             if (val.indexOf('/') == 0) {
494               // absolute path
495               r = file(val)
496             } else if (val.indexOf('/') > 0) {
497               // relative path (relative to jalviewDir)
498               r = file( jalviewDir + '/' + val )
499             }
500             if (r.exists()) {
501               val = getdown_resource_dir + '/' + r.getName()
502               getdownWebsiteResourceFilenames += val
503               getdownResourceFilenames += r.getPath()
504             }
505           }
506           def line = prop.substring(12) + " = " + val + "\n"
507           getdownTextString += line
508         }
509       }
510     }
511
512     getdownWebsiteResourceFilenames.each{ filename ->
513       getdownTextString += "resource = "+filename+"\n"
514     }
515     getdownResourceFilenames.each{ filename ->
516       copy {
517         from filename
518         into project.ext.getdownResourceDir
519       }
520     }
521
522     def codeFiles = []
523     makeDist.outputs.files.each{ f ->
524       if (f.isDirectory()) {
525         def files = fileTree(dir: f, include: ["*"]).getFiles()
526         codeFiles += files
527       } else if (f.exists()) {
528         codeFiles += f
529       }
530     }
531     codeFiles.sort().each{f ->
532       def line = "code = " + getdown_app_dir + '/' + f.getName() + "\n"
533       getdownTextString += line
534       copy {
535         from f.getPath()
536         into project.ext.getdownAppDir
537       }
538     }
539
540     if (JAVA_VERSION.equals("11")) {
541       def j11libFiles = fileTree(dir: "$jalviewDir/$j11libDir", include: ["*.jar"]).getFiles()
542       j11libFiles.sort().each{f ->
543         def line = "code = " + getdown_j11lib_dir + '/' + f.getName() + "\n"
544         getdownTextString += line
545         copy {
546           from f.getPath()
547           into project.ext.getdownJ11libDir
548         }
549       }
550     }
551
552     getdownTextString += "code = " + file(getdownLauncher).getName() + "\n"
553     getdownTextString += "class = " + mainClass + "\n"
554
555     def getdown_txt = file(project.ext.getdownWebsiteDir + "/getdown.txt")
556     getdown_txt.write(getdownTextString)
557
558     copy {
559       from getdown_txt
560       into project.ext.getdownFilesDir
561     }
562
563     copy {
564       from getdownLauncher
565       into project.ext.getdownWebsiteDir
566     }
567
568     copy {
569       from getdownLauncher
570       into project.ext.getdownFilesDir
571     }
572
573     copy {
574       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.background_image')
575       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.error_background')
576       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.progress_image')
577       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.icon')
578       from jalviewDir + '/' + project.getProperty('getdown_txt_ui.mac_dock_icon')
579       into project.ext.getdownFilesDir + '/' + getdown_resource_dir 
580     }
581   }
582
583   inputs.dir(jalviewDir + '/' + packageDir)
584   outputs.dir(project.ext.getdownWebsiteDir)
585   outputs.dir(project.ext.getdownFilesDir)
586 }
587
588 task getdownDigest(type: JavaExec) {
589   dependsOn getdownWebsite
590   classpath = files(jalviewDir + '/' + getdown_core)
591   main = "com.threerings.getdown.tools.Digester"
592   args project.ext.getdownWebsiteDir
593   outputs.file(project.ext.getdownWebsiteDir + '/' + "digest2.txt")
594 }
595
596 task getdown() {
597   dependsOn getdownDigest
598 }
599
600 clean {
601   delete project.ext.getdownWebsiteDir
602   delete project.ext.getdownFilesDir
603 }
604
605 install4j {
606   def install4jHomeDir = "/opt/install4j"
607   def hostname = "hostname".execute().text.trim()
608   if (hostname.equals("jv-bamboo")) {
609     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
610   } else if (OperatingSystem.current().isMacOsX()) {
611     install4jHomeDir = '/Applications/install4j.app/Contents/Resources/app'
612     if (! file(install4jHomeDir).exists()) {
613       install4jHomeDir = System.getProperty("user.home")+install4jHomeDir
614     }
615   } else if (OperatingSystem.current().isLinux()) {
616     install4jHomeDir = System.getProperty("user.home")+"/buildtools/install4j"
617   }
618   installDir = file(install4jHomeDir)
619 }
620
621 task installers(type: com.install4j.gradle.Install4jTask) {
622   dependsOn getdown
623   projectFile = "$jalviewDir/$install4jResourceDir/$install4jConf"
624   variables = [majorVersion: version.substring(2, 11), build: 001]
625   destination = "$jalviewDir/$install4jBuildDir"
626   buildSelected = true
627   inputs.dir(project.ext.getdownWebsiteDir)
628   outputs.dir("$jalviewDir/$install4jBuildDir")
629 }
630