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