|
| 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 | + |
0 commit comments