Skip to content

Commit ed03b9a

Browse files
authored
Add example standalone module (#48)
1 parent bf04247 commit ed03b9a

File tree

19 files changed

+912
-0
lines changed

19 files changed

+912
-0
lines changed

standalone/README.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
A simple LabKey module to illustrate how to build outside of the LabKey enlistment.
2+
3+
This simple module was created using LabKey's 'createModule' command and then
4+
removing a few things. It does not include any JSP or XSD files. To include
5+
these, you may need additional tasks for creating additional Jar files, or you
6+
can use the LabKey Gradle plugins. The file `withPlugins_build.gradle` provides
7+
an example of how to use the plugins with a standalone module.
8+
9+
For more details see:
10+
- https://www.labkey.org/Documentation/wiki-page.view?name=gradleModules
11+
- https://github.com/LabKey/gradlePlugin - the repository for the plugins used to
12+
build LabKey
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.labkey.api.standalone;
2+
3+
public class SomeUtils
4+
{
5+
private Integer usefulCount = 0;
6+
7+
public void doSomethingUseful()
8+
{
9+
usefulCount++;
10+
}
11+
12+
public Integer getUsefulCount()
13+
{
14+
return usefulCount;
15+
}
16+
}

standalone/build.gradle

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// This is a sample gradle file for creating a stand-alone Java module that can be deployed in a LabKey server
2+
// instance. This file assumes the module layout described in the LabKey documentation
3+
// (https://labkey.org/Documentation/wiki-page.view?name=moduleDirectoryStructures)
4+
// but, for simplicity, does not include JSP files that would be compiled and collected into a JSP jar file
5+
// or XSD files that would be transformed into XMLBeans objects and include in the main jar.
6+
//
7+
// You can refer to the gradlePlugins source code (https://github.com/LabKey/gradlePlugin) for more details on the
8+
// full build process implemented for modules within the LabKey source tree.
9+
//
10+
// Also for simplicity, this example deliberately does not use the LabKey Gradle plugins, but if your module
11+
// has JSPs or other client-side code and XMLBeans you may want to consider using these plugins. You will
12+
// need to add a dependency on the plugins (see the settings.gradle file in LabKey Server's root enlistment)
13+
// and then apply the appropriate plugins, probably most simply the 'org.labkey.module' plugin, which, in turn,
14+
// will apply, for example, the JSP and NpmRun plugins, among others.
15+
//
16+
// Then run the command
17+
// ./gradlew module
18+
// This will create a .module file for your project and deposit it in the build directory
19+
// for your standalone module.
20+
//
21+
// See the Gradle documentation for more information (https://docs.gradle.org).
22+
//
23+
import java.util.regex.Matcher
24+
import java.util.regex.Pattern
25+
26+
apply plugin: 'java'
27+
28+
project.version="20.7-SNAPSHOT"
29+
30+
ext {
31+
// The following are convenience variables for the various output directories used below
32+
explodedModuleDir = "${project.buildDir}/explodedModule"
33+
libDir = "${explodedModuleDir}/lib"
34+
configDir = "${explodedModuleDir}/config"
35+
}
36+
37+
repositories
38+
{
39+
// Use this repository when relying on release versions of the LabKey artifacts and their external dependencies
40+
maven {
41+
url "${project.artifactoryContextUrl}/libs-release"
42+
}
43+
// Use this repository when relying on snapshot versions of LabKey artifacts or requiring snapshot external dependencies
44+
maven {
45+
url "${project.artifactoryContextUrl}/libs-snapshot"
46+
}
47+
jcenter() // include the bintray/jcenter repository separately in case lookup with previous repository fails
48+
}
49+
50+
configurations
51+
{
52+
external // Define a configuration for use in specifying which libraries should be included in the module's lib directory
53+
implementation.extendsFrom(external)
54+
}
55+
56+
dependencies
57+
{
58+
implementation "org.labkey.api:internal:${labkeyVersion}" // Dependency on the api jar file for internal module
59+
implementation "org.labkey.api:issues:${labkeyVersion}" // An example of declaring a dependency on the API jar for a module
60+
implementation "org.labkey.api:labkey-client-api:${labkeyClientApiVersion}" // Dependency on the LabKey client api
61+
external "commons-beanutils:commons-beanutils:${commonsBeanutilsVersion}" // An external dependency to be included in the module's lib directory
62+
}
63+
64+
65+
project.sourceSets
66+
{
67+
main { // source files for the main Jar file
68+
java {
69+
srcDirs = ['src']
70+
}
71+
}
72+
module {
73+
resources { // resources to be included in the module Jar file
74+
srcDirs = ['resources']
75+
}
76+
output.resourcesDir = explodedModuleDir
77+
}
78+
api { // source files for the api Jar file
79+
java {
80+
srcDirs = ['api-src']
81+
}
82+
}
83+
// use this if you have a spring configuration file in your project
84+
spring {
85+
resources {
86+
srcDirs = ["webapp/WEB-INF"]
87+
}
88+
output.resourcesDir = configDir
89+
}
90+
}
91+
92+
project.tasks.register("apiJar", Jar) {
93+
Jar jar ->
94+
jar.group = "Build"
95+
jar.description = "produce jar file for api"
96+
jar.from project.sourceSets.api.output
97+
jar.archiveBaseName.set("${project.name}_api")
98+
jar.destinationDirectory = project.file(libDir)
99+
jar.dependsOn(project.apiClasses)
100+
}
101+
102+
project.jar {
103+
Jar jar ->
104+
jar.archiveBaseName.set(project.name)
105+
jar.destinationDirectory = project.file(libDir)
106+
jar.dependsOn(project.tasks.apiJar)
107+
}
108+
109+
project.tasks.register("copyExternalLibs", Copy) {
110+
CopySpec copy ->
111+
copy.group = "Build"
112+
copy.description = "copy the dependencies declared in the 'external' configuration into the lib directory of the built module"
113+
copy.from project.configurations.external
114+
copy.into libDir
115+
}
116+
117+
project.tasks.register('moduleXml') {
118+
Task task ->
119+
task.group = "Build"
120+
task.description = "Create the module.xml file from the module.properties and module.template.xml files"
121+
task.inputs.file(project.file("module.template.xml"))
122+
task.inputs.file(project.file("module.properties"))
123+
task.outputs.file(new File(configDir.toString(), "module.xml"))
124+
task.doLast
125+
{
126+
final Pattern PROPERTY_PATTERN = Pattern.compile("@@([^@]+)@@")
127+
128+
Properties modProperties = new Properties()
129+
FileInputStream propertiesStream = new FileInputStream("module.properties")
130+
modProperties.load(propertiesStream)
131+
propertiesStream.close()
132+
133+
InputStream is = new FileInputStream(new File("module.template.xml"))
134+
135+
if (is == null) {
136+
throw new GradleException("Could not find template file 'module.template.xml'.")
137+
}
138+
139+
project.mkdir(configDir)
140+
File moduleXmlFile = new File((String) configDir, "module.xml")
141+
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(moduleXmlFile))
142+
143+
is.readLines().each {
144+
String line ->
145+
Matcher matcher = PROPERTY_PATTERN.matcher(line)
146+
String newLine = line
147+
while (matcher.find()) {
148+
String property = (String) modProperties.get(matcher.group(1))
149+
newLine = newLine.replace(matcher.group(), property == null ? "" : property)
150+
}
151+
writer.println(newLine)
152+
}
153+
writer.close()
154+
is.close()
155+
}
156+
157+
}
158+
159+
project.tasks.register("module", Jar) {
160+
Jar jar ->
161+
jar.group = "Build"
162+
jar.description = "create the module file for this project"
163+
jar.from explodedModuleDir
164+
jar.archiveBaseName.set(project.name)
165+
jar.archiveExtension.set('module')
166+
jar.destinationDirectory = project.buildDir
167+
jar.dependsOn(project.tasks.moduleXml)
168+
jar.dependsOn(project.tasks.processModuleResources)
169+
jar.dependsOn(project.tasks.copyExternalLibs)
170+
jar.dependsOn(project.tasks.jar)
171+
jar.dependsOn(project.tasks.processSpringResources)
172+
}
173+

standalone/gradle.properties

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Location of the repository for various artifacts used in the build
2+
artifactoryContextUrl=https://artifactory.labkey.com/artifactory
3+
4+
# Version 1.17.0 or higher is required to fully support building stand-alone modules with the LabKey Gradle plugins
5+
gradlePluginsVersion=1.17.0
6+
7+
# versions of artifacts required as dependencies
8+
labkeyVersion=20.7.0
9+
labkeyClientApiVersion=1.3.0
10+
11+
# used by the LabKey Jsp Gradle plugin for declaring dependencies
12+
apacheTomcatVersion=8.5.51
13+
servletApiVersion=3.0
14+
15+
# Example external dependency used in build file
16+
commonsBeanutilsVersion=1.7.0
52.9 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sun Jun 04 20:22:05 PDT 2017
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip

0 commit comments

Comments
 (0)