75c38e1d8b418559aa624873c58a6a0da97c9d91
[jalview.git] / doc / building.md
1 # Building Jalview from Source
2
3 ## tl;dr
4
5 ```
6 # download
7 git clone http://source.jalview.org/git/jalview.git
8 # compile
9 cd jalview
10 gradle shadowJar
11 # run
12 java -jar build/libs/jalview-all-11.jar
13
14 # and/or create launcher
15 gradle getdown
16 # use launcher
17 cd getdown/files
18 java -jar getdown-launcher.jar . jalview
19 ```
20
21
22 ## Setting up
23
24 The method here is described in terms of using a command line.  You can easily do this on linux or in a Terminal window in macOS.  You can do it in Windows.
25
26 * Java 11 compliant JDK
27 * gradle 5.1 or above
28 * git
29
30 > The versions and installation methods here are just suggestions (which we have tested 
31 so are known to work).  If you need or wish to use different implementations (particularly 
32 you might need a bespoke JDK if you are on an exotic architecture) then the general 
33 build instructions should work with any gradle 5+.  You should be able to compile the 
34 bytecode with any JDK Java 11+.  The resulting bytecode (in particular the shadow jar) 
35 should be runnable in any JRE Java 1.8+.  Remember that because Jalview and the getdown launcher 
36 are Java bytecode you can build on one system where you might have gradle, and run 
37 on another where you don't (JRE 1.8+ required).
38
39 ### Java 11 compliant JDK
40
41 #### All platforms
42 We recommend obtaining an OpenJDK JDK 11 (since 11 is the long term support release) from AdoptOpenJDK: <https://adoptopenjdk.net/?variant=openjdk11&jvmVariant=hotspot>, either the *Installer* or `.zip`/`.tar.gz` variants whichever you prefer (if you're not sure, choose the *Installer*).
43
44 >##### Alternative/CLI install of AdoptOpenJDK 11
45 >
46 >You can also install adoptopenjdk11 using either `brew` (macOS), `choco` (Windows)
47 (see the section on `gradle` and `git` for more informaiton on `brew` and `choco`) 
48 or `yum` or `apt` (Linux):
49 >
50 >###### alternative for MacOS and Homebrew
51 >```
52 >brew tap adoptopenjdk/openjdk
53 >brew cask install adoptopenjdk11
54 >```
55 >
56 >###### alternative for Windows and Chocolatey
57 >```
58 >choco install adoptopenjdk11
59 >```
60 >
61 >###### alternative for Linux with yum/apt
62 >
63 >see <https://adoptopenjdk.net/installation.html#linux-pkg>
64
65
66 ### gradle and git
67 You should be able to install the latest (or sufficiently recent) versions of gradle and git using your OS package manager.
68
69 #### MacOS
70 we recommend using `brew`, which can be installed following the instructions at <https://brew.sh/>.
71 After installing `brew`, open a Terminal window and type in (using an Administrator privileged user):
72
73 ```bash
74 brew install gradle git
75 ```
76
77 or if you aready have them installed but need to upgrade the version:
78
79 ```bash
80 brew upgrade gradle git
81 ```
82
83 #### Windows
84
85 we suggest using the **Chocolatey** package manager.  See install instructions at <https://chocolatey.org/>, and you will just need
86
87 ```bash
88 choco install gradle
89 choco install git
90 ```
91
92 Alternatively, you could install a real `bash` shell and install both `gradle` and `git` through `apt-get`.
93 See <https://devblogs.microsoft.com/commandline/bash-on-ubuntu-on-windows-download-now-3/> 
94 for how to install the ubuntu bash shell in Windows 10.
95
96 Another alternative would be to install them separately. For `gradle` follow the instructions at <https://gradle.org/install/>, and for `git` here are a couple of suggestions: Git for Windows <https://gitforwindows.org/>.
97 Getting the individual installs working together on the command line will be trickier 
98 so we recommend using Chocolatey or bash.
99
100
101 #### Linux
102 this will depend on which distribution you're using.
103
104 ##### For *Debian* based distributions (e.g. Mint, Ubuntu, Debian)
105 run
106
107 ```bash
108  sudo apt-get install gradle git
109 ```
110
111 ##### for RPM-based distributions (e.g. Fedora, CentOS, RedHat)
112 run
113
114 ```bash
115 sudo yum install gradle git
116 ```
117
118 If you have some other version of linux you'll probably be able to work it out!
119
120
121
122
123 ## Downloading the Jalview source tree
124
125 This can be done with `git`.
126 On the command line, change directory to where you want to download Jalview's build-tree 
127 top level directory.  Then run
128
129 ```bash
130 git clone http://source.jalview.org/git/jalview.git
131 ```
132
133 You'll get some progress output and after a minute or two you should have the full 
134 Jalview build-tree in the folder `jalview`.
135
136 ### What's in the source tree?
137
138 Jalview is a mature product with its codebase going back many years.  As such it doesn't 
139 have a folder structure that most new gradle projects would have, so you might not 
140 find everything in the place you might expect.  Here's a brief description of what 
141 you might find in the main folders under the `jalview` tree.
142
143 Within the `jalview` folder you will find (of possible interest):
144
145   dir/ or file       | contains                                                   
146 ---------------------|----------------------------------------------------------------------------------------------------------------
147  `bin/`              | used by eclipse for compiled classes -- no need to touch this
148  `build/`            | the gradle build dir
149  `classes/`          | contains the compiled Java classes for the Jalview application
150  `dist/`             | assembled `.jar` files needed to run Jalview application
151  `examples/`         | example input files usable by Jalview
152  `getdown/`          | the libraries used by the Javliew launcher (getdown)
153  `getdown/src/`      | our modified source for `getdown`
154  `getdown/website/`  | the assembled "download" folder used by getdown for downloads/upgrades
155  `getdown/files/`    | the minimal fileset to launch the Jalview launcher, which can then download the rest of the Jalview application
156  `help/`             | the help documents
157  `j8lib/`            | libraries needed to run Jalview under Java 1.8
158  `j11lib/`           | libraries needed to run Jalivew under Java 11
159  `resource/`         | non-java resources used in the Jalview application
160  `src/`              | the Jalview application source `.java` files
161  `test/`             | Test class source files
162  `utils/`            | helper applications used in the build process
163  `utils/install4j/`  | files used by the packaging tool, install4j
164  `build.gradle`      | the build file used by gradle
165  `gradle.properties` | configurable properties for the build process
166
167 Note that you need a Java 11 JDK to compile Jalview whether your target build is Java 1.8 or Java 11.
168
169
170 ## Building Jalview
171
172 You will need to have the Java 11 `javac` in your path, or alternatively you can configure 
173 gradle to know where this is by putting
174
175 ```
176 org.gradle.java.home=/path_to_jdk_directory
177 ```
178 in the `gradle.properties` file.
179
180 > *You may want to see some of the other properties you can change at the end of this document.*
181
182 ### Minimal Jalview Build
183
184 To compile the necessary class files, just run
185
186 ```bash
187 gradle compileJava
188 ```
189 to compile the classes into the `classes` folder.
190 You should now be able to run the Jalview application directly with
191
192 ```bash
193 java -cp "classes:resources:help:j11lib/*" jalview.bin.Jalview
194 ```
195
196 You can also run with an automatic large memory setting (which will set the maximum 
197 memory heap of the Jalview JVM to 90% of your local physical memory) and docked icon setting 
198 (if possible in your OS) with
199
200 ```bash
201 java -cp "classes:resources:help:j11lib/*" jalview.bin.Launcher
202 ```
203
204 >*You must use just "`j11lib/*`" and not "`j11lib/*.jar`" as this is a special Java 
205 classpath argument wildcard interpreted by `java`, **not** a shell expansion wildcard interpreted 
206 by the shell.*
207
208 Note that `jalview.bin.Launcher` is a simplified launcher class that re-launches `jalview.bin.Jalview` 
209 with the same JRE (*not* the same JVM instance), classpath and arguments, but with an automatically determined `-Xmx...` 
210 memory setting if one hasn't been provided.
211
212 ### Jalview in a Jar File
213
214 To package the `classes`, `resources`, and `help` into one jar, you can run
215
216 ```bash
217 gradle jar
218 ```
219 which assembles the Jalview classes and resources into `dist/jalview.jar`
220
221 To run this, use
222
223 ```bash
224 java -cp "dist/jalview.jar:j11lib/*" jalview.bin.Jalview
225 ```
226
227 ### Distributed Jar Files
228
229 To simplify this, all required `.jar` files can be assembled into the `dist` folder 
230 using
231
232 ```bash
233 gradle makeDist
234 ```
235 which puts all required jar files into `dist` so you can run with
236
237 ```bash
238 java -cp "dist/*" jalview.bin.Jalview
239 ```
240
241 ### Single *shadow* Jar File
242
243 The shadow jar file is a single `.jar` that contains all required classes and resources from `jalview.jar` 
244 and all of the supporting libraries in `j11lib/*.jar` merged into one `.jar` archive 
245 file.  A default launching class (`MAIN-CLASS: jalview.bin.Launcher`) is specified in the `.jar` 
246 manifest file (`META/MANIFEST.MF`) so a start class doesn't need to be specified.
247
248 Build the shadow jar file in `build/lib/jalview-all-11.jar` with
249
250 ```bash
251 gradle shadowJar
252 ```
253 and run it with
254
255 ```bash
256 java -jar build/lib/jalview-all-11.jar
257 ```
258
259 Because no arguments are required, most OSes will associate a `.jar` file with the 
260 `java` application (if this has been installed through the OS and not just a local 
261 unzip) as a `-jar` argument so you may find you can launch `jalview-all-11.jar` 
262 just by double-clicking on it)!
263
264 > The `shadowJar` task is not a requirement for any other task, so to build the shadow 
265 jar file you must specify the `shadowJar` task.
266
267 > The shadow jar file represents probably the simplest way to distribute the Jalview application to machines that already have a Java 11 installed, 
268 although without the many and compelling benefits of the `getdown` launcher.
269
270
271 ### Building the `getdown` launcher
272
273 We have made significant customisations to the `getdown` launcher which you can find 
274 in `getdown/src/getdown`.
275
276 > You don't need to build this afresh as the required `gradle-core.jar` 
277 and `gradle-launcher.jar` files are already distributed in `j11lib` and `getdown/lib` but if you want to, then 
278 you'll need a working Maven and also a Java 8 JDK.  Ensure the Java 8 `javac` is forefront 
279 in your path and do
280 >
281 >```bash
282 >cd getdown/src/getdown
283 >mvn clean package -Dgetdown.host.whitelist="jalview.org,*.jalview.org"
284 >```
285 > and you will find the required `.jar` files in `core/target/gradle-core-XXX.jar` 
286 and `launcher/target/gradle-launcher-XXX.jar`.  The `gradle-core.jar` should then be copied 
287 to all three of the `j8lib`, `j11lib` and `getdown/lib` folders, whilst the `gradle-launcher.jar` only 
288 needs to be copied to `getdown/lib`.
289 >
290 >The `mvn` command should ideally include the `-Dgetdown.host.whitelist=*.jalview.org` setting. 
291  This, and the necessary file copying commands, can be found in `getdown/src/getdown/mvn_cmd`.
292
293 To assemble Jalview with `getdown` use the following gradle task:
294
295 ```bash
296 gradle getdown
297 ```
298
299 This puts all the necessary files to launch Jalview with `getdown`
300 into `getdown/website/11/`.  This could be treated as the reference folder 
301 for `getdown`, which is where a getdown launcher will check to see if the Jalview application 
302 files it has are up to date, and download if they aren't or it simply doesn't have 
303 them.
304
305 A minimal getdown-launcher can be found in `getdown/files/11/` which checks its up-to-date 
306 status with (the absolute path to) `getdown/website/11/`.
307
308 This can be launched with
309
310 ```bash
311 java -jar getdown/files/11/getdown-launcher.jar getdown/files/11/ jalview
312 ```
313
314 > We've already met the `-jar file.jar` arguments.  The next argument is the working folder for 
315 getdown, and the final argument, "`jalview`", is a getdown application id (only "`jalview`" 
316 is defined here).
317
318
319 ### Running tests
320
321 There are substantial tests written for Jalview that use TestNG, which you can run with
322
323 ```bash
324 gradle test
325 ```
326
327 These normally take around 5 - 10 minutes to complete and outputs its full results into 
328 the `tests/` folder.  A summary of results should appear in your console.
329
330
331 ### Installer packaging with *install4j*
332
333 Jalview is currently using *install4j* <https://www.ej-technologies.com/products/install4j/overview.html> 
334 as its installer packaging tool.
335
336 If you have a licensed installation of *install4j* you can build Jalview installers 
337 by running
338
339 ```bash
340 gradle installers
341 ```
342
343 though you may need to fiddle with the `install4j` and `copyInstall4jTemplate` tasks 
344 in `build.gradle` file to point to your installation of *install4j* and also to bundled 
345 JREs if you want to bundle those into the installers.
346
347 If you want more details, get in touch on our development mailing list <jalview-dev@jalview.org>. 
348 Sign up at <http://www.compbio.dundee.ac.uk/mailman/listinfo/jalview-dev>.
349
350
351 ## Building in Eclipse
352
353 We develop in Eclipse, and support settings to develop and save Jalview source code 
354 in our preferred style.  We also support running the Jalview application, debugging 
355 and running tests with TestNG from within Eclipse.
356
357 To get Jalview set up as a project in Eclipse, we recommend using at least the 2019-03 
358 version of Eclipse IDE for Java Developers which you can download from the Eclipse 
359 website: <https://www.eclipse.org/downloads/>
360
361 Once installed, we also recommend installing several plugins from the Eclipse Marketplace.
362
363 To do so, launch Eclipse, and go to Help->Eclipse Marketplace...
364
365 Search for and install:
366
367 1. Buildship Gradle Integration 3.0 (or greater)
368 1. Groovy Development Tools 3.4.0 (or greater)
369 1. TestNG for Eclipse (optional -- only needed if you want to run tests from Eclipse)
370
371 > At time of writing, TestNG for Eclipse does not show up in the Eclipse Marketplace 
372 as the latest released version does not install in Eclipse 2019-03.
373 However, you can install a working beta of TestNG for Eclipse by going to
374 >
375 Help->Install New Software...
376 >
377 and entering
378 >
379 `TestNG Eclipse Composite P2 Repo - http://beust.com/eclipse-beta`
380 >
381 into the *Work with* box and click on the *Add...* button.
382 >
383 Eclipse might pause for a bit with the word *Pending* in the table below at this point, but it will eventually list TestNG with 
384 a selection box under the *Name* column.
385 >
386 Select *TestNG* and carry on through the 
387 install process to install the TestNG plugin.
388
389 After installing the plugins, it is a good to get Java 11 set up in Eclipse as the default JRE.
390
391 To do this go to Preferences (Eclipse->Preferences in macOS, File->Preferences 
392 on Windows or Window->Preferences on Linux) and find
393
394 Java -> Installed JREs
395
396 If your Java 11 installation is not listed, click on
397
398 *Add* -> Standard VM -> *Next*
399
400 and enter the JRE home.  You can browse to where it was installed. Give it a name (like "AdoptOpenJDK 11").  Select this JDK 
401 as the default JRE and click on *Apply and Close*.
402
403
404 You can now import Jalview.  It is important to import 
405 Jalview as a Gradle project (not as a Java project), so go to
406
407 File->Import...
408
409 find and select
410
411 Gradle->Existing Gradle Project
412
413 and then click on the *Next >* button.
414
415 In the following options, it is the *Project Root Directory* you should set to be the 
416 `jalview` folder that git downloaded.  Then you can click on the *Finish* button.
417
418
419 ## Gradle properties
420
421 There are a lot of properties configured in `gradle.properties` which we strongly recommend 
422 being left as they are unless you have a specific problem with the build process.
423
424 There are a few gradle properties you might want to set on the command line with the 
425 `-P` flag when building a version of Jalview with specific requirements:
426
427 #### `JAVA_VERSION`
428 This changes the *target* java bytecode version
429 > NOTE that you will need to use a Java 11 (or greater) JDK Java compiler to build 
430 Jalview for any byte-code target version.
431
432 Valid values are `11` and `1.8`.
433
434 e.g.
435
436 ```bash
437 gradle shadowJar -PJAVA_VERSION=1.8
438 ```
439
440 When using `-PJAVA_VERSION=1.8` the libraries from `j8lib` (instead of `j11lib`) will be used in the compile  
441 and runtime classpath and also used in the `makeDist` build step.  Where a Java version of `11` is used in folder and file names, it will 
442 instead use `1.8`.  Also if you are building installer packages with *install4j* the 
443 package builder will look for JRE 1.8 bundles to package in the installers.
444
445 > Note that continued development of Jalview will assume a Java 11+ runtime environment, 
446 the 2.11.0 release will run under a Java 1.8 JRE with a few minor features disabled.
447
448 #### `CHANNEL`
449 This changes the `appbase` setting in `getdown.txt` (`appbase` is where the getdown launcher 
450 looks to see if there's an updated file) to point to a particular Jalview 
451 channel.  Expected values are `FILE`, `STABLE`, `DEVELOPMENT`, or a specific version 
452 of Jalview like `2.11` or `2.10.5`.
453
454 A value of `FILE` behaves differently to the other expected values and will use a local 
455 file-system scheme URI instead of a Jalview release channel.  This `file:` scheme URI 
456 uses an absolute path to the `getdown/website/<JAVA_VERSION>`
457
458 On a regular development machine, this property will default to `LOCAL`.
459
460 e.g.
461 ```bash
462 gradle getdown -PCHANNEL=DEVELOPMENT
463 ```
464
465 #### `MEDIA_TYPES`
466 If you are building *install4j* installers (requires *install4j* to be installed) then this property specifies a comma-separated 
467 list of media types (i.e. platform specific installers) *install4j* should actually build.
468
469 Currently the valid values are
470 `linuxDeb`, 
471 `linuxRPM`, 
472 `macosArchive`, 
473 `unixArchive`, 
474 `unixInstaller`, 
475 `windows` 
476
477 The default value is all of them.
478
479 e.g.
480 ```bash
481 gradle installers -PJAVA_VERSION=1.8 -PMEDA_TYPE=macosArchive
482 ```
483
484 To get an up-to-date list of possible values, you can run
485
486 ```bash
487 perl -n -e 'm/^\s*<(\w+)[^>]*\bmediaFileName=/ && print "$1\n";' utils/install4j/install4j_template.install4j  | sort -u
488 ```
489 in the `jalview` root folder.