JAL-3830 JAL-3421 Conda launch script for 2.11.3 and new CLI with OS compatibility...
[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 task releasesTemplates {
304   group "help"
305   description "Recreate whatsNew.html and releases.html from markdown files and templates in help"
306
307   dependsOn copyHelp
308
309   def releasesTemplateFile = file("${jalviewDir}/${releases_template}")
310   def whatsnewTemplateFile = file("${jalviewDir}/${whatsnew_template}")
311   def releasesHtmlFile = file("${helpBuildDir}/${help_dir}/${releases_html}")
312   def whatsnewHtmlFile = file("${helpBuildDir}/${help_dir}/${whatsnew_html}")
313   def releasesMdDir = "${jalviewDir}/${releases_dir}"
314   def whatsnewMdDir = "${jalviewDir}/${whatsnew_dir}"
315
316   doFirst {
317     def JALVIEW_VERSION_UNDERSCORES = JALVIEW_VERSION.replaceAll("\\.", "_")
318     def releaseMdFile = file("${releasesMdDir}/release-${JALVIEW_VERSION_UNDERSCORES}.md")
319     def whatsnewMdFile = file("${whatsnewMdDir}/whatsnew-${JALVIEW_VERSION_UNDERSCORES}.md")
320
321     if (CHANNEL == "RELEASE") {
322       if (!releaseMdFile.exists()) {
323         throw new GradleException("File ${releaseMdFile} must be created for RELEASE")
324       }
325       if (!whatsnewMdFile.exists()) {
326         throw new GradleException("File ${whatsnewMdFile} must be created for RELEASE")
327       }
328     }
329
330     def releaseFiles = fileTree(dir: releasesMdDir, include: "release-*.md")
331     def releaseFilesDates = releaseFiles.collectEntries {
332       [(it): getDate("")]
333     }
334     releaseFiles = releaseFiles.sort { a,b -> releaseFilesDates[a].compareTo(releaseFilesDates[b]) }
335
336     def releasesTemplate = releasesTemplateFile.text
337     def m = releasesTemplate =~ /(?s)__VERSION_LOOP_START__(.*)__VERSION_LOOP_END__/
338     def versionTemplate = m[0][1]
339
340     def versionsHtml = ""
341     def linkedVersions = []
342
343     releasesTemplate = releasesTemplate.replaceAll("(?s)__VERSION_LOOP_START__.*__VERSION_LOOP_END__", versionsHtml)
344     releasesHtmlFile.text = releasesTemplate
345
346     whatsnewHtmlFile.text = "Debian build " + getDate("yyyy-MM-dd HH:mm:ss")
347   }
348
349   inputs.file(releasesTemplateFile)
350   inputs.file(whatsnewTemplateFile)
351   inputs.dir(releasesMdDir)
352   inputs.dir(whatsnewMdDir)
353   outputs.file(releasesHtmlFile)
354   outputs.file(whatsnewHtmlFile)
355 }
356
357 task copyResources(type: Copy) {
358   group = "build"
359   description = "Copy (and make text substitutions in) the resources dir to the build area"
360
361   def inputDir = resourceDir
362   def outputDir = resourcesBuildDir
363   from(inputDir) {
364     include('**/*.txt')
365     include('**/*.md')
366     include('**/*.html')
367     include('**/*.xml')
368     filter(ReplaceTokens,
369       beginToken: '$$',
370       endToken: '$$',
371       tokens: [
372         'Version-Rel': JALVIEW_VERSION,
373         'Year-Rel': getDate("yyyy")
374       ]
375     )
376   }
377   from(inputDir) {
378     exclude('**/*.txt')
379     exclude('**/*.md')
380     exclude('**/*.html')
381     exclude('**/*.xml')
382   }
383   into outputDir
384
385   inputs.dir(inputDir)
386   outputs.dir(outputDir)
387 }
388
389 task copyChannelResources(type: Copy) {
390   dependsOn copyResources
391   group = "build"
392   description = "Copy the channel resources dir to the build resources area"
393
394   def inputDir = "${channelDir}/${resource_dir}"
395   def outputDir = resourcesBuildDir
396   from inputDir
397   into outputDir
398
399   inputs.dir(inputDir)
400   outputs.dir(outputDir)
401 }
402
403 task createBuildProperties(type: Copy) {
404   // using the build_properties already included in the source tarball
405   def inputFile = "build_properties"
406   def outputFile = buildProperties
407   from inputFile
408   into file(outputFile).getParent()
409   rename(file(inputFile).getName(), file(outputFile).getName())
410
411   inputs.file(inputFile)
412   outputs.file(outputFile)
413 }
414
415
416 task buildIndices(type: JavaExec) {
417   dependsOn copyHelp
418   classpath = sourceSets.main.compileClasspath
419   main = "com.sun.java.help.search.Indexer"
420   workingDir = "${helpBuildDir}/${help_dir}"
421   def argDir = "html"
422   args = [ argDir ]
423   inputs.dir("${workingDir}/${argDir}")
424
425   outputs.dir("${classesDir}/doc")
426   outputs.dir("${classesDir}/help")
427   outputs.file("${workingDir}/JavaHelpSearch/DOCS")
428   outputs.file("${workingDir}/JavaHelpSearch/DOCS.TAB")
429   outputs.file("${workingDir}/JavaHelpSearch/OFFSETS")
430   outputs.file("${workingDir}/JavaHelpSearch/POSITIONS")
431   outputs.file("${workingDir}/JavaHelpSearch/SCHEMA")
432   outputs.file("${workingDir}/JavaHelpSearch/TMAP")
433 }
434
435 task buildResources {
436   dependsOn copyResources
437   dependsOn copyChannelResources
438   dependsOn createBuildProperties
439 }
440
441 task prepare {
442   dependsOn buildResources
443   dependsOn copyDocs
444   dependsOn copyHelp
445   dependsOn releasesTemplates
446   dependsOn buildIndices
447 }
448
449
450 compileJava.dependsOn prepare
451 run.dependsOn compileJava
452 //run.dependsOn prepare
453
454
455 //testReportDirName = "test-reports" // note that test workingDir will be $jalviewDir
456 test {
457   dependsOn prepare
458   dependsOn compileJava //?
459
460   useTestNG() {
461     includeGroups testng_groups
462     excludeGroups testng_excluded_groups
463     preserveOrder true
464     useDefaultListeners=true
465   }
466
467   maxHeapSize = "1024m"
468
469   workingDir = jalviewDir
470   //systemProperties 'clover.jar' System.properties.clover.jar
471   def testLaf = project.findProperty("test_laf")
472   if (testLaf != null) {
473     println("Setting Test LaF to '${testLaf}'")
474     systemProperty "laf", testLaf
475   }
476   def testHiDPIScale = project.findProperty("test_HiDPIScale")
477   if (testHiDPIScale != null) {
478     println("Setting Test HiDPI Scale to '${testHiDPIScale}'")
479     systemProperty "sun.java2d.uiScale", testHiDPIScale
480   }
481   sourceCompatibility = compile_source_compatibility
482   targetCompatibility = compile_target_compatibility
483   jvmArgs += additional_compiler_args
484
485   doFirst {
486   }
487 }
488
489
490 task compileLinkCheck(type: JavaCompile) {
491   options.fork = true
492   classpath = files("${jalviewDir}/${utils_dir}")
493   destinationDir = file("${jalviewDir}/${utils_dir}")
494   source = fileTree(dir: "${jalviewDir}/${utils_dir}", include: ["HelpLinksChecker.java", "BufferedLineReader.java"])
495
496   inputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.java")
497   inputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.java")
498   outputs.file("${jalviewDir}/${utils_dir}/HelpLinksChecker.class")
499   outputs.file("${jalviewDir}/${utils_dir}/BufferedLineReader.class")
500 }
501
502
503 task linkCheck(type: JavaExec) {
504   dependsOn prepare
505   dependsOn compileLinkCheck
506
507   def helpLinksCheckerOutFile = file("${jalviewDir}/${utils_dir}/HelpLinksChecker.out")
508   classpath = files("${jalviewDir}/${utils_dir}")
509   main = "HelpLinksChecker"
510   workingDir = "${helpBuildDir}"
511   args = [ "${helpBuildDir}/${help_dir}", "-nointernet" ]
512
513   def outFOS = new FileOutputStream(helpLinksCheckerOutFile, false) // false == don't append
514   standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
515     outFOS,
516     System.out)
517   errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
518     outFOS,
519     System.err)
520
521   inputs.dir(helpBuildDir)
522   outputs.file(helpLinksCheckerOutFile)
523 }
524
525
526 // import the pubhtmlhelp target
527 ant.properties.basedir = "${jalviewDir}"
528 ant.properties.helpBuildDir = "${helpBuildDir}/${help_dir}"
529 ant.importBuild "${utils_dir}/publishHelp.xml"
530
531
532 task cleanPackageDir(type: Delete) {
533   doFirst {
534     delete fileTree(dir: "${jalviewDir}/${package_dir}", include: "*.jar")
535   }
536 }
537
538
539 jar {
540   dependsOn prepare
541   dependsOn linkCheck
542
543   manifest {
544     attributes "Main-Class": main_class,
545     "Permissions": "all-permissions",
546     "Application-Name": install4jApplicationName,
547     "Codebase": application_codebase,
548     "Implementation-Version": JALVIEW_VERSION
549   }
550
551   def outputDir = "${jalviewDir}/${package_dir}"
552   destinationDirectory = file(outputDir)
553   archiveFileName = rootProject.name+".jar"
554
555   exclude "cache*/**"
556   exclude "*.jar"
557   exclude "*.jar.*"
558   exclude "**/*.jar"
559   exclude "**/*.jar.*"
560
561   inputs.dir(sourceSets.main.java.outputDir)
562   sourceSets.main.resources.srcDirs.each{ dir ->
563     inputs.dir(dir)
564   }
565
566   outputs.file("${outputDir}/${archiveFileName}")
567 }
568