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