-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileModule.groovy
More file actions
452 lines (406 loc) · 20.4 KB
/
FileModule.groovy
File metadata and controls
452 lines (406 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright (c) 2017-2018 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.gradle.plugin
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.CopySpec
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.java.archives.Manifest
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.bundling.Jar
import org.labkey.gradle.plugin.extension.LabKeyExtension
import org.labkey.gradle.plugin.extension.ModuleExtension
import org.labkey.gradle.plugin.extension.ServerDeployExtension
import org.labkey.gradle.task.ModuleXmlFile
import org.labkey.gradle.util.BuildUtils
import org.labkey.gradle.util.GroupNames
import org.labkey.gradle.util.PomFileHelper
import org.labkey.gradle.util.TaskUtils
/**
* This class is used for building a LabKey file-based module, which contains only client-side code.
* It also serves as a base class for the Java module classes.
*/
class FileModule implements Plugin<Project>
{
static boolean shouldDoBuild(Project project, boolean logMessages)
{
List<String> indicators = new ArrayList<>()
if (!project.file(ModuleExtension.MODULE_PROPERTIES_FILE).exists())
indicators.add(ModuleExtension.MODULE_PROPERTIES_FILE + " does not exist")
if (indicators.size() > 0 && logMessages)
{
project.logger.quiet("${project.path} build skipped because: " + indicators.join("; "))
}
return indicators.isEmpty()
}
@Override
void apply(Project project)
{
def moduleKey = project.getName().toLowerCase()
def shouldBuild = shouldDoBuild(project, true)
if (project.findProject(BuildUtils.getServerProjectPath(project.gradle)) != null) {
ServerDeployExtension deployExt = BuildUtils.getServerProject(project).extensions.getByType(ServerDeployExtension.class)
def otherPath = deployExt.getFoundModule(moduleKey)
if (otherPath != null && !otherPath.equals(project.getPath()) && project.findProject(otherPath) != null) {
if (shouldBuild)
throw new IllegalStateException("Found duplicate module '${project.getName()}' in ${project.getPath()} and ${otherPath}. Modules should have unique names; Rename one or exclude it from your build.")
} else if (shouldBuild) {
deployExt.addFoundModule(moduleKey, project.getPath())
}
}
if (shouldBuild) {
project.apply plugin: 'java'
project.apply plugin: 'org.labkey.build.base'
project.extensions.create("lkModule", ModuleExtension, project)
addSourceSet(project)
applyPlugins(project)
addConfigurations(project)
addTasks(project)
addDependencies(project)
addArtifacts(project)
}
}
private static void addSourceSet(Project project)
{
ModuleResources.addSourceSet(project)
}
protected static void applyPlugins(Project project)
{
project.apply plugin: 'maven-publish'
if (SpringConfig.isApplicable(project))
project.apply plugin: 'org.labkey.build.springConfig'
if (Webapp.isApplicable(project))
project.apply plugin: 'org.labkey.build.webapp'
ClientLibraries.addTasks(project)
if (NpmRun.isApplicable(project))
project.apply plugin: 'org.labkey.build.npmRun'
}
protected void addConfigurations(Project project)
{
project.configurations
{
published
}
}
protected void addTasks(Project project)
{
ModuleResources.addTasks(project)
var moduleXmlTask = project.tasks.register('moduleXml', ModuleXmlFile) {
ModuleXmlFile task ->
List<String> moduleDependencies = []
project.configurations.modules.dependencies.each {
Dependency dep -> moduleDependencies += dep.getName()
}
if (!moduleDependencies.isEmpty())
project.lkModule.setPropertyValue("ModuleDependencies", moduleDependencies.join(", "))
task.getModuleProperties().set(project.lkModule.getModProperties())
if (project.file("build.gradle").exists())
task.inputs.file(project.file("build.gradle"))
task.outputs.cacheIf { false } // disable build caching. Has too many undeclared inputs.
}
var moduleTask = project.tasks.register("module", Jar) {
Jar jar ->
jar.group = GroupNames.MODULE
jar.description = "create the module file for this project"
jar.from project.labkey.explodedModuleDir
jar.exclude '**/*.uptodate'
jar.exclude "META-INF/${project.name}/**"
jar.exclude 'gwt-unitCache/**'
jar.archiveBaseName.set(project.name)
jar.archiveExtension.set('module')
jar.destinationDirectory.set(project.layout.buildDirectory)
jar.outputs.cacheIf({true})
}
moduleTask.configure {
it.dependsOn(project.tasks.named('processResources'))
it.dependsOn(moduleXmlTask)
setJarManifestAttributes(project, (Manifest) it.manifest)
if (!LabKeyExtension.isDevMode(project) && BuildUtils.haveMinificationProject(project.gradle))
it.dependsOn(project.tasks.named('compressClientLibs'))
}
project.tasks.named("build").configure {dependsOn(moduleTask)}
project.tasks.clean {
dependsOn(project.tasks.named('cleanModule'))
if (BuildUtils.haveMinificationProject(project.gradle))
dependsOn(project.tasks.named('cleanClientLibs'))
}
project.artifacts
{
// TODO: Figure out how to add this artifact without resolving 'module' task
published moduleTask.get()
}
project.tasks.register('deployModule')
{ Task task ->
task.group = GroupNames.MODULE
task.description = "copy a project's .module file to the local deploy directory"
task.inputs.files moduleTask
task.outputs.file "${ServerDeployExtension.getModulesDeployDirectory(project)}/${moduleTask.get().outputs.getFiles()[0].getName()}"
task.doLast {
project.copy { CopySpec copy ->
copy.from moduleTask
copy.from project.configurations.modules
copy.into "${BuildUtils.getRootBuildDirPath(project)}/$ServerDeploy.STAGING_MODULES_DIR"
copy.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
}
project.copy { CopySpec copy ->
copy.from moduleTask
copy.from project.configurations.modules
copy.into ServerDeployExtension.getModulesDeployDirectory(project)
copy.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
}
BuildUtils.updateRestartTriggerFile(project)
}
task.notCompatibleWithConfigurationCache("Needs its own class to do the two copies (one to staging and one to deploy or possibly two Copy tasks chained together.")
}
project.tasks.register('undeployModule', Delete) {
Delete task ->
task.group = GroupNames.MODULE
task.description = "remove a project's .module file and the unjarred file from the deploy directory"
task.configure(
{ Delete delete ->
getModuleFilesAndDirectories(project).forEach({
File file ->
if (file.isDirectory())
delete.inputs.dir file
else
delete.inputs.file file
})
})
task.doFirst {
undeployModule(project)
Api.deleteModulesApiJar(project)
}
task.doLast {
BuildUtils.updateRestartTriggerFile(project)
}
task.notCompatibleWithConfigurationCache("Does multiple deletes using project.delete. Should have its own class.")
}
project.tasks.register("reallyClean") {
Task task ->
task.group = GroupNames.BUILD
task.description = "Deletes the build, staging, and deployment directories of this module"
task.dependsOn(project.tasks.clean, project.tasks.undeployModule)
TaskUtils.addOptionalTaskDependency(project, task, "cleanNodeModules")
TaskUtils.addOptionalTaskDependency(project, task, "cleanSchemasCompile")
}
}
static void setJarManifestAttributes(Project project, Manifest manifest)
{
manifest.attributes(
"Implementation-Version": project.version,
"Implementation-Title": project.lkModule.getPropertyValue("Label", project.name),
"Implementation-Vendor": "LabKey"
)
}
static undeployModule(Project project)
{
getModuleFilesAndDirectories(project).forEach({File file ->
project.delete file
})
}
/**
* Finds all module files and directories for a project included in the deployment directory and/or staging directory
* @param project the project to find module files for
* @param includeDeployed include .module files and directories in the build/deploy directory
* @param includeStaging include .module files in the build/staging directory
* @return list of files and directories for this module with the deploy .module files first, followed by the deploy directories
* followed by the staging .module files.
*/
static List<File> getModuleFilesAndDirectories(Project project, Boolean includeDeployed = true, Boolean includeStaging=true)
{
String moduleFilePrefix = "${project.name}-"
List<File> files = new ArrayList<>()
if (includeDeployed)
{
File deployDir = new File(ServerDeployExtension.getModulesDeployDirectory(project))
if (deployDir.isDirectory())
{
// first add the files because we want to delete these first. If the directory goes away and the .module file is there
// the directory might get recreated because of listeners.
files.addAll(deployDir.listFiles(new FileFilter() {
@Override
boolean accept(final File file)
{
return file.isFile() && file.getName().startsWith(moduleFilePrefix)
}
})
)
// then add the directories
files.addAll(deployDir.listFiles(new FileFilter() {
@Override
boolean accept(final File file)
{
return file.isDirectory() && (file.getName().startsWith("${project.name}-") || file.getName().equalsIgnoreCase(project.name))
}
})
)
}
}
// staging has only the .modules files
if (includeStaging)
{
File stagingDir = BuildUtils.getRootBuildDirFile(project, ServerDeploy.STAGING_MODULES_DIR)
if (stagingDir.isDirectory())
{
files.addAll(stagingDir.listFiles(new FilenameFilter() {
@Override
boolean accept(final File dir,
final String name)
{
return name.startsWith(moduleFilePrefix)
}
})
)
}
}
return files
}
protected void addArtifacts(Project project)
{
project.afterEvaluate {
project.publishing {
publications {
if (project.hasProperty('module'))
{
Properties pomProperties = LabKeyExtension.getModulePomProperties(project)
modules(MavenPublication) { pub ->
// Use org.labkey.module for module dependency groupIds instead of "org.labkey"
pub.groupId = pomProperties.get('groupId')
pub.artifact(project.tasks.module)
PomFileHelper pomUtil = new PomFileHelper(pomProperties, project, true)
pom {
name = project.name
description = pomProperties.getProperty("Description")
url = pomProperties.getProperty("OrganizationURL")
// developers PomFileHelper.getLabKeyTeamDevelopers()
licenses pomUtil.getLicense()
organization pomUtil.getOrganization()
// scm PomFileHelper.getLabKeyScm()
withXml {
pomUtil.getDependencyClosure(asNode(), true)
}
}
}
}
if (project.hasProperty('apiJar'))
{
Properties pomProperties = LabKeyExtension.getApiPomProperties(project)
apiLib(MavenPublication) { pub ->
pub.groupId = pomProperties.get('groupId')
pub.artifact(project.tasks.apiJar)
PomFileHelper pomUtil = new PomFileHelper(pomProperties, project, false)
pom {
name = project.name
description = pomProperties.getProperty("Description")
url = pomProperties.getProperty("OrganizationURL")
// developers PomFileHelper.getLabKeyTeamDevelopers()
licenses pomUtil.getLicense()
organization pomUtil.getOrganization()
// scm PomFileHelper.getLabKeyScm()
withXml {
pomUtil.getDependencyClosure(asNode(), false)
}
}
}
}
else if (project.path.equals(BuildUtils.getApiProjectPath(project.gradle)))
{
Properties pomProperties = LabKeyExtension.getApiPomProperties(project)
apiLib(MavenPublication) { pub ->
pub.groupId = pomProperties.get('groupId')
pub.artifact(project.tasks.jar)
PomFileHelper pomUtil = new PomFileHelper(pomProperties, project, false)
pom {
name = project.name
description = pomProperties.getProperty("Description")
url = PomFileHelper.LABKEY_ORG_URL
developers PomFileHelper.getLabKeyTeamDevelopers()
licenses pomUtil.getLicense()
organization pomUtil.getOrganization()
// scm PomFileHelper.getLabKeyScm()
withXml {
pomUtil.getDependencyClosure(asNode(), false)
}
}
}
}
}
if (BuildUtils.shouldPublish(project))
{
if (LabKeyExtension.isDevMode(project))
throw new GradleException("Modules produced with deployMode=dev are not portable and should never be published.")
project.artifactoryPublish {
if (project.hasProperty('module'))
{
dependsOn project.tasks.named("module")
}
if (project.hasProperty('apiJar'))
{
dependsOn project.tasks.named("apiJar")
}
else if (project.path.equals(BuildUtils.getApiProjectPath(project.gradle)))
{
dependsOn project.tasks.named("jar")
}
publications('modules', 'apiLib')
}
}
}
}
}
private static void addDependencies(Project project)
{
Project serverProject = BuildUtils.getServerProject(project)
if (serverProject != null && serverProject != project)
{
project.evaluationDependsOn(BuildUtils.getServerProjectPath(project.gradle))
// This is done after the project is evaluated otherwise the dependencies for the modules configuration will not have been added yet.
project.afterEvaluate({
BuildUtils.addLabKeyDependency(project: serverProject, config: 'modules', depProjectPath: project.path, depProjectConfig: 'published', depExtension: 'module')
BuildUtils.addLabKeyDependency(project: serverProject, config: 'builtModules', depProjectPath: project.path, depProjectConfig: 'published', depExtension: 'module')
try {
project.configurations.named("modules") {
Configuration config -> {
config.dependencies.each {
Dependency dep ->
if (dep instanceof ProjectDependency) {
ProjectDependency projectDep = (ProjectDependency) dep
if (shouldDoBuild(project.project(projectDep.getPath()), false)) {
BuildUtils.addLabKeyDependency(project: serverProject, config: 'modules', depProjectPath: projectDep.getPath(), depProjectConfig: 'published', depExtension: 'module')
BuildUtils.addLabKeyDependency(project: serverProject, config: 'builtModules', depProjectPath: projectDep.getPath(), depProjectConfig: 'published', depExtension: 'module')
} else {
serverProject.dependencies.add("modules", BuildUtils.getLabKeyArtifactName(project, projectDep.getPath(), projectDep.version, "module"))
serverProject.dependencies.add("downloadedModules", BuildUtils.getLabKeyArtifactName(project, projectDep.getPath(), projectDep.version, "module"))
}
} else {
serverProject.dependencies.add("modules", dep)
serverProject.dependencies.add("downloadedModules", dep)
}
}
}
}
} catch (UnknownDomainObjectException ignore) { }
})
}
}
}