Merge branch 'improvement/JAL-1988+JAL-3416_Java8_macOS_APQHandlers_and_FlatLaF_optio...
[jalview.git] / utils / debian / debian_build.gradle
1 /* Convention for properties.  Read from gradle.properties, use lower_case_underlines for property names.
2  * For properties set within build.gradle, use camelCaseNoSpace.
3  */
4 import org.apache.tools.ant.filters.ReplaceTokens
5
6 plugins {
7   id 'java'
8   id 'application'
9 }
10
11 // in ext the values are cast to Object. Ensure string values are cast as String (and not GStringImpl) for later use
12 def string(Object o) {
13   return o == null ? "" : o.toString()
14 }
15
16 def overrideProperties(String propsFileName, boolean output = false) {
17   if (propsFileName == null) {
18     return
19   }
20   def propsFile = file(propsFileName)
21   if (propsFile != null && propsFile.exists()) {
22     println("Using properties from file '${propsFileName}'")
23     try {
24       def p = new Properties()
25       def localPropsFIS = new FileInputStream(propsFile)
26       p.load(localPropsFIS)
27       localPropsFIS.close()
28       p.each {
29         key, val -> 
30           def oldval
31           if (project.hasProperty(key)) {
32             oldval = project.findProperty(key)
33             project.setProperty(key, val)
34             if (output) {
35               println("Overriding property '${key}' ('${oldval}') with ${file(propsFile).getName()} value '${val}'")
36             }
37           } else {
38             ext.setProperty(key, val)
39             if (output) {
40               println("Setting ext property '${key}' with ${file(propsFile).getName()}s value '${val}'")
41             }
42           }
43       }
44     } catch (Exception e) {
45       println("Exception reading local.properties")
46       e.printStackTrace()
47     }
48   }
49 }
50
51 project.ext {
52   jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
53   jalviewDirRelativePath = jalviewDir
54
55   propertiesChannelName = "release"
56   channelDir = string("${jalviewDir}/${channel_properties_dir}/${propertiesChannelName}")
57   channelGradleProperties = string("${channelDir}/channel_gradle.properties")
58   overrideProperties(channelGradleProperties, false)
59   
60   ////  
61   // Import releaseProps from the RELEASE file
62   // or a file specified via JALVIEW_RELEASE_FILE if defined
63   // Expect jalview.version and target release branch in jalview.release        
64   def releaseProps = new Properties();
65   def releasePropFile = findProperty("JALVIEW_RELEASE_FILE");
66   def defaultReleasePropFile = "${jalviewDirAbsolutePath}/RELEASE";
67   try {
68     (new File(releasePropFile!=null ? releasePropFile : defaultReleasePropFile)).withInputStream { 
69      releaseProps.load(it)
70     }
71   } catch (Exception fileLoadError) {
72     throw new Error("Couldn't load release properties file "+(releasePropFile==null ? defaultReleasePropFile : "from custom location: releasePropFile"),fileLoadError);
73   }
74   ////
75   // Set JALVIEW_VERSION if it is not already set
76   if (findProperty("JALVIEW_VERSION")==null || "".equals(JALVIEW_VERSION)) {
77     JALVIEW_VERSION = releaseProps.get("jalview.version")
78   }
79
80   // essentials
81   bareSourceDir = string(source_dir)
82   sourceDir = string("${jalviewDir}/${bareSourceDir}")
83   resourceDir = string("${jalviewDir}/${resource_dir}")
84   bareTestSourceDir = string(test_source_dir)
85   testDir = string("${jalviewDir}/${bareTestSourceDir}")
86
87   classesDir = string("${jalviewDir}/${classes_dir}")
88
89   useClover = false
90
91   resourceClassesDir = classesDir
92
93   testSourceDir = testDir
94   testClassesDir = "${jalviewDir}/${test_output_dir}"
95
96   buildProperties = string("${classesDir}/${build_properties_file}")
97   getdownSetAppBaseProperty = false // whether to pass the appbase and appdistdir to the application
98
99   install4jApplicationName = "${jalview_name}"
100   
101   println("Using a ${CHANNEL} profile.")
102
103   additional_compiler_args = []
104   // configure classpath/args for j8/j11 compilation
105   if (JAVA_VERSION.equals("1.8")) {
106     JAVA_INTEGER_VERSION = string("8")
107     //libDir = j8libDir
108     libDir = j11libDir
109     libDistDir = j8libDir
110     compile_source_compatibility = 1.8
111     compile_target_compatibility = 1.8
112   } else if (JAVA_VERSION.equals("11")) {
113     JAVA_INTEGER_VERSION = string("11")
114     libDir = j11libDir
115     libDistDir = j11libDir
116     compile_source_compatibility = 11
117     compile_target_compatibility = 11
118   } else {
119     throw new GradleException("JAVA_VERSION=${JAVA_VERSION} not currently supported by Jalview")
120   }
121
122   resourceBuildDir = string("${buildDir}/resources")
123   resourcesBuildDir = string("${resourceBuildDir}/resources_build")
124   helpBuildDir = string("${resourceBuildDir}/help_build")
125   docBuildDir = string("${resourceBuildDir}/doc_build")
126
127   if (buildProperties == null) {
128     buildProperties = string("${resourcesBuildDir}/${build_properties_file}")
129   }
130   buildingHTML = string("${jalviewDir}/${doc_dir}/building.html")
131   helpParentDir = string("${jalviewDir}/${help_parent_dir}")
132   helpSourceDir = string("${helpParentDir}/${help_dir}")
133   helpFile = string("${helpBuildDir}/${help_dir}/help.jhm")
134
135   // ENDEXT
136 }
137
138
139 sourceSets {
140   main {
141     java {
142       srcDirs sourceDir
143       outputDir = file(classesDir)
144     }
145
146     resources {
147       srcDirs = [ resourcesBuildDir, docBuildDir, helpBuildDir ]
148     }
149
150     compileClasspath = files(sourceSets.main.java.outputDir)
151     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
152
153
154     compileClasspath = files(sourceSets.main.java.outputDir)
155     compileClasspath += fileTree(dir: "${jalviewDir}/${libDir}", include: ["*.jar"])
156
157     runtimeClasspath = compileClasspath
158     runtimeClasspath += files(sourceSets.main.resources.srcDirs)
159   }
160
161   test {
162     java {
163       srcDirs testSourceDir
164       outputDir = file(testClassesDir)
165     }
166
167     resources {
168       srcDirs = useClover ? sourceSets.clover.resources.srcDirs : sourceSets.main.resources.srcDirs
169     }
170
171     compileClasspath = files( sourceSets.test.java.outputDir )
172     compileClasspath += useClover ? sourceSets.clover.compileClasspath : sourceSets.main.compileClasspath
173     compileClasspath += fileTree(dir: "${jalviewDir}/${utils_dir}/testnglibs", include: ["**/*.jar"])
174
175     runtimeClasspath = compileClasspath
176     runtimeClasspath += files(sourceSets.test.resources.srcDirs)
177   }
178  /*  test {
179     java {
180       srcDirs testSourceDir
181       outputDir = file(testClassesDir)
182     }
183
184     resources {
185       srcDirs = sourceSets.main.resources.srcDirs
186     }
187
188     compileClasspath = files( sourceSets.test.java.outputDir )
189     compileClasspath += sourceSets.main.compileClasspath
190     compileClasspath += fileTree(dir: "${jalviewDir}/${utils_dir}/testnglibs", include: ["**   REMOVE_THIS_GAP  /*.jar"])
191
192     runtimeClasspath = compileClasspath
193   }
194 */
195 }
196
197
198 compileJava {
199   sourceCompatibility = compile_source_compatibility
200   targetCompatibility = compile_target_compatibility
201   options.compilerArgs = additional_compiler_args
202   doFirst {
203     print ("Setting target compatibility to "+compile_target_compatibility+"\n")
204   }
205 }
206
207
208 compileTestJava {
209   doFirst {
210     sourceCompatibility = compile_source_compatibility
211     targetCompatibility = compile_target_compatibility
212     options.compilerArgs = additional_compiler_args
213     print ("Setting target compatibility to "+targetCompatibility+"\n")
214   }
215 }
216
217
218 clean {
219   doFirst {
220     delete sourceSets.main.java.outputDir
221   }
222 }
223
224
225 cleanTest {
226   doFirst {
227     delete sourceSets.test.java.outputDir
228   }
229 }
230
231
232 // format is a string like date.format("dd MMMM yyyy")
233 def getDate(format) {
234   def date = new Date()
235   return date.format(format)
236 }
237
238
239 task copyDocs(type: Copy) {
240   def inputDir = "${jalviewDir}/${doc_dir}"
241   def outputDir = "${docBuildDir}/${doc_dir}"
242   from(inputDir) {
243     include('**/*.txt')
244     include('**/*.md')
245     include('**/*.html')
246     include('**/*.xml')
247     filter(ReplaceTokens,
248       beginToken: '$$',
249       endToken: '$$',
250       tokens: [
251         'Version-Rel': JALVIEW_VERSION,
252         'Year-Rel': getDate("yyyy")
253       ]
254     )
255   }
256   from(inputDir) {
257     exclude('**/*.txt')
258     exclude('**/*.md')
259     exclude('**/*.html')
260     exclude('**/*.xml')
261   }
262   into outputDir
263
264   inputs.dir(inputDir)
265   outputs.dir(outputDir)
266 }
267
268
269 task copyHelp(type: Copy) {
270   def inputDir = helpSourceDir
271   def outputDir = "${helpBuildDir}/${help_dir}"
272   from(inputDir) {
273     include('**/*.txt')
274     include('**/*.md')
275     include('**/*.html')
276     include('**/*.hs')
277     include('**/*.xml')
278     include('**/*.jhm')
279     filter(ReplaceTokens,
280       beginToken: '$$',
281       endToken: '$$',
282       tokens: [
283         'Version-Rel': JALVIEW_VERSION,
284         'Year-Rel': getDate("yyyy")
285       ]
286     )
287   }
288   from(inputDir) {
289     exclude('**/*.txt')
290     exclude('**/*.md')
291     exclude('**/*.html')
292     exclude('**/*.hs')
293     exclude('**/*.xml')
294     exclude('**/*.jhm')
295   }
296   into outputDir
297
298   inputs.dir(inputDir)
299   outputs.files(helpFile)
300   outputs.dir(outputDir)
301 }
302
303
304 task copyResources(type: Copy) {
305   group = "build"
306   description = "Copy (and make text substitutions in) the resources dir to the build area"
307
308   def inputDir = resourceDir
309   def outputDir = resourcesBuildDir
310   from(inputDir) {
311     include('**/*.txt')
312     include('**/*.md')
313     include('**/*.html')
314     include('**/*.xml')
315     filter(ReplaceTokens,
316       beginToken: '$$',
317       endToken: '$$',
318       tokens: [
319         'Version-Rel': JALVIEW_VERSION,
320         'Year-Rel': getDate("yyyy")
321       ]
322     )
323   }
324   from(inputDir) {
325     exclude('**/*.txt')
326     exclude('**/*.md')
327     exclude('**/*.html')
328     exclude('**/*.xml')
329   }
330   into outputDir
331
332   inputs.dir(inputDir)
333   outputs.dir(outputDir)
334 }
335
336 task copyChannelResources(type: Copy) {
337   dependsOn copyResources
338   group = "build"
339   description = "Copy the channel resources dir to the build resources area"
340
341   def inputDir = "${channelDir}/${resource_dir}"
342   def outputDir = resourcesBuildDir
343   from inputDir
344   into outputDir
345
346   inputs.dir(inputDir)
347   outputs.dir(outputDir)
348 }
349
350 task createBuildProperties(type: Copy) {
351   // using the build_properties already included in the source tarball
352   def inputFile = "build_properties"
353   def outputFile = buildProperties
354   from inputFile
355   into file(outputFile).getParent()
356   rename(file(inputFile).getName(), file(outputFile).getName())
357
358   inputs.file(inputFile)
359   outputs.file(outputFile)
360 }
361
362
363 task buildIndices(type: JavaExec) {
364   dependsOn copyHelp
365   classpath = sourceSets.main.compileClasspath
366   main = "com.sun.java.help.search.Indexer"
367   workingDir = "${helpBuildDir}/${help_dir}"
368   def argDir = "html"
369   args = [ argDir ]
370   inputs.dir("${workingDir}/${argDir}")
371
372   outputs.dir("${classesDir}/doc")
373   outputs.dir("${classesDir}/help")
374   outputs.file("${workingDir}/JavaHelpSearch/DOCS")
375   outputs.file("${workingDir}/JavaHelpSearch/DOCS.TAB")
376   outputs.file("${workingDir}/JavaHelpSearch/OFFSETS")
377   outputs.file("${workingDir}/JavaHelpSearch/POSITIONS")
378   outputs.file("${workingDir}/JavaHelpSearch/SCHEMA")
379   outputs.file("${workingDir}/JavaHelpSearch/TMAP")
380 }
381
382 task buildResources {
383   dependsOn copyResources
384   dependsOn copyChannelResources
385   dependsOn createBuildProperties
386 }
387
388 task prepare {
389   dependsOn buildResources
390   dependsOn copyDocs
391   dependsOn copyHelp
392   dependsOn buildIndices
393 }
394
395
396 compileJava.dependsOn prepare
397 run.dependsOn compileJava
398 //run.dependsOn prepare
399
400
401 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
402 test {
403   dependsOn prepare
404   dependsOn compileJava //?
405
406   useTestNG() {
407     includeGroups testng_groups
408     excludeGroups testng_excluded_groups
409     preserveOrder true
410     useDefaultListeners=true
411   }
412
413   maxHeapSize = "1024m"
414
415   workingDir = jalviewDir
416   //systemProperties 'clover.jar' System.properties.clover.jar
417   def testLaf = project.findProperty("test_laf")
418   if (testLaf != null) {
419     println("Setting Test LaF to '${testLaf}'")
420     systemProperty "laf", testLaf
421   }
422   def testHiDPIScale = project.findProperty("test_HiDPIScale")
423   if (testHiDPIScale != null) {
424     println("Setting Test HiDPI Scale to '${testHiDPIScale}'")
425     systemProperty "sun.java2d.uiScale", testHiDPIScale
426   }
427   sourceCompatibility = compile_source_compatibility
428   targetCompatibility = compile_target_compatibility
429   jvmArgs += additional_compiler_args
430
431   doFirst {
432   }
433 }
434
435
436 task compileLinkCheck(type: JavaCompile) {
437   options.fork = true
438   classpath = files("${jalviewDir}/${utils_dir}")
439   destinationDir = file("${jalviewDir}/${utils_dir}")
440   source = fileTree(dir: "${jalviewDir}/${utils_dir}", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
441
442   inputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.java")
443   inputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.java")
444   outputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.class")
445   outputs.file("${jalviewDir}/${utils_dir}/BufferedLineReader.class")
446 }
447
448
449 task linkCheck(type: JavaExec) {
450   dependsOn prepare
451   dependsOn compileLinkCheck
452
453   def helpLinksCheckerOutFile = file("${jalviewDir}/${utils_dir}/HelpLinksChecker.out")
454   classpath = files("${jalviewDir}/${utils_dir}")
455   main = "HelpLinksChecker"
456   workingDir = jalviewDir
457   args = [ "${helpBuildDir}/${help_dir}", "-nointernet" ]
458
459   def outFOS = new FileOutputStream(helpLinksCheckerOutFile, false) // false == don't append
460   standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
461     outFOS,
462     System.out)
463   errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
464     outFOS,
465     System.err)
466
467   inputs.dir(helpBuildDir)
468   outputs.file(helpLinksCheckerOutFile)
469 }
470
471
472 // import the pubhtmlhelp target
473 ant.properties.basedir = "${jalviewDir}"
474 ant.properties.helpBuildDir = "${helpBuildDir}/${help_dir}"
475 ant.importBuild "${utils_dir}/publishHelp.xml"
476
477
478 task cleanPackageDir(type: Delete) {
479   doFirst {
480     delete fileTree(dir: "${jalviewDir}/${package_dir}", include: "*.jar")
481   }
482 }
483
484
485 jar {
486   dependsOn prepare
487   dependsOn linkCheck
488
489   manifest {
490     attributes "Main-Class": main_class,
491     "Permissions": "all-permissions",
492     "Application-Name": install4jApplicationName,
493     "Codebase": application_codebase,
494     "Implementation-Version": JALVIEW_VERSION
495   }
496
497   def outputDir = "${jalviewDir}/${package_dir}"
498   destinationDirectory = file(outputDir)
499   archiveFileName = rootProject.name+".jar"
500
501   exclude "cache*/**"
502   exclude "*.jar"
503   exclude "*.jar.*"
504   exclude "**/*.jar"
505   exclude "**/*.jar.*"
506
507   inputs.dir(sourceSets.main.java.outputDir)
508   sourceSets.main.resources.srcDirs.each{ dir ->
509     inputs.dir(dir)
510   }
511
512   outputs.file("${outputDir}/${archiveFileName}")
513 }
514