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