dec0765052989b8c48bf411f85099431a57bbe5b
[jalview.git] / build.gradle
1 import org.apache.tools.ant.filters.ReplaceTokens
2 import org.gradle.internal.os.OperatingSystem
3 import org.gradle.plugins.ide.eclipse.model.*
4 import groovy.transform.ExternalizeMethods
5 import org.eclipse.osgi.*
6
7 /*
8 buildscript {
9   repositories {
10     repos
11     mavenCentral()
12     jcenter()
13   }
14   dependencies {
15     //eclipseDeps.each { compile "p2:${it}:+" }
16     //classpath 'org.eclipse.platform:org.eclipse.osgi:3.15.0'
17     //compile group: 'org.eclipse.platform', name: 'org.eclipse.osgi', version: '3.15.0'
18     //classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:4.4.0'
19   }
20 }
21 */
22
23 plugins {
24   id 'java'
25   id 'application'
26   id 'eclipse'
27   id 'com.diffplug.gradle.oomph.ide' version '3.18.1'
28   id 'com.diffplug.gradle.equinoxlaunch' version '3.18.1'
29
30   //id 'com.diffplug.gradle.p2.asmaven' version '3.18.1'
31
32   //id 'org.eclipse.osgi' version '3.15.0'
33   //id 'osgi'
34 }
35
36
37 /*
38 def eclipseDeps = [
39     // The dependencies we actually use
40     'org.eclipse.jdt.core',
41     'org.eclipse.text',
42
43     // Their transitives
44     'org.eclipse.core.contenttype',
45     'org.eclipse.core.jobs',
46     'org.eclipse.core.runtime',
47     'org.eclipse.core.resources',
48     'org.eclipse.equinox.common',
49     'org.eclipse.equinox.preferences',
50     //'org.eclipse.osgi'
51 ]
52 */
53
54 /*
55 p2AsMaven {
56   group 'p2', {
57     repoEclipse goomph_eclipse_version
58     eclipseDeps.each { p2.addIU(it) }
59     eclipseDeps.each { p2.addIU(it + '.source') }
60   }
61 }
62 */
63
64
65 /*
66 dependencies {
67   //eclipseDeps.each { compile "p2:${it}:+" }
68   //implementation 'org.eclipse.platform:org.eclipse.osgi:3.15.0'
69   //compile 'org.eclipse.platform:org.eclipse.osgi:3.15.0'
70   //classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:4.4.0'
71 }
72 */
73
74
75
76 repositories {
77   jcenter()
78   mavenCentral()
79   flatDir {
80     dirs gradlePluginsDir
81   }
82 }
83
84 mainClassName = launcherClass
85 def classes = "$jalviewDir/$classesDir"
86
87 // configure classpath/args for j8/j11 compilation
88
89 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
90 def libDir
91 def libDistDir
92 def compile_source_compatibility
93 def compile_target_compatibility
94
95 ext {
96   buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
97
98   gitHash = ""
99   gitBranch = ""
100
101   jalviewjsServer = ""
102 }
103
104 def JAVA_INTEGER_VERSION
105 //def additional_compiler_args = []
106 // this property is for the Java library used in eclipse
107 def eclipse_java_runtime_name
108 if (JAVA_VERSION.equals("1.8")) {
109   JAVA_INTEGER_VERSION = "8"
110   //libDir = j8libDir
111   libDir = j11libDir
112   libDistDir = j8libDir
113   compile_source_compatibility = 1.8
114   compile_target_compatibility = 1.8
115   eclipse_java_runtime_name = "JavaSE-1.8"
116 } else if (JAVA_VERSION.equals("11")) {
117   JAVA_INTEGER_VERSION = "11"
118   libDir = j11libDir
119   libDistDir = j11libDir
120   compile_source_compatibility = 11
121   compile_target_compatibility = 11
122   eclipse_java_runtime_name = "JavaSE-11"
123   /* compile without modules -- using classpath libraries
124   additional_compiler_args += [
125   '--module-path', ext.modules_compileClasspath.asPath,
126   '--add-modules', j11modules
127   ]
128   */
129 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
130   JAVA_INTEGER_VERSION = JAVA_VERSION
131   libDir = j11libDir
132   libDistDir = j11libDir
133   compile_source_compatibility = JAVA_VERSION
134   compile_target_compatibility = JAVA_VERSION
135   eclipse_java_runtime_name = "JavaSE-11"
136   /* compile without modules -- using classpath libraries
137   additional_compiler_args += [
138   '--module-path', ext.modules_compileClasspath.asPath,
139   '--add-modules', j11modules
140   ]
141   */
142 } else {
143   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
144 }
145
146 sourceSets {
147
148   main {
149     java {
150       srcDirs "$jalviewDir/$sourceDir"
151       outputDir = file("$classes")
152     }
153
154     resources {
155       srcDirs "$jalviewDir/$resourceDir"
156     }
157
158     compileClasspath = files(sourceSets.main.java.outputDir)
159     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
160
161     runtimeClasspath = compileClasspath
162   }
163
164 }
165
166 eclipse {
167   project {
168     name = eclipse_project_name
169
170     natures 'org.eclipse.jdt.core.javanature',
171     'org.eclipse.buildship.core.gradleprojectnature'
172
173     buildCommand 'org.eclipse.jdt.core.javabuilder'
174     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
175   }
176
177   classpath {
178     //defaultOutputDir = sourceSets.main.java.outputDir
179     def removeThese = []
180     configurations.each{ if (it.isCanBeResolved()) {
181         removeThese += it
182       }
183     }
184
185     minusConfigurations += removeThese
186     plusConfigurations = [ ]
187     file {
188
189       whenMerged { cp ->
190         def removeTheseToo = []
191         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
192         cp.entries.each { entry ->
193           if (entry.kind == 'src') {
194             if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
195               removeTheseToo += entry
196             } else {
197               addedSrcPath.putAt(entry.path, true)
198             }
199           }
200         }
201         cp.entries.removeAll(removeTheseToo)
202
203         if (file(eclipse_bin_dir+"/main").isDirectory()) {
204                 cp.entries += new Output(eclipse_bin_dir+"/main")
205         }
206         if (file(helpParentDir).isDirectory()) {
207                 cp.entries += new Library(fileReference(helpParentDir))
208         }
209         if (file(resourceDir).isDirectory()) {
210                 cp.entries += new Library(fileReference(resourceDir))
211         }
212
213         HashMap<String, Boolean> addedLibPath = new HashMap<>();
214
215         // changing from sourcesets.main.classpath to specific Java version lib
216         //sourceSets.main.compileClasspath.each{
217         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
218           //don't want to add outputDir as eclipse is using its own output dir in bin/main
219           if (it.isDirectory() || ! it.exists()) {
220             // don't add dirs to classpath
221             return
222           }
223           def itPath = it.toString()
224           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
225             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
226           }
227           if (addedLibPath.get(itPath)) {
228             //println("Not adding duplicate entry "+itPath)
229           } else {
230             //println("Adding entry "+itPath)
231             cp.entries += new Library(fileReference(itPath))
232             addedLibPath.put(itPath, true)
233           }
234         }
235       } // whenMerged
236
237     } // file
238
239     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
240   } // classpath
241
242   jdt {
243     // for the IDE, use java 11 compatibility
244     sourceCompatibility = compile_source_compatibility
245     targetCompatibility = compile_target_compatibility
246     javaRuntimeName = eclipse_java_runtime_name
247
248     /*
249     file {
250     withProperties { props ->
251     def jalview_prefs = new Properties()
252     def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
253     jalview_prefs.load(ins)
254     ins.close()
255     jalview_prefs.forEach { t, v ->
256     if (props.getAt(t) == null) {
257     props.putAt(t, v)
258     }
259     }
260     }
261     }
262      */
263   } // jdt
264
265   //synchronizationTasks eclipseClasspath
266   //autoBuildTasks eclipseClasspath
267 } // eclipse
268
269 /*
270 compileJava {
271
272   doFirst {
273     sourceCompatibility = compile_source_compatibility
274     targetCompatibility = compile_target_compatibility
275     //options.compilerArgs = additional_compiler_args
276     print ("Setting target compatibility to "+targetCompatibility+"\n")
277   }
278
279 }
280
281 clean {
282   delete sourceSets.main.java.outputDir
283 }
284 */
285
286
287
288
289
290
291
292
293
294
295
296 oomphIde {
297   repoEclipse goomph_eclipse_version
298   jdt {
299     /*
300     installedJre {
301       //markDefault = true
302       //executionEnvironments = [ eclipse.jdt.javaRuntimeName ]
303     }
304     */
305     //compilerComplianceLevel( JAVA_INTEGER_VERSION )
306   }
307   //repo 'http://download.eclipse.org/buildship/updates/e45/milestones/3.x/'
308   repo goomph_repo_buildship
309         feature goomph_feature_buildship
310 }
311
312 equinoxLaunch {
313   headlessAppSetup {
314
315     launchTask 'ideJalviewjsBuild', {
316       //it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
317       it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
318     }
319   }
320 }
321
322 task ideCopyDropins (type: Copy) {
323   dependsOn ideSetupP2
324
325   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir)
326   def outputDir = oomphIde.ideDir + "/" + com.diffplug.gradle.FileMisc.macContentsEclipse() + "/dropins"
327
328   from inputFiles
329   into outputDir
330   def outputFiles = []
331   rename { filename ->
332     outputFiles += outputDir+"/"+filename
333     println("COPYING ${filename} to ${outputFiles}")
334     null
335   }
336   outputs.files outputFiles
337   inputs.files inputFiles
338
339 }
340
341 ide.dependsOn eclipseProject
342 ide.dependsOn eclipseClasspath
343 ide.dependsOn eclipseJdt
344 ide.dependsOn ideCopyDropins
345 ideJalviewjsBuild.dependsOn ideSetupWorkspace
346 ideJalviewjsBuild.dependsOn ideCopyDropins
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 def tempEclipseWorkspace = ""
362
363 task jalviewjsSetTempEclipseWorkspace {
364   tempEclipseWorkspace = file(jalviewjs_eclipse_workspace)
365   if (!tempEclipseWorkspace.exists()) {
366     tempEclipseWorkspace = File.createTempDir()
367     tempEclipseWorkspace.deleteOnExit()
368   }
369   println("ECLIPSE WORKSPACE: "+tempEclipseWorkspace.getPath())
370 }
371
372 task jalviewjsUnzipFiles {
373   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
374   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
375
376   doLast {
377     zipFiles.each { file_zip -> 
378       copy {
379         from zipTree(file_zip)
380         into jalviewjs_site_dir
381       }
382     }
383   }
384
385   inputs.files zipFiles
386   outputs.dir jalviewjs_site_dir
387 }
388
389 def eclipseBinary
390 def eclipseDropinsDir
391 def eclipsePluginsDir
392 task jalviewjsEclipsePaths {
393   def eclipseRoot = jalviewjs_eclipse_root
394   if (eclipseRoot.startsWith("~")) {
395     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
396   }
397         if (OperatingSystem.current().isMacOsX()) {
398     eclipseRoot += "/Eclipse.app"
399                 eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
400                 eclipsePluginsDir = eclipseRoot+"/Contents/Eclipse/plugins"
401                 eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
402         } else if (OperatingSystem.current().isWindows()) { // check these paths!!
403                 eclipseDropinsDir = eclipseRoot+"/dropins"
404                 eclipsePluginsDir = eclipseRoot+"/plugins"
405                 eclipseBinary = eclipseRoot+"/eclipse"
406         } else { // linux or unix
407                 eclipseDropinsDir = eclipseRoot+"/dropins"
408                 eclipsePluginsDir = eclipseRoot+"/plugins"
409                 eclipseBinary = eclipseRoot+"/eclipse"
410         }
411   println("ECLIPSE ROOT: "+eclipseRoot)
412 }
413
414 task jalviewjsEclipseCopyDropins (type: Copy) {
415   dependsOn jalviewjsEclipsePaths
416   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir)
417   def outputDir = eclipseDropinsDir
418
419   from inputFiles
420   into outputDir
421   def outputFiles = []
422   rename { filename ->
423     outputFiles += outputDir+"/"+filename
424     null
425   }
426   outputs.files outputFiles
427   inputs.files inputFiles
428 }
429
430 task jalviewjsEclipseCopyPlugins (type: Copy) {
431   dependsOn jalviewjsEclipsePaths
432   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_plugins_dir)
433   def outputDir = eclipsePluginsDir
434
435   from inputFiles
436   into outputDir
437   def outputFiles = []
438   rename { filename ->
439     outputFiles += outputDir+"/"+filename
440     null
441   }
442   outputs.files outputFiles
443   inputs.files inputFiles
444 }
445
446 task jalviewjsEclipseSetup {
447   dependsOn jalviewjsEclipseCopyDropins
448   dependsOn jalviewjsEclipseCopyPlugins
449   dependsOn jalviewjsSetTempEclipseWorkspace
450 }
451
452
453 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
454   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
455   def props = project.properties.sort { it.key }
456   props.each { prop, val ->
457     if (prop.startsWith("j2s.") && val != null) {
458       property(prop,val)
459     }
460   }
461   outputs.file(outputFile)
462 }
463
464 task jalviewjsCopyResources (type: Copy) {
465   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
466   def outputDir = jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir
467
468   from inputFiles
469   into outputDir
470   def outputFiles = []
471   rename { filename ->
472     outputFiles += outputDir+"/"+filename
473     null
474   }
475   outputs.files outputFiles
476   inputs.files inputFiles
477 }
478
479 task jalviewjsCopySiteResources (type: Copy) {
480   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
481   def outputDir = jalviewjs_site_dir
482
483   from inputFiles
484   into outputDir
485   def outputFiles = []
486   rename { filename ->
487     outputFiles += outputDir+"/"+filename
488     null
489   }
490   outputs.files outputFiles
491   inputs.files inputFiles
492 }
493
494 task cleanJalviewjs {
495   /*
496   delete jalviewDir+"/"+jalviewjs_site_dir
497   delete jalviewDir+"/"+eclipse_bin_dir
498   delete file(tempEclipseWorkspace.getAbsolutePath()+"/.metadata")
499   delete jalviewDir+"/"+jalviewjs_j2s_settings
500   */
501 }
502
503 task jalviewjsProjectImport(type: Exec) {
504   // work out how to do this!
505   dependsOn eclipseProject
506   dependsOn eclipseClasspath
507   dependsOn eclipseJdt
508   dependsOn jalviewjsEclipsePaths
509   dependsOn jalviewjsEclipseSetup
510   executable(eclipseBinary)
511   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", tempEclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
512
513   def projdir = tempEclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core/"
514   inputs.file(jalviewDir+"/.project")
515   outputs.dir(projdir)
516 }
517
518 task jalviewjsTranspile(type: Exec) {
519   dependsOn jalviewjsCreateJ2sSettings 
520   dependsOn jalviewjsProjectImport
521   dependsOn jalviewjsEclipsePaths
522   executable(eclipseBinary)
523   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
524   inputs.dir(sourceDir)
525   outputs.dir(eclipse_bin_dir+"/main")
526   outputs.file(eclipse_bin_dir+"/main/jalview/bin/Jalview.class")
527   outputs.dir(jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir)
528 }
529
530 task jalviewjsBuildSite {
531   dependsOn jalviewjsUnzipFiles
532   dependsOn jalviewjsCopyResources
533   dependsOn jalviewjsCopySiteResources
534   dependsOn jalviewjsTranspile
535 }
536
537 task jalviewjsSiteTar(type: Tar) {
538   dependsOn jalviewjsBuildSite
539   def outputFilename = "site.tar.gz"
540   try {
541     archiveFileName = outputFilename
542   } catch (Exception e) {
543     archiveName = outputFilename
544   }
545
546   compression Compression.GZIP
547
548   from jalviewjs_site_dir
549   into jalviewjs_site_dir // this is inside the tar file
550
551   inputs.dir(jalviewjs_site_dir)
552 }
553
554 task jalviewjs {
555   dependsOn jalviewjsBuildSite
556 }
557
558 def jalviewjsServer = null
559 task jalviewjsServerStart {
560   doLast {
561
562     if (jalviewjsServer != null) {
563       println("SERVER ALREADY RUNNING. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle jalviewjsServerStop  to stop.")
564     } else {
565
566       SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
567       def port = Integer.valueOf(jalviewjs_server_port)
568       def start = port
569       def running = false
570       while(port < start+1000 && !running) {
571         try {
572           jalviewjsServer = factory.start(new File(jalviewDirAbsolutePath+"/"+jalviewjs_site_dir), port)
573           running = true
574           println("SERVER STARTED. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle jalviewjsServerStop  to stop.")
575           //println("Ctrl-c to stop.");java.lang.Thread.sleep(Integer.valueOf(jalviewjs_server_wait)*1000);
576         } catch (Exception e) {
577           port++;
578         }
579       }
580
581     }
582
583   }
584
585 }
586
587 /* server persists in gradle daemon but reference is not accessible across builds
588 task jalviewjsServerStop {
589   doLast {
590
591     if (jalviewjsServer != null) {
592
593       println("SERVER ON PORT "+jalviewjsServer.getPort()+" STOPPING. Run  gradle jalviewjsServerStart  to start again.")
594       jalviewjsServer.stop()
595
596     } else {
597       println("SERVER NOT RUNNING. Run  gradle jalviewjsServerStart  to start.")
598     }
599
600   }
601 }
602 */
603
604 project.afterEvaluate {
605   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseProject
606   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseClasspath
607   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseJdt
608 }
609
610
611