The process of building and deploying LabKey Server from source is covered in this topic.
These are the various directories involved in the build process, listed in the order in which they are used in the build process:
- Source directory - Where the source code lives. This is where developers do their work. The build process uses this as input only.
- Build directory - Where all the building happens. This is the top-level output directory for the build. It is created if it does not exist when doing any of the build steps.
- Module build directory - Where the building happens for a module. This is the output directory for a module's build steps. It is created if it does not exist when doing a build.
- Staging directory - A gathering point for items that are to be deployed. This is just a way station for modules and other jars. The artifacts are collected here so they can be copied all at once into the deploy directory in order to prevent Tomcat from reloading the appliation multiple times when multiple modules are being updated.
- Deploy directory - The directory where the application is deployed and recognized by Tomcat.
The table below shows the commands used to create, modify and delete the various directories. Since various commands depend on others, there are generally several commands that will affect a given directory. We list here only the commands that would be most commonly used or useful.
(relative to <LABKEY_HOME>)
|Added to by...||Removed from by...||Deleted by...|
|Build||build||Any build step||Any cleaning step||cleanBuild|
One of the key things to note here is the cleanBuild command removes the entire build directory
, requiring all of the build steps to be run again. This is generally not
what you want or need to do since Gradle's up-to-date checks should be able to determine when things need to be rebuilt. (If that does not seem to be the case, please file a bug.) The one exception to this rule is when we change the LabKey version number after a release. Then you need to do a cleanBuild to get rid of the artifacts with the previous release version in their names.
Application Build Steps
Source code is built into jar files (and other types of files) in a module's build directory.
The result is a .module file, which contains potentially several jar files as well as other resources used by the module. This .module file is copied from the module's build directory into the staging directory and from there into the deploy directory. This is all usually accomplished with the './gradlew deployApp' command. The 'deployApp' task also configures and copies the labkey.xml tomcat context file. This file points to the deploy directory as a context path for Tomcat. Changes in that directory will therefore be noticed by Tomcat and cause it to reload the application.
Module Build Steps
A single module is deployed using the `./gradlew deployModule' command. This command will create a .module file in the module's build directory, then copy it first into the staging modules directory and then into the deploy modules directory.
A few important targets:
|gradlew tasks||Lists all of the available tasks in the current project.|
|Specify the database server to use. The first time you build LabKey, you need to invoke one of these targets to configure your database settings. If you are running against PostgreSQL, invoke the pickPg target. If you are running against SQL Server, invoke the pickMSSQL target. These targets copy the settings specified in the pg.properties or mssql.properties file, which you previously modified, to the LabKey configuration file, labkey.xml.|
|For convenience, every module can be deployed separately. If your changes are restricted to a single module then building just that module is a faster option than a full build. Example, to build the wiki module: 'gradlew :server:modules:wiki:deployModule'.|
|gradlew deployApp -PdeployMode=prod||Build the LabKey Server source for deployment to a production server. This build takes longer than 'gradlew deployApp' but results in artifacts that are suitable and optimized for production environments.|
|gradlew cleanBuild||Delete all artifacts from previous builds. This should be used sparingly as it requires Gradle to start all over again and not capitalize on the work it's done before.|
|Starts/stops the server in dev mode.|
|gradlew projects||Lists the current modules included in the build.|
|gradlew :server:test:uiTest||Open the test runner's graphical UI.|
|gradlew :server:test:uiTest -Psuite=DRT||Run the basic automated test suite.|
Gradle targets can also be invoked from within IntelliJ via the "Gradle projects" panel, but this has not been widely tested.
Parallel Build Feature
To improve performance, the LabKey build is configured by default to use Gradle's parallel build feature. This is controlled by these properties in the <LABKEY_ROOT>/gradle.properties file:
By changing these properties, you can turn off parallel builds or adjust the number of threads Gradle uses.
If Gradle warns that "JVM heap space is exhausted", add more memory as described in the topic Gradle Tips and Tricks