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