JAL-3210 gradle jalviewjsServer now working. Up to date src and swingjs from Jalview...
[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
7 plugins {
8   id 'java'
9   id 'application'
10   id 'eclipse'
11 }
12
13 repositories {
14   jcenter()
15   mavenCentral()
16   mavenLocal()
17   flatDir {
18     dirs gradlePluginsDir
19   }
20 }
21
22 mainClassName = launcherClass
23 def classes = "$jalviewDir/$classesDir"
24
25 // configure classpath/args for j8/j11 compilation
26
27 def jalviewDirAbsolutePath = file(jalviewDir).getAbsolutePath()
28 def libDir
29 def libDistDir
30 def compile_source_compatibility
31 def compile_target_compatibility
32
33 ext {
34   buildProperties = jalviewDir + "/" + classesDir +"/" + buildPropertiesFile
35
36   gitHash = ""
37   gitBranch = ""
38 }
39
40 def JAVA_INTEGER_VERSION
41 //def additional_compiler_args = []
42 // this property is for the Java library used in eclipse
43 def eclipse_java_runtime_name
44 if (JAVA_VERSION.equals("1.8")) {
45   JAVA_INTEGER_VERSION = "8"
46   //libDir = j8libDir
47   libDir = j11libDir
48   libDistDir = j8libDir
49   compile_source_compatibility = 1.8
50   compile_target_compatibility = 1.8
51   eclipse_java_runtime_name = "JavaSE-1.8"
52 } else if (JAVA_VERSION.equals("11")) {
53   JAVA_INTEGER_VERSION = "11"
54   libDir = j11libDir
55   libDistDir = j11libDir
56   compile_source_compatibility = 11
57   compile_target_compatibility = 11
58   eclipse_java_runtime_name = "JavaSE-11"
59   /* compile without modules -- using classpath libraries
60   additional_compiler_args += [
61   '--module-path', ext.modules_compileClasspath.asPath,
62   '--add-modules', j11modules
63   ]
64   */
65 } else if (JAVA_VERSION.equals("12") || JAVA_VERSION.equals("13")) {
66   JAVA_INTEGER_VERSION = JAVA_VERSION
67   libDir = j11libDir
68   libDistDir = j11libDir
69   compile_source_compatibility = JAVA_VERSION
70   compile_target_compatibility = JAVA_VERSION
71   eclipse_java_runtime_name = "JavaSE-11"
72   /* compile without modules -- using classpath libraries
73   additional_compiler_args += [
74   '--module-path', ext.modules_compileClasspath.asPath,
75   '--add-modules', j11modules
76   ]
77   */
78 } else {
79   throw new GradleException("JAVA_VERSION=$JAVA_VERSION not currently supported by Jalview")
80 }
81
82 sourceSets {
83
84   main {
85     java {
86       srcDirs "$jalviewDir/$sourceDir"
87       outputDir = file("$classes")
88     }
89
90     resources {
91       srcDirs "$jalviewDir/$resourceDir"
92     }
93
94     compileClasspath = files(sourceSets.main.java.outputDir)
95     compileClasspath += fileTree(dir: "$jalviewDir/$libDir", include: ["*.jar"])
96
97     runtimeClasspath = compileClasspath
98   }
99
100 }
101
102 eclipse {
103   project {
104     name = eclipse_project_name
105
106     natures 'org.eclipse.jdt.core.javanature',
107     'org.eclipse.buildship.core.gradleprojectnature'
108
109     buildCommand 'org.eclipse.jdt.core.javabuilder'
110     buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
111   }
112
113   classpath {
114     //defaultOutputDir = sourceSets.main.java.outputDir
115     def removeThese = []
116     configurations.each{ if (it.isCanBeResolved()) {
117         removeThese += it
118       }
119     }
120
121     minusConfigurations += removeThese
122     plusConfigurations = [ ]
123     file {
124
125       whenMerged { cp ->
126         def removeTheseToo = []
127         HashMap<String, Boolean> addedSrcPath = new HashMap<>();
128         cp.entries.each { entry ->
129           if (entry.kind == 'src') {
130             if (addedSrcPath.getAt(entry.path) || !(entry.path == "src" || entry.path == "test")) {
131               removeTheseToo += entry
132             } else {
133               addedSrcPath.putAt(entry.path, true)
134             }
135           }
136         }
137         cp.entries.removeAll(removeTheseToo)
138
139         cp.entries += new Output(eclipse_bin_dir+"/main")
140         cp.entries += new Library(fileReference(helpParentDir))
141         cp.entries += new Library(fileReference(resourceDir))
142
143         HashMap<String, Boolean> addedLibPath = new HashMap<>();
144
145         // changing from sourcesets.main.classpath to specific Java version lib
146         //sourceSets.main.compileClasspath.each{
147         fileTree("$jalviewDir/$libDistDir").include("**/*.jar").include("*.jar").each {
148           //don't want to add outputDir as eclipse is using its own output dir in bin/main
149           if (it.isDirectory() || ! it.exists()) {
150             // don't add dirs to classpath
151             return
152           }
153           def itPath = it.toString()
154           if (itPath.startsWith(jalviewDirAbsolutePath+"/")) {
155             itPath = itPath.substring(jalviewDirAbsolutePath.length()+1)
156           }
157           if (addedLibPath.get(itPath)) {
158             //println("Not adding duplicate entry "+itPath)
159           } else {
160             //println("Adding entry "+itPath)
161             cp.entries += new Library(fileReference(itPath))
162             addedLibPath.put(itPath, true)
163           }
164         }
165       } // whenMerged
166
167     } // file
168
169     containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
170   } // classpath
171
172   jdt {
173     // for the IDE, use java 11 compatibility
174     sourceCompatibility = compile_source_compatibility
175     targetCompatibility = compile_target_compatibility
176     javaRuntimeName = eclipse_java_runtime_name
177
178     /*
179     file {
180     withProperties { props ->
181     def jalview_prefs = new Properties()
182     def ins = new FileInputStream(jalviewDirAbsolutePath+"/"+eclipse_extra_jdt_prefs_file)
183     jalview_prefs.load(ins)
184     ins.close()
185     jalview_prefs.forEach { t, v ->
186     if (props.getAt(t) == null) {
187     props.putAt(t, v)
188     }
189     }
190     }
191     }
192      */
193   } // jdt
194
195   //synchronizationTasks eclipseClasspath
196   //autoBuildTasks eclipseClasspath
197 } // eclipse
198
199 /*
200 compileJava {
201
202   doFirst {
203     sourceCompatibility = compile_source_compatibility
204     targetCompatibility = compile_target_compatibility
205     //options.compilerArgs = additional_compiler_args
206     print ("Setting target compatibility to "+targetCompatibility+"\n")
207   }
208
209 }
210
211 clean {
212   delete sourceSets.main.java.outputDir
213 }
214 */
215
216 task jalviewjsUnzipFiles {
217   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
218   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
219
220   doLast {
221     zipFiles.each { file_zip -> 
222       copy {
223         from zipTree(file_zip)
224         into jalviewjs_site_dir
225       }
226     }
227   }
228
229   inputs.files zipFiles
230   outputs.dir jalviewjs_site_dir
231 }
232
233 def eclipseBinary
234 def eclipseDropinsDir
235 def eclipsePluginsDir
236 task jalviewjsEclipsePaths {
237   def eclipseRoot = jalviewjs_eclipse_root
238   if (eclipseRoot.startsWith("~")) {
239     eclipseRoot = System.getProperty("user.home") + eclipseRoot.substring(1)
240   }
241         if (OperatingSystem.current().isMacOsX()) {
242     eclipseRoot += "/Eclipse.app"
243                 eclipseDropinsDir = eclipseRoot+"/Contents/Eclipse/dropins"
244                 eclipsePluginsDir = eclipseRoot+"/Contents/Eclipse/plugins"
245                 eclipseBinary = eclipseRoot+"/Contents/MacOS/eclipse"
246         } else if (OperatingSystem.current().isWindows()) { // check these paths!!
247                 eclipseDropinsDir = eclipseRoot+"/dropins"
248                 eclipsePluginsDir = eclipseRoot+"/plugins"
249                 eclipseBinary = eclipseRoot+"/eclipse"
250         } else { // linux or unix
251                 eclipseDropinsDir = eclipseRoot+"/dropins"
252                 eclipsePluginsDir = eclipseRoot+"/plugins"
253                 eclipseBinary = eclipseRoot+"/eclipse"
254         }
255   println("ECLIPSE ROOT: "+eclipseRoot)
256 }
257
258 task jalviewjsEclipseCopyDropins (type: Copy) {
259   dependsOn jalviewjsEclipsePaths
260   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir)
261   def outputDir = eclipseDropinsDir
262
263   from inputFiles
264   into outputDir
265   def outputFiles = []
266   rename { filename ->
267     outputFiles += outputDir+"/"+filename
268     null
269   }
270   outputs.files outputFiles
271   inputs.files inputFiles
272 }
273
274 task jalviewjsEclipseCopyPlugins (type: Copy) {
275   dependsOn jalviewjsEclipsePaths
276   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_plugins_dir)
277   def outputDir = eclipsePluginsDir
278
279   from inputFiles
280   into outputDir
281   def outputFiles = []
282   rename { filename ->
283     outputFiles += outputDir+"/"+filename
284     null
285   }
286   outputs.files outputFiles
287   inputs.files inputFiles
288 }
289
290 def tempEclipseWorkspace = ""
291
292 task jalviewjsSetTempEclipseWorkspace {
293   tempEclipseWorkspace = file(jalviewjs_eclipse_workspace)
294   if (!tempEclipseWorkspace.exists()) {
295     tempEclipseWorkspace = File.createTempDir()
296     tempEclipseWorkspace.deleteOnExit()
297   }
298   println("ECLIPSE WORKSPACE: "+tempEclipseWorkspace.getPath())
299 }
300
301 task jalviewjsEclipseSetup {
302   dependsOn jalviewjsEclipseCopyDropins
303   dependsOn jalviewjsEclipseCopyPlugins
304   dependsOn jalviewjsSetTempEclipseWorkspace
305 }
306
307
308 task jalviewjsCreateJ2sSettings(type: WriteProperties) {
309   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
310   def props = project.properties.sort { it.key }
311   props.each { prop, val ->
312     if (prop.startsWith("j2s.") && val != null) {
313       property(prop,val)
314     }
315   }
316   outputs.file(outputFile)
317 }
318
319 task jalviewjsCopyResources (type: Copy) {
320   def inputFiles = fileTree(dir: jalviewjs_resource_dir)
321   def outputDir = jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir
322
323   from inputFiles
324   into outputDir
325   def outputFiles = []
326   rename { filename ->
327     outputFiles += outputDir+"/"+filename
328     null
329   }
330   outputs.files outputFiles
331   inputs.files inputFiles
332 }
333
334 task jalviewjsCopySiteResources (type: Copy) {
335   def inputFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
336   def outputDir = jalviewjs_site_dir
337
338   from inputFiles
339   into outputDir
340   def outputFiles = []
341   rename { filename ->
342     outputFiles += outputDir+"/"+filename
343     null
344   }
345   outputs.files outputFiles
346   inputs.files inputFiles
347 }
348
349 task cleanJalviewjs {
350   /*
351   delete jalviewDir+"/"+jalviewjs_site_dir
352   delete jalviewDir+"/"+eclipse_bin_dir
353   delete file(tempEclipseWorkspace.getAbsolutePath()+"/.metadata")
354   delete jalviewDir+"/"+jalviewjs_j2s_settings
355   */
356 }
357
358 task jalviewjsProjectImport(type: Exec) {
359   // work out how to do this!
360   dependsOn eclipseProject
361   dependsOn eclipseClasspath
362   dependsOn eclipseJdt
363   dependsOn jalviewjsEclipsePaths
364   dependsOn jalviewjsEclipseSetup
365   executable(eclipseBinary)
366   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", tempEclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
367
368   def projdir = tempEclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview/org.eclipse.jdt.core/"
369   inputs.file(jalviewDir+"/.project")
370   outputs.dir(projdir)
371 }
372
373 task jalviewjsTranspile(type: Exec) {
374   dependsOn jalviewjsCreateJ2sSettings 
375   dependsOn jalviewjsProjectImport
376   dependsOn jalviewjsEclipsePaths
377   executable(eclipseBinary)
378   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
379   inputs.dir(sourceDir)
380   outputs.dir(eclipse_bin_dir+"/main")
381   outputs.file(eclipse_bin_dir+"/main/jalview/bin/Jalview.class")
382   outputs.dir(jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir)
383 }
384
385 task jalviewjsBuildSite {
386   dependsOn jalviewjsUnzipFiles
387   dependsOn jalviewjsCopyResources
388   dependsOn jalviewjsCopySiteResources
389   dependsOn jalviewjsTranspile
390 }
391
392 task jalviewjsSiteTar(type: Tar) {
393   dependsOn jalviewjsBuildSite
394   def outputFilename = "site.tar.gz"
395   try {
396     archiveFileName = outputFilename
397   } catch (Exception e) {
398     archiveName = outputFilename
399   }
400
401   compression Compression.GZIP
402
403   from jalviewjs_site_dir
404   into jalviewjs_site_dir // this is inside the tar file
405
406   inputs.dir(jalviewjs_site_dir)
407 }
408
409 task jalviewjs {
410   dependsOn jalviewjsBuildSite
411 }
412
413 task jalviewjsServer {
414   //dependsOn jalviewjsBuildSite
415
416   doLast {
417     SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
418     def port = Integer.valueOf(jalviewjs_server_port)
419     def start = port
420     def running = false
421     HttpFileServer server
422 println "server wait = ${jalviewjs_server_wait}"
423     while(port < start+100 && !running) {
424       try {
425         server = factory.start(new File(jalviewDirAbsolutePath+"/"+jalviewjs_site_dir), port)
426         running = true
427         println("SERVER STARTED on "+server.getResourceUrl(jalviewjs_server_resource)+" for ${jalviewjs_server_wait} seconds. Ctrc+C to kill it.")
428         java.lang.Thread.sleep(Integer.valueOf(jalviewjs_server_wait)*1000);
429       } catch (Exception e) {
430         port++;
431       }
432     }
433   }
434 }