This topic is under construction.

This tutorial shows you how to create a new Java module and deploy it to the server.

Create a Java Module

The createModule Gradle task
The Gradle task called createModule makes it easy to create a template Java module with the correct file structure and template Controller classes. We recommend using it instead of trying to copy an existing module, as renaming a module requires editing and renaming many files.

Invoke createModule

> gradlew createModule

It will prompt you for the following 5 parameters:

  1. The module name. This should be a single word (or multiple words concatenated together), for example MyModule, ProjectXAssay, etc. This is the name used in the Java code, so you should follow Java naming conventions.
  2. A directory in which to put the files.
  3. Will this module create and manage a database schema? (Y/n)
  4. Create test stubs (y/N)
  5. Create API stubs (y/N)
Enter the following parameters:

1. "JavaTutorial" 
2. "C:\dev\labkey\trunk\externalModules\JavaTutorial" (depending on where your externalModules directory is)
3. Y
4. N
5. N

The JavaTutorial dir will be created, and the following resources added to it:

JavaTutorial
├── module.properties
├── resources
│   ├── schemas
│   │   ├── dbscripts
│   │   │   ├── postgresql
│   │   │   │   └── javatutorial-<0.00>-<currentver>.sql
│   │   │   └── sqlserver
│   │   │   └── javatutorial-<0.00>-<currentver>.sql
│   │   └── javatutorial.xml
│   └── web
└── src
└── org
└── labkey
└── Tutorial
├── TutorialContainerListener.java
├── TutorialController.java
├── TutorialManager.java
├── TutorialModule.java
├── TutorialSchema.java
└── view
└── hello.jsp

ContainerListener class todo

Controller class
This is a subclass of SpringActionController that links requests from a browser to code in your application.

Manager class
In LabKey Server, the Manager classes encapsulate much of the business logic for the module. Typical examples include fetching objects from the database, inserting, updating, and deleting objects, and so forth.

Module class
This is the entry point for LabKey Server to talk to your module. Exactly one instance of this class will be instantiated. It allows your module to register providers that other modules may use.

Schema class
Schema classes provide places to hook in to the LabKey Server Table layer, which provides easy querying of the database and object-relational mapping.

Schema XML file
This provides metadata about your database tables and views. In order to pass the developer run test (DRT), you must have entries for every table and view in your database schema. To regenerate this XML file, see Modules: Database Transition Scripts. For more information about the DRT, see Check in to the Source Project.

web directory
All of that static web content that will be served by Tomcat should go into this directory. These items typically include things like .gif and .jpg files. The contents of this directory will be combined with the other modules' webapp content, so we recommend adding content in a subdirectory to avoid file name conflicts.

.sql files
These files are the scripts that create and update your module's database schema. They are automatically run at server startup time. See the Modules: SQL Scripts for details on how to create and modify database tables and views. LabKey Server supports these scripts in PostgreSQL and Microsoft SQL Server.

module.properties
At server startup time, LabKey Server uses this file to determine your module's name, class, and dependencies.

Build and Deploy the Java Module

1. In the root directory of the module, add a build.gradle file with this content:

apply plugin: 'java'
apply plugin: 'org.labkey.module'

2. Add the module to your settings.gradle file:

include :externalModules:JavaTutorial

3. In IntelliJ, on the Gradle tab, sync by clicking the (Refresh) button . For details see Gradle: How to Add Modules.

4. To build the module, use the targeted Gradle task:

gradlew :externalModules:tutorial:deployModule

Or use the main deployApp target, which will rebuild all modules.

You can run these Gradle tasks via the command line, or using IntelliJ's Gradle tab.

Either task will compile your Java files and JSPs, package all code and resources into a .module file, and deploy it to your local server.

4. Enable the module in a LabKey folder. The BeginAction (the "home page" for the module) is available at:

or go to > Go to Module > JavaTutorial

Add a Module API

A module may define its own API which is available to the implementations of other modules; the createModule task described above will offer to create the required files for you. We declined this step in the above tutorial. If you say yes to the api and test options, the following files and directories are created:

JavaTutorial
│ module.properties

├───api-src
│ └───org
│ └───labkey
│ └───api
│ └───javatutorial
├───resources
│ ├───schemas
│ │ │ javatutorial.xml
│ │ │
│ │ └───dbscripts
│ │ ├───postgresql
│ │ │ javatutorial-0.00-19.01.sql
│ │ │
│ │ └───sqlserver
│ │ javatutorial-0.00-19.01.sql
│ │
│ └───web
├───src
│ └───org
│ └───labkey
│ └───javatutorial
│ │ JavaTutorialContainerListener.java
│ │ JavaTutorialController.java
│ │ JavaTutorialManager.java
│ │ JavaTutorialModule.java
│ │ JavaTutorialSchema.java
│ │
│ └───view
│ hello.jsp

└───test
├───sampledata
└───src
└───org
└───labkey
└───test
├───components
│ └───javatutorial
│ JavaTutorialWebPart.java

├───pages
│ └───javatutorial
│ BeginPage.java

└───tests
└───javatutorial
JavaTutorialTest.java

To add an API to an existing module:

  • Create a new api-src directory in the module's root.
  • In IntelliJ, go to File > New Module. In the New Module wizard:
    • Choose Java as the module type.
    • Click Next.
    • Name: JavaTutorial-api
    • Content Root: make the module's api-src directory the content root
    • Module file location: use the root of the module as the module file location
    • Click Finish.
  • In IntelliJ, go to File > Project Structure and choose Modules.
    • Select your new API module from the list (JavaTutorial-api).
    • In the Sources tab, remove the "src" directory as a source root, and make the api-src directory as a source root (if necessary).
    • In the Dependencies tab, add a Module Dependency on the "Internal" module and check the box to Export it. Find your original module from the list (JavaTutorial). Remove the dependency on the Internal module (if necesary), and add a Module Dependency on your new API module.
  • Remove the "src" directory under the api-src directory.
  • Create a new package under your api-src directory, "org.labkey.MODULENAME.api" or similar.
  • Add Java classes to the new package, and reference them from within your module.
  • Add a Module Dependency to any other modules that depend on your module's API.
  • Develop and test.
  • Commit your new Java source files.

Related Topic

Discussion

Was this content helpful?

Log in or register an account to provide feedback


previousnext
 
expand all collapse all