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