62b4df525d4e592c7acda9f6a876862926730af7
[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("bin/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 compileJava {
199
200   doFirst {
201     sourceCompatibility = compile_source_compatibility
202     targetCompatibility = compile_target_compatibility
203     //options.compilerArgs = additional_compiler_args
204     print ("Setting target compatibility to "+targetCompatibility+"\n")
205   }
206
207 }
208
209 clean {
210   delete sourceSets.main.java.outputDir
211 }
212
213
214 task jalviewjs_unzipFiles {
215   def zipFiles = fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_libjs_dir).include("*.zip")
216   zipFiles += jalviewjs_utils_dir+"/"+jalviewjs_swingjs_zip
217
218   doLast {
219     zipFiles.each { file_zip -> 
220       copy {
221         from zipTree(file_zip)
222         into jalviewjs_site_dir
223       }
224     }
225   }
226
227   inputs.files zipFiles
228   outputs.dir jalviewjs_site_dir
229 }
230
231 def eclipseBinary
232 def eclipseDropinsDir
233 def eclipsePluginsDir
234 task jalviewjs_eclipsePaths {
235         if (OperatingSystem.current().isMacOsX()) {
236                 eclipseDropinsDir = jalviewjs_eclipse_root+"/Contents/Eclipse/dropins"
237                 eclipsePluginsDir = jalviewjs_eclipse_root+"/Contents/Eclipse/plugins"
238                 eclipseBinary = jalviewjs_eclipse_root+"/Contents/MacOS/eclipse"
239         } else if (OperatingSystem.current().isWindows()) { // check this!
240                 eclipseDropinsDir = jalviewjs_eclipse_root+"/dropins"
241                 eclipsePluginsDir = jalviewjs_eclipse_root+"/plugins"
242                 eclipseBinary = jalviewjs_eclipse_root+"/eclipse"
243         } else { // linux or unix
244                 eclipseDropinsDir = jalviewjs_eclipse_root+"/dropins"
245                 eclipsePluginsDir = jalviewjs_eclipse_root+"/plugins"
246                 eclipseBinary = jalviewjs_eclipse_root+"/eclipse"
247         }
248 }
249
250 task jalviewjs_eclipse_copy_dropins (type: Copy) {
251   dependsOn jalviewjs_eclipsePaths
252   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_dropins_dir)
253   def outputDir = eclipseDropinsDir
254   from inputFiles
255   into outputDir
256   inputs.files inputFiles
257   //outputs.dir outputDir
258 }
259
260 task jalviewjs_eclipse_copy_plugins (type: Copy) {
261   dependsOn jalviewjs_eclipsePaths
262   def inputFiles = fileTree(jalviewjs_utils_dir+"/"+jalviewjs_eclipse_plugins_dir)
263   def outputDir = eclipsePluginsDir
264   from inputFiles
265   into outputDir
266   //inputs.files inputFiles
267   //outputs.dir outputDir
268 }
269
270 def tempEclipseWorkspace = ""
271
272 task jalviewjs_setTempEclipseWorkspace {
273   tempEclipseWorkspace = file(jalviewjs_eclipse_workspace)
274   if (!tempEclipseWorkspace.exists()) {
275     tempEclipseWorkspace = File.createTempDir()
276     //tempEclipseWorkspace.deleteOnExit()
277   }
278 }
279
280 task jalviewjs_eclipse_setup {
281   dependsOn jalviewjs_eclipse_copy_dropins
282   dependsOn jalviewjs_eclipse_copy_plugins
283   dependsOn jalviewjs_setTempEclipseWorkspace
284 }
285
286 task jalviewjs_createJ2sSettings(type: WriteProperties) {
287   outputFile (jalviewDir+"/"+jalviewjs_j2s_settings)
288   def props = project.properties.sort { it.key }
289   props.each { prop, val ->
290         if (prop.startsWith("j2s.") && val != null) {
291                 property(prop,val)
292         }
293   }
294   outputs.file(outputFile)
295 }
296
297 task jalviewjs_copyResources (type: Copy) {
298   from fileTree(dir: jalviewjs_resource_dir)
299   into jalviewjs_site_dir+"/"+jalviewjs_j2s_subdir
300 }
301
302 task jalviewjs_copySiteResources (type: Copy) {
303   from fileTree(dir: jalviewjs_utils_dir+"/"+jalviewjs_site_resource_dir)
304   into jalviewjs_site_dir
305 }
306
307 task cleanJalviewjsSite {
308   //delete jalviewjs_site_dir
309 }
310
311 task jalviewjsProjectImport(type: Exec) {
312   // work out how to do this!
313   //dependsOn eclipse
314   dependsOn jalviewjs_eclipsePaths
315   dependsOn jalviewjs_eclipse_setup
316   executable(eclipseBinary)
317   args(["-nosplash", "--launcher.suppressErrors", "-application", "com.seeq.eclipse.importprojects.headlessimport", "-data", tempEclipseWorkspace.getPath(), "-import", jalviewDirAbsolutePath])
318
319   def tempdir = tempEclipseWorkspace.getPath()+"/.metadata/.plugins/org.eclipse.core.resources/.projects/jalview"
320   outputs.dir(tempdir)
321 }
322
323 task jalviewjsTranspile(type: Exec) {
324   dependsOn jalviewjsProjectImport
325   dependsOn jalviewjs_eclipsePaths
326   executable(eclipseBinary)
327   args(["-nosplash", "--launcher.suppressErrors", "-application", "org.eclipse.jdt.apt.core.aptBuild", "-data", tempEclipseWorkspace, "-"+jalviewjs_eclipseBuildArg, eclipse_project_name ])
328 }
329
330 task jalviewjsBuildSite {
331   dependsOn jalviewjs_unzipFiles
332   dependsOn jalviewjs_copyResources
333   dependsOn jalviewjs_copySiteResources
334   dependsOn jalviewjsTranspile
335 }
336
337 task jalviewjs {
338   dependsOn jalviewjs_createJ2sSettings 
339   dependsOn jalviewjsBuildSite
340 }