/modules/sdk/gradle-plugins-tlddoc-builder/src/main/java/com/liferay/gradle/plugins/tlddoc/builder/TLDDocBuilderPlugin.java

http://github.com/liferay/liferay-portal · Java · 255 lines · 165 code · 74 blank · 16 comment · 0 complexity · d7ed501ab2299d69a8f96c62958a7366 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.gradle.plugins.tlddoc.builder;
  15. import com.liferay.gradle.plugins.tlddoc.builder.tasks.TLDDocTask;
  16. import com.liferay.gradle.plugins.tlddoc.builder.tasks.ValidateSchemaTask;
  17. import com.liferay.gradle.util.GradleUtil;
  18. import java.io.File;
  19. import java.util.Collections;
  20. import java.util.concurrent.Callable;
  21. import org.gradle.api.Action;
  22. import org.gradle.api.Plugin;
  23. import org.gradle.api.Project;
  24. import org.gradle.api.artifacts.Configuration;
  25. import org.gradle.api.artifacts.DependencySet;
  26. import org.gradle.api.file.FileCollection;
  27. import org.gradle.api.file.SourceDirectorySet;
  28. import org.gradle.api.plugins.JavaBasePlugin;
  29. import org.gradle.api.plugins.JavaPlugin;
  30. import org.gradle.api.plugins.JavaPluginConvention;
  31. import org.gradle.api.plugins.PluginContainer;
  32. import org.gradle.api.tasks.Copy;
  33. import org.gradle.api.tasks.SourceSet;
  34. import org.gradle.api.tasks.TaskContainer;
  35. /**
  36. * @author Andrea Di Giorgi
  37. */
  38. public class TLDDocBuilderPlugin implements Plugin<Project> {
  39. public static final String CONFIGURATION_NAME = "tlddoc";
  40. public static final String COPY_TLDDOC_RESOURCES_TASK_NAME =
  41. "copyTLDDocResources";
  42. public static final String TLDDOC_TASK_NAME = "tlddoc";
  43. public static final String VALIDATE_TLD_TASK_NAME = "validateTLD";
  44. public static final String XML_PARSER_CONFIGURATION_NAME = "xmlParser";
  45. @Override
  46. public void apply(Project project) {
  47. Configuration tlddocConfiguration = addConfigurationTLDDoc(project);
  48. ValidateSchemaTask validateTLDTask = _addTaskValidateTLD(project);
  49. Copy copyTLDDocResourcesTask = _addTaskCopyTLDDocResources(project);
  50. _addTaskTLDDoc(copyTLDDocResourcesTask, validateTLDTask);
  51. _configureTasksTLDDoc(project, tlddocConfiguration);
  52. }
  53. protected static Configuration addConfigurationTLDDoc(
  54. final Project project) {
  55. Configuration configuration = GradleUtil.addConfiguration(
  56. project, CONFIGURATION_NAME);
  57. configuration.defaultDependencies(
  58. new Action<DependencySet>() {
  59. @Override
  60. public void execute(DependencySet dependencySet) {
  61. _addDependenciesTLDDoc(project);
  62. }
  63. });
  64. configuration.setDescription(
  65. "Configures Tag Library Documentation Generator for this project.");
  66. configuration.setVisible(false);
  67. return configuration;
  68. }
  69. private static void _addDependenciesTLDDoc(Project project) {
  70. GradleUtil.addDependency(
  71. project, CONFIGURATION_NAME, "taglibrarydoc", "tlddoc", "1.3");
  72. }
  73. private Copy _addTaskCopyTLDDocResources(final Project project) {
  74. Copy copy = GradleUtil.addTask(
  75. project, COPY_TLDDOC_RESOURCES_TASK_NAME, Copy.class);
  76. copy.from("src/main/tlddoc");
  77. copy.into(
  78. new Callable<File>() {
  79. @Override
  80. public File call() throws Exception {
  81. TLDDocTask tldDocTask = (TLDDocTask)GradleUtil.getTask(
  82. project, TLDDOC_TASK_NAME);
  83. return tldDocTask.getDestinationDir();
  84. }
  85. });
  86. copy.setDescription("Copies tag library documentation resources.");
  87. return copy;
  88. }
  89. private TLDDocTask _addTaskTLDDoc(
  90. Copy copyTLDDocResourcesTask, ValidateSchemaTask validateTLDTask) {
  91. Project project = copyTLDDocResourcesTask.getProject();
  92. final TLDDocTask tldDocTask = GradleUtil.addTask(
  93. project, TLDDOC_TASK_NAME, TLDDocTask.class);
  94. tldDocTask.dependsOn(copyTLDDocResourcesTask, validateTLDTask);
  95. tldDocTask.setDescription("Generates tag library documentation.");
  96. tldDocTask.setGroup(JavaBasePlugin.DOCUMENTATION_GROUP);
  97. PluginContainer pluginContainer = project.getPlugins();
  98. pluginContainer.withType(
  99. JavaPlugin.class,
  100. new Action<JavaPlugin>() {
  101. @Override
  102. public void execute(JavaPlugin javaPlugin) {
  103. _configureTaskTLDDocForJavaPlugin(tldDocTask);
  104. }
  105. });
  106. return tldDocTask;
  107. }
  108. private ValidateSchemaTask _addTaskValidateTLD(Project project) {
  109. final ValidateSchemaTask validateSchemaTask = GradleUtil.addTask(
  110. project, VALIDATE_TLD_TASK_NAME, ValidateSchemaTask.class);
  111. validateSchemaTask.setDescription("Validates TLD files.");
  112. PluginContainer pluginContainer = project.getPlugins();
  113. pluginContainer.withType(
  114. JavaPlugin.class,
  115. new Action<JavaPlugin>() {
  116. @Override
  117. public void execute(JavaPlugin javaPlugin) {
  118. _configureTaskValidateSchemaForJavaPlugin(
  119. validateSchemaTask);
  120. }
  121. });
  122. return validateSchemaTask;
  123. }
  124. private void _configureTasksTLDDoc(
  125. Project project, final Configuration tlddocConfiguration) {
  126. TaskContainer taskContainer = project.getTasks();
  127. taskContainer.withType(
  128. TLDDocTask.class,
  129. new Action<TLDDocTask>() {
  130. @Override
  131. public void execute(TLDDocTask tldDocTask) {
  132. _configureTaskTLDDocClasspath(
  133. tldDocTask, tlddocConfiguration);
  134. }
  135. });
  136. }
  137. private void _configureTaskTLDDocClasspath(
  138. TLDDocTask tldDocTask, FileCollection fileCollection) {
  139. tldDocTask.setClasspath(fileCollection);
  140. }
  141. private void _configureTaskTLDDocForJavaPlugin(TLDDocTask tldDocTask) {
  142. final Project project = tldDocTask.getProject();
  143. tldDocTask.setDestinationDir(
  144. new Callable<File>() {
  145. @Override
  146. public File call() throws Exception {
  147. JavaPluginConvention javaPluginConvention =
  148. GradleUtil.getConvention(
  149. project, JavaPluginConvention.class);
  150. return new File(
  151. javaPluginConvention.getDocsDir(), "tlddoc");
  152. }
  153. });
  154. tldDocTask.setIncludes(Collections.singleton("**/*.tld"));
  155. tldDocTask.setSource(
  156. new Callable<Iterable<File>>() {
  157. @Override
  158. public Iterable<File> call() throws Exception {
  159. return _getResourceDirs(project);
  160. }
  161. });
  162. }
  163. private void _configureTaskValidateSchemaForJavaPlugin(
  164. ValidateSchemaTask validateSchemaTask) {
  165. final Project project = validateSchemaTask.getProject();
  166. validateSchemaTask.setIncludes(Collections.singleton("**/*.tld"));
  167. validateSchemaTask.setSource(
  168. new Callable<Iterable<File>>() {
  169. @Override
  170. public Iterable<File> call() throws Exception {
  171. return _getResourceDirs(project);
  172. }
  173. });
  174. }
  175. private Iterable<File> _getResourceDirs(Project project) {
  176. SourceSet sourceSet = GradleUtil.getSourceSet(
  177. project, SourceSet.MAIN_SOURCE_SET_NAME);
  178. SourceDirectorySet sourceDirectorySet = sourceSet.getResources();
  179. return sourceDirectorySet.getSrcDirs();
  180. }
  181. }