JAL-3210 ... a bit closer ... goomph jalviewjs build
[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 com.diffplug.gradle.GoomphCacheLocations
6
7 buildscript {
8   repositories {
9     /*
10     mavenCentral()
11     jcenter()
12     */
13         mavenLocal()
14   }
15 }
16
17 plugins {
18   id 'java'
19   id 'application'
20   id 'eclipse'
21   id 'com.diffplug.gradle.oomph.ide' version '3.18.1'
22   id 'com.diffplug.gradle.equinoxlaunch' version '3.18.1'
23   id 'com.diffplug.gradle.eclipse.mavencentral' version '3.18.1'
24   id 'com.diffplug.gradle.p2.asmaven' version '3.18.1'
25 }
26
27 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
28
29 repositories {
30   jcenter()
31   mavenCentral()
32   //maven { url "http://maven.icm.edu.pl/artifactory/repo/" }
33   //maven { url "http://maven.geomajas.org/" }
34   //maven { url "https://repo.spring.io/plugins-release/" }
35   //maven { url "http://maven.thingml.org/thirdparty/" }
36   //maven { url "${jalviewDirAbsolutePath}/${m2RepoDir}" }
37   //mavenLocal()
38   flatDir {
39     dirs gradlePluginsDir
40   }
41 }
42
43 configurations {
44   jalviewjsIde
45 }
46
47 p2AsMaven {
48   group 'eclipseDeps', {
49     repoEclipse goomph_eclipse_repo_version
50     slicingOption 'latestVersionOnly', 'true'
51     iu 'org.eclipse.update.configurator'
52   }
53   group 'eclipseOrbit', {
54     repo goomph_orbit_repo
55     slicingOption 'latestVersionOnly', 'true'
56     iu 'org.apache.ant'
57     iu 'org.apache.batik.css'
58     iu 'org.apache.commons.jxpath'
59     iu 'javax.servlet'
60   }
61 }
62
63 dependencies {
64     //compile group: 'org.osgi', name: 'org.osgi.service.component', version: '1.4.0'
65 /*
66     source.addMaven('org.eclipse.platform:org.eclipse.osgi:3.14.0')
67     source.addMaven('org.eclipse.platform:org.eclipse.equinox.common:3.10.400')
68     source.addMaven('org.eclipse.platform:org.eclipse.core.jobs:3.10.400')
69     source.addMaven('org.eclipse.platform:org.eclipse.update.configurator:3.4.300')
70     source.addMaven('org.eclipse.platform:org.eclipse.core.runtime:3.16.0')
71     source.addMaven('org.eclipse.platform:org.eclipse.equinox.ds:1.6.0')
72     source.addMaven('javax.inject:com.springsource.javax.inject:1.0.0')
73 */
74
75   //compile 'eclipseOrbit:org.apache.ant:1.10.5.v20190526-1402'
76   //compile 'eclipseOrbit:javax.servlet:3.1.0.v201410161800'
77   //compile 'eclipseDeps:net.sf.j2s.core:3.2.4'
78   //compile 'eclipseDeps:com.seeq.eclipse.importprojects:1.4.0'
79
80   //compile 'eclipseDeps:org.eclipse.update.configurator:3.3.400'
81
82   
83 }
84
85
86
87
88
89
90 eclipseMavenCentral {
91   release goomph_eclipse_repo_version, {
92
93     //def platformCode="cocoa.macosx.x86_64"
94     //compile 'org.eclipse.swt.'+platformCode
95     useNativesForRunningPlatform()
96     compile 'org.eclipse.swt'
97
98     compile 'org.eclipse.ant.core'
99     compile 'org.eclipse.compare.core'
100     compile 'org.eclipse.core.commands'
101     compile 'org.eclipse.core.contenttype'
102     compile 'org.eclipse.core.jobs'
103     compile 'org.eclipse.core.expressions'
104     compile 'org.eclipse.core.filesystem'
105     compile 'org.eclipse.core.resources'
106     compile 'org.eclipse.core.runtime'
107     compile 'org.eclipse.core.variables'
108     compile 'org.eclipse.e4.core.commands'
109     compile 'org.eclipse.e4.core.contexts'
110     compile 'org.eclipse.e4.core.di'
111     compile 'org.eclipse.e4.core.di.annotations'
112     compile 'org.eclipse.e4.core.services'
113     compile 'org.eclipse.equinox.common'
114     compile 'org.eclipse.equinox.ds'
115     compile 'org.eclipse.equinox.app'
116     compile 'org.eclipse.equinox.registry'
117     compile 'org.eclipse.equinox.preferences'
118     compile 'org.eclipse.jface'
119     compile 'org.eclipse.osgi'
120     compile 'org.eclipse.osgi.services'
121     compile 'org.eclipse.ui'
122     compile 'org.eclipse.ui.workbench'
123     compile 'org.eclipse.team.core'
124     compile 'org.eclipse.text'
125
126     compile 'org.eclipse.e4.ui.bindings'
127     compile 'org.eclipse.e4.ui.model.workbench'
128     compile 'org.eclipse.e4.ui.workbench'
129     compile 'org.eclipse.e4.ui.workbench.swt'
130     compile 'org.eclipse.e4.ui.css.core'
131     compile 'org.eclipse.e4.ui.css.swt'
132     compile 'org.eclipse.e4.ui.services'
133     compile 'org.eclipse.e4.ui.di'
134
135     // org.eclipse.update.configurator deliberately kept at 3.3.400 see https://github.com/diffplug/goomph/issues/69
136     //compile 'org.eclipse.update.configurator'
137
138   }
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152 mainClassName = launcherClass
153 def classes = "$jalviewDir/$classesDir"
154
155 // configure classpath/args for j8/j11 compilation
156
157 def libDir
158 def libDistDir
159 def compile_source_compatibility
160 def compile_target_compatibility
161
162 ext {
163   buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
164
165   gitHash = ""
166   gitBranch = ""
167
168   jalviewjsServer = ""
169 }
170
171 def JAVA_INTEGER_VERSION
172 //def additional_compiler_args = []
173 // this property is for the Java library used in eclipse
174 def eclipse_java_runtime_name
175 if (JAVA_VERSION.equals("1.8")) {
176   JAVA_INTEGER_VERSION = "8"
177   //libDir = j8libDir
178   libDir = j11libDir
179   libDistDir = j8libDir
180   compile_source_compatibility = 1.8
181   compile_target_compatibility = 1.8
182   eclipse_java_runtime_name = "JavaSE-1.8"
183 } else if (JAVA_VERSION.equals("11")) {
184   JAVA_INTEGER_VERSION = "11"
185   libDir = j11libDir
186   libDistDir = j11libDir
187   compile_source_compatibility = 11
188   compile_target_compatibility = 11
189   eclipse_java_runtime_name = "JavaSE-11"
190   /* compile without modules -- using classpath libraries
191   additional_compiler_args += [
192   '--module-path', ext.modules_compileClasspath.asPath,
193   '--add-modules', j11modules
194   ]
195   */
196 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
197   JAVA_INTEGER_VERSION = JAVA_VERSION
198   libDir = j11libDir
199   libDistDir = j11libDir
200   compile_source_compatibility = JAVA_VERSION
201   compile_target_compatibility = JAVA_VERSION
202   eclipse_java_runtime_name = "JavaSE-11"
203   /* compile without modules -- using classpath libraries
204   additional_compiler_args += [
205   '--module-path', ext.modules_compileClasspath.asPath,
206   '--add-modules', j11modules
207   ]
208   */
209 } else {
210   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
211 }
212
213
214 sourceSets {
215
216   main {
217     java {
218       srcDirs "$jalviewDir/$sourceDir"
219       outputDir = file("$classes")
220     }
221
222     resources {
223       srcDirs "$jalviewDir/$resourceDir"
224     }
225
226     compileClasspath = files(sourceSets.main.java.outputDir)
227     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
228
229     runtimeClasspath = compileClasspath
230   }
231
232 }
233
234 eclipse {
235   project {
236     name = eclipse_project_name
237
238     natures 'org.eclipse.jdt.core.javanature',
239     'org.eclipse.buildship.core.gradleprojectnature'
240
241     buildCommand 'org.eclipse.jdt.core.javabuilder'
242     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
243   }
244
245   classpath {
246     //defaultOutputDir = sourceSets.main.java.outputDir
247     def removeThese = []
248     configurations.each{ if (it.isCanBeResolved()) {
249         removeThese += it
250       }
251     }
252
253     minusConfigurations += removeThese
254     plusConfigurations = [ ]
255     file {
256
257       whenMerged { cp ->
258         def removeTheseToo = []
259         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
260         cp.entries.each { entry ->
261           if (entry.kind == 'src') {
262             if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
263               removeTheseToo += entry
264             } else {
265               addedSrcPath.putAt(entry.path, true)
266             }
267           }
268         }
269         cp.entries.removeAll(removeTheseToo)
270
271         if (file(eclipse_bin_dir+"/main").isDirectory()) {
272                 cp.entries += new Output(eclipse_bin_dir+"/main")
273         }
274         if (file(helpParentDir).isDirectory()) {
275                 cp.entries += new Library(fileReference(helpParentDir))
276         }
277         if (file(resourceDir).isDirectory()) {
278                 cp.entries += new Library(fileReference(resourceDir))
279         }
280
281         HashMap<String, Boolean> addedLibPath = new HashMap<>();
282
283         // changing from sourcesets.main.classpath to specific Java version lib
284         //sourceSets.main.compileClasspath.each{
285         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
286           //don't want to add outputDir as eclipse is using its own output dir in bin/main
287           if (it.isDirectory() || ! it.exists()) {
288             // don't add dirs to classpath
289             return
290           }
291           def itPath = it.toString()
292           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
293             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
294           }
295           if (addedLibPath.get(itPath)) {
296             //println("Not adding duplicate entry "+itPath)
297           } else {
298             //println("Adding entry "+itPath)
299             cp.entries += new Library(fileReference(itPath))
300             addedLibPath.put(itPath, true)
301           }
302         }
303       } // whenMerged
304
305     } // file
306
307     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
308   } // classpath
309
310   jdt {
311     // for the IDE, use java 11 compatibility
312     sourceCompatibility = compile_source_compatibility
313     targetCompatibility = compile_target_compatibility
314     javaRuntimeName = eclipse_java_runtime_name
315
316     /*
317     file {
318     withProperties { props ->
319     def jalview_prefs = new Properties()
320     def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
321     jalview_prefs.load(ins)
322     ins.close()
323     jalview_prefs.forEach { t, v ->
324     if (props.getAt(t) == null) {
325     props.putAt(t, v)
326     }
327     }
328     }
329     }
330      */
331   } // jdt
332
333   //synchronizationTasks eclipseClasspath
334   //autoBuildTasks eclipseClasspath
335 } // eclipse
336
337 /*
338 compileJava {
339
340   doFirst {
341     sourceCompatibility = compile_source_compatibility
342     targetCompatibility = compile_target_compatibility
343     //options.compilerArgs = additional_compiler_args
344     print ("Setting target compatibility to "+targetCompatibility+"\n")
345   }
346
347 }
348
349 clean {
350   delete sourceSets.main.java.outputDir
351 }
352 */
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 def useIde = useGoomph.equals("true")
374
375 def eclipseWorkspace
376 task jalviewjsSetEclipseWorkspace {
377   def eclipseWsDir = useIde?goomph_workspace:jalviewjs_eclipse_workspace
378   eclipseWorkspace = file(eclipseWsDir)
379   if (!eclipseWorkspace.exists()) {
380     eclipseWorkspace = file("${buildDir}/tmp/eclipse-workspace")
381     //eclipseWorkspace.deleteOnExit()
382   } else if (useIde) {
383     GoomphCacheLocations.override_workspaces = file(eclipseWorkspace.getAbsolutePath())
384     eclipseWorkspace = oomphIde.workspaceRegistry.workspaceDir(project, file(oomphIde.ideDir))
385   }
386   println("ECLIPSE WORKSPACE: "+eclipseWorkspace.getPath())
387 }
388
389 oomphIde {
390   repoEclipse goomph_eclipse_version
391   jdt {
392     /*
393     installedJre {
394       //markDefault = true
395       //executionEnvironments = [ eclipse.jdt.javaRuntimeName ]
396     }
397     */
398     //compilerComplianceLevel( JAVA_INTEGER_VERSION )
399   }
400
401   repo "https://download.eclipse.org/releases/${goomph_eclipse_version_name}/"
402   feature goomph_eclipse_jee_feature, goomph_eclipse_jee_version
403
404   //iu 'org.eclipse.osgi'
405
406   thirdParty {
407     buildship {
408       repo goomph_repo_buildship
409       feature goomph_feature_buildship
410     }
411
412   }
413
414
415
416   // CLI ARGS HERE!
417 }
418
419 equinoxLaunch {
420   ideJalviewjsBuildSetup {
421
422     source.addConfiguration(configurations.compile)
423
424     source.addMaven('eclipseOrbit:org.apache.ant:1.10.5.v20190526-1402')
425     source.addMaven('eclipseOrbit:javax.servlet:3.1.0.v201410161800')
426
427     source.addMaven('org.eclipse.birt.runtime:com.ibm.icu:56.1.0.v201601250100')
428     source.addMaven('javax.inject:com.springsource.javax.inject:1.0.0')
429
430     //source.addMaven('eclipse-deps:org.apache.ant:1.10.5')
431
432     source.addMaven('org.apache.felix:org.apache.felix.scr:2.1.16')
433
434     source.addMaven('org.osgi:org.osgi.framework:1.9.0')
435     source.addMaven('org.osgi:org.osgi.service.component:1.4.0')
436     source.addMaven('org.osgi:org.osgi.service.event:1.4.0')
437     source.addMaven('org.osgi:org.osgi.util.promise:1.1.1')
438     source.addMaven('org.osgi:org.osgi.util.function:1.1.0')
439
440 //### TRY AND FIND A HEADLESS BUILD WITHOUT org.eclipse.jdt.apt.core
441     source.addMaven('org.eclipse.jdt:org.eclipse.jdt.apt.core:3.6.500')
442     source.addMaven('org.eclipse.jdt:org.eclipse.jdt.core:3.19.0')
443
444     source.addMaven('org.eclipse.platform:org.eclipse.update.configurator:3.3.400')
445
446     //source.addMaven('org.eclipse.e4.ui.internal.workbench.swt')
447     /*
448     source.addMaven('org.eclipse.platform:org.eclipse.ant.core:3.5.500')
449     source.addMaven('org.eclipse.platform:org.eclipse.compare.core:3.6.600')
450     source.addMaven('org.eclipse.platform:org.eclipse.core.commands:3.9.500')
451     source.addMaven('org.eclipse.platform:org.eclipse.core.contenttype:3.7.400')
452     source.addMaven('org.eclipse.platform:org.eclipse.core.jobs:3.10.500')
453     source.addMaven('org.eclipse.platform:org.eclipse.core.expressions:3.6.500')
454     source.addMaven('org.eclipse.platform:org.eclipse.core.filesystem:1.7.500')
455     source.addMaven('org.eclipse.platform:org.eclipse.core.resources:3.13.500')
456     source.addMaven('org.eclipse.platform:org.eclipse.core.runtime:3.16.0')
457     source.addMaven('org.eclipse.platform:org.eclipse.core.variables:3.4.600')
458     //source.addMaven('org.eclipse.platform:org.eclipse.e4.commands:3.10.500')
459     source.addMaven('org.eclipse.platform:org.eclipse.e4.core.commands:0.12.700')
460     source.addMaven('org.eclipse.platform:org.eclipse.e4.core.contexts:1.8.200')
461     source.addMaven('org.eclipse.platform:org.eclipse.e4.core.di:1.7.400')
462     source.addMaven('org.eclipse.platform:org.eclipse.e4.core.di.annotations:1.6.400')
463     source.addMaven('org.eclipse.platform:org.eclipse.e4.core.services:2.2.0')
464     source.addMaven('org.eclipse.platform:org.eclipse.e4.ui.workbench:1.10.100')
465     source.addMaven('org.eclipse.platform:org.eclipse.equinox.common:3.10.500')
466     source.addMaven('org.eclipse.platform:org.eclipse.equinox.ds:1.6.0')
467     source.addMaven('org.eclipse.platform:org.eclipse.equinox.app:1.4.300')
468     source.addMaven('org.eclipse.platform:org.eclipse.equinox.registry:3.8.500')
469     source.addMaven('org.eclipse.platform:org.eclipse.equinox.preferences:3.7.500')
470     source.addMaven('org.eclipse.platform:org.eclipse.jface:3.17.0')
471     source.addMaven('org.eclipse.platform:org.eclipse.osgi:3.14.0')
472     source.addMaven('org.eclipse.platform:org.eclipse.swt:3.112.0')
473     source.addMaven('org.eclipse.platform:org.eclipse.ui:3.114.0')
474     source.addMaven('org.eclipse.platform:org.eclipse.ui.workbench:3.116.0')
475     source.addMaven('org.eclipse.platform:org.eclipse.team.core:3.8.700')
476     source.addMaven('org.eclipse.platform:org.eclipse.text:3.9.0')
477     // org.eclipse.update.configurator deliberately kept at 3.3.400 see https://github.com/diffplug/goomph/issues/69
478     source.addMaven('org.eclipse.platform:org.eclipse.update.configurator:3.3.400')
479
480     source.addMaven('org.eclipse.platform:org.eclipse.e4.ui.bindings:0.12.600')
481     */
482
483
484
485
486     launchTask 'ideJalviewjsBuild', {
487       //it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
488       //it.args = ["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
489       it.args = ["-consoleLog", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ]
490       //it.vmArgs = ['-Dosgi.bundles=org.eclipse.core.jobs@2:start,org.eclipse.equinox.common@3:start,org.eclipse.core.runtime@2:start']
491       //it.vmArgs = ['-Dequinox.use.ds=false']
492
493     }
494   }
495 }
496
497 /*
498 task ideCopyDropins (type: Copy) {
499   dependsOn ideSetupP2
500
501   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir)
502   def outputDir = oomphIde.ideDir + "/" + com.diffplug.gradle.FileMisc.macContentsEclipse() + "/dropins"
503
504   from inputFiles
505   into outputDir
506   def outputFiles = []
507   rename { filename ->
508     outputFiles += outputDir+"/"+filename
509     println("COPYING ${filename} to ${outputFiles}")
510     null
511   }
512   outputs.files outputFiles
513   inputs.files inputFiles
514
515 }
516 */
517
518 ideSetupWorkspace.dependsOn jalviewjsSetEclipseWorkspace
519 ideSetupWorkspace.outputs.upToDateWhen {file(eclipseWorkspace).exists()}
520
521 task ideSetup {
522   dependsOn ideSetupP2
523   dependsOn ideSetupWorkspace
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 def jalviewjsBuildDir
541 def jalviewjsSiteDir
542 task jalviewjsSitePath {
543   if (jalviewjs_site_dir.startsWith("/")) {
544     jalviewjsSiteDir = jalviewjs_site_dir
545   } else {
546     def relativeBuildDir = file(jalviewDirAbsolutePath).toPath().relativize(buildDir.toPath())
547     jalviewjsBuildDir = "${relativeBuildDir}/jalviewjs"
548     jalviewjsSiteDir = jalviewjsBuildDir + "/" + jalviewjs_site_dir
549   }
550 }
551
552
553 task jalviewjsUnzipFiles {
554   dependsOn jalviewjsSitePath
555
556   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
557   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
558
559   doLast {
560     zipFiles.each { file_zip -> 
561       copy {
562         from zipTree(file_zip)
563         into jalviewjsSiteDir
564       }
565     }
566   }
567
568   inputs.files zipFiles
569   outputs.dir jalviewjsSiteDir
570 }
571
572 def eclipseDropinsDir
573 def eclipseBinary
574 task jalviewjsEclipsePaths {
575   def eclipseRoot
576   if (useIde) {
577     eclipseRoot = oomphIde.ideDir
578     eclipseDropinsDir = eclipseRoot+"/"+com.diffplug.gradle.FileMisc.macContentsEclipse()+"/dropins"
579     eclipseBinary = eclipseRoot+"/"+com.diffplug.common.swt.os.OS.getNative().winMacLinux("", "Contents/MacOS", "")+"/eclipse"
580   } else {
581     eclipseRoot = jalviewjs_eclipse_root
582     if (eclipseRoot.startsWith("~")) {
583       eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
584     }
585     if (OperatingSystem.current().isMacOsX()) {
586       eclipseRoot += "/Eclipse.app"
587       eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
588       eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
589     } else if (OperatingSystem.current().isWindows()) { // check these paths!!
590       eclipseDropinsDir = eclipseRoot+"/dropins"
591       eclipseBinary = eclipseRoot+"/eclipse"
592     } else { // linux or unix
593       eclipseDropinsDir = eclipseRoot+"/dropins"
594       eclipseBinary = eclipseRoot+"/eclipse"
595     }
596   }
597   //println("ECLIPSE_ROOT=${eclipseRoot}")
598   //println("ECLIPSE_BINARY=${eclipseBinary}")
599 }
600
601 //task jalviewjsEclipseCopyDropins (type: Copy) {
602 task jalviewjsEclipseCopyDropins {
603   dependsOn jalviewjsEclipsePaths
604   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir, include: "*.jar")
605   def outputDir = eclipseDropinsDir
606
607   inputs.files inputFiles
608   inputFiles.each { file ->
609     outputs.file(outputDir+"/"+file.name)
610   }
611
612   doLast {
613     def outputFiles = []
614     inputFiles.each { file ->
615       copy {
616         from file
617         into outputDir
618       }
619     }
620   }
621 }
622
623 task jalviewjsEclipseSetup {
624   dependsOn jalviewjsEclipseCopyDropins
625   dependsOn jalviewjsSetEclipseWorkspace
626 }
627
628
629 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
630   dependsOn jalviewjsSitePath
631   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
632   def props = project.properties.sort { it.key }
633   def siteDirProperty = "j2s.site.directory"
634   def setSiteDir = false
635   props.each { prop, val ->
636     if (prop.startsWith("j2s.") && val != null) {
637       if (prop == siteDirProperty) {
638         if (!(val.startsWith("/") || val.startsWith("file://") )) {
639           val = jalviewjsSiteDir+"/"+val
640         }
641         setSiteDir = true
642       }
643       property(prop,val)
644     }
645     if (!setSiteDir) {
646       property(siteDirProperty,"${jalviewjsSiteDir}")
647     }
648   }
649   outputs.file(outputFile)
650 }
651
652 task jalviewjsCopyResources (type: Copy) {
653   dependsOn jalviewjsSitePath
654   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
655   def outputDir = jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir
656
657   from inputFiles
658   into outputDir
659   def outputFiles = []
660   rename { filename ->
661     outputFiles += outputDir+"/"+filename
662     null
663   }
664   outputs.files outputFiles
665   inputs.files inputFiles
666 }
667
668 task jalviewjsCopySiteResources (type: Copy) {
669   dependsOn jalviewjsSitePath
670   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
671   def outputDir = jalviewjsSiteDir
672
673   from inputFiles
674   into outputDir
675   def outputFiles = []
676   rename { filename ->
677     outputFiles += outputDir+"/"+filename
678     null
679   }
680   outputs.files outputFiles
681   inputs.files inputFiles
682 }
683
684 task cleanJalviewjs {
685   dependsOn jalviewjsSitePath
686   /*
687   delete jalviewDir+"/"+jalviewjsSiteDir
688   delete jalviewDir+"/"+eclipse_bin_dir
689   delete file(eclipseWorkspace.getAbsolutePath()+"/.metadata")
690   delete jalviewDir+"/"+jalviewjs_j2s_settings
691   */
692 }
693
694 task jalviewjsProjectImport(type: Exec) {
695   // work out how to do this!
696   dependsOn eclipseProject
697   dependsOn eclipseClasspath
698   dependsOn eclipseJdt
699   dependsOn jalviewjsEclipsePaths
700   dependsOn jalviewjsEclipseSetup
701   executable(eclipseBinary)
702   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", eclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
703
704   def projdir = eclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core"
705   inputs.file(jalviewDir+"/.project")
706   outputs.dir(projdir)
707   outputs.upToDateWhen { file(projdir).exists() }
708 }
709
710
711
712
713
714
715
716
717 task jalviewjsTranspile(type: Exec) {
718   dependsOn jalviewjsCreateJ2sSettings 
719   dependsOn jalviewjsProjectImport
720   dependsOn jalviewjsEclipsePaths
721   executable(eclipseBinary)
722   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", eclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
723
724   def stdout
725   def stderr
726   doFirst {
727     stdout = new ByteArrayOutputStream()
728     stderr = new ByteArrayOutputStream()
729     standardOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}"), stdout);
730     errorOutput = new org.apache.tools.ant.util.TeeOutputStream(new FileOutputStream("${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}"), stderr);
731   }
732   doLast {
733     if (stdout.toString().contains("Error processing ")) {
734       // j2s did not complete transpile
735       throw new GradleException("Error during transpilation:\n${stderr}\nSee eclipse transpile log files '${jalviewjsBuildDir}/${jalviewjs_j2s_stdout}' and '${jalviewjsBuildDir}/${jalviewjs_j2s_stderr}'\n")
736     }
737   }
738
739   inputs.dir(sourceDir)
740   outputs.dir(eclipse_bin_dir+"/main")
741   outputs.files(
742     fileTree(
743       dir: jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir,
744       includes: [ "**/*.js" ],
745       excludes: [
746         //jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir+"/intervalstore/**",
747         //jalviewjsSiteDir+"/"+jalviewjs_j2s_subdir+"/org/json/**",
748         "intervalstore/**",
749         "org/json/**",
750       ]
751     ).files
752   )
753   outputs.file(jalviewjsSiteDir+jalviewjs_server_resource)
754
755 }
756
757
758
759
760
761
762 jalviewjsUnzipFiles.mustRunAfter jalviewjsTranspile
763 jalviewjsTranspile.mustRunAfter jalviewjsCopyResources
764 jalviewjsTranspile.mustRunAfter jalviewjsCopySiteResources
765
766 task jalviewjsBuildSite {
767   dependsOn jalviewjsSitePath
768   dependsOn jalviewjsUnzipFiles
769   dependsOn jalviewjsCopyResources
770   dependsOn jalviewjsCopySiteResources
771   dependsOn jalviewjsTranspile
772 }
773
774 task jalviewjsSiteTar(type: Tar) {
775   dependsOn jalviewjsBuildSite
776   def outputFilename = "jalviewjs-site-${JALVIEW_VERSION}.tar.gz"
777   try {
778     archiveFileName = outputFilename
779   } catch (Exception e) {
780     archiveName = outputFilename
781   }
782
783   compression Compression.GZIP
784
785   from jalviewjsSiteDir
786   into jalviewjs_site_dir // this is inside the tar file
787
788   inputs.dir(jalviewjsSiteDir)
789 }
790
791 def jalviewjsServer = null
792 task jalviewjsServerStart {
793   dependsOn jalviewjsSitePath
794   doLast {
795
796     if (jalviewjsServer != null) {
797       println("SERVER ALREADY RUNNING. Go to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle jalviewjsServerStop  to stop.")
798     } else {
799
800       SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
801       def port = Integer.valueOf(jalviewjs_server_port)
802       def start = port
803       def running = false
804       while(port < start+1000 && !running) {
805         try {
806           def doc_root = new File(jalviewDirAbsolutePath +"/"+ jalviewjsSiteDir)
807           jalviewjsServer = factory.start(doc_root, port)
808           running = true
809           println("SERVER STARTED with document root ${doc_root}.\nGo to "+jalviewjsServer.getResourceUrl(jalviewjs_server_resource)+" . Run  gradle --stop  to stop (kills all gradle daemons).")
810           //println("Ctrl-c to stop.");java.lang.Thread.sleep(Integer.valueOf(jalviewjs_server_wait)*1000);
811         } catch (Exception e) {
812           port++;
813         }
814       }
815
816     }
817
818   }
819
820 }
821
822 /* server persists in gradle daemon but reference is not accessible across builds
823 task jalviewjsServerStop {
824   doLast {
825
826     if (jalviewjsServer != null) {
827
828       println("SERVER ON PORT "+jalviewjsServer.getPort()+" STOPPING. Run  gradle jalviewjsServerStart  to start again.")
829       jalviewjsServer.stop()
830
831     } else {
832       println("SERVER NOT RUNNING. Run  gradle jalviewjsServerStart  to start.")
833     }
834
835   }
836 }
837 */
838
839 task ideBuildSite {
840   dependsOn jalviewjsSitePath
841   dependsOn jalviewjsUnzipFiles
842   dependsOn jalviewjsCopyResources
843   dependsOn jalviewjsCopySiteResources
844   dependsOn ideSetup
845   dependsOn ide
846 }
847
848
849
850
851 task jalviewjs {
852   dependsOn jalviewjsBuildSite
853 }
854
855
856 project.afterEvaluate {
857   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseProject
858   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseClasspath
859   tasks.findByName('ideJalviewjsBuild').dependsOn eclipseJdt
860   //tasks.findByName('ideJalviewjsBuild').dependsOn ideSetup
861
862   tasks.findByName('ide').dependsOn eclipseProject
863   tasks.findByName('ide').dependsOn eclipseClasspath
864   tasks.findByName('ide').dependsOn eclipseJdt
865 }
866
867
868