PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/amps-maven-plugin/src/main/java/com/atlassian/maven/plugins/amps/PluginModuleGenerationMojo.java

https://bitbucket.org/atlassian/amps
Java | 240 lines | 191 code | 37 blank | 12 comment | 7 complexity | 89f9357cf9adb5a136b09a56e905906c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps;
  2. import com.atlassian.maven.plugins.amps.codegen.ConditionFactory;
  3. import com.atlassian.maven.plugins.amps.codegen.ContextProviderFactory;
  4. import com.atlassian.maven.plugins.amps.codegen.PluginModuleSelectionQueryer;
  5. import com.atlassian.maven.plugins.amps.codegen.jira.ActionTypeFactory;
  6. import com.atlassian.maven.plugins.amps.codegen.jira.CustomFieldSearcherFactory;
  7. import com.atlassian.maven.plugins.amps.codegen.jira.CustomFieldTypeFactory;
  8. import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompter;
  9. import com.atlassian.maven.plugins.amps.codegen.prompter.PluginModulePrompterFactory;
  10. import com.atlassian.plugins.codegen.MavenProjectRewriter;
  11. import com.atlassian.plugins.codegen.PluginProjectChangeset;
  12. import com.atlassian.plugins.codegen.PluginXmlRewriter;
  13. import com.atlassian.plugins.codegen.ProjectFilesRewriter;
  14. import com.atlassian.plugins.codegen.modules.PluginModuleCreator;
  15. import com.atlassian.plugins.codegen.modules.PluginModuleCreatorFactory;
  16. import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
  17. import com.atlassian.plugins.codegen.modules.PluginModuleProperties;
  18. import org.apache.maven.artifact.DependencyResolutionRequiredException;
  19. import org.apache.maven.model.FileSet;
  20. import org.apache.maven.plugin.MojoExecutionException;
  21. import org.apache.maven.plugin.MojoFailureException;
  22. import org.apache.maven.plugin.logging.Log;
  23. import org.apache.maven.plugins.annotations.Component;
  24. import org.apache.maven.plugins.annotations.Mojo;
  25. import org.apache.maven.plugins.annotations.ResolutionScope;
  26. import org.apache.maven.project.MavenProject;
  27. import java.io.File;
  28. import java.util.List;
  29. import java.util.function.BinaryOperator;
  30. import static com.atlassian.maven.plugins.amps.analytics.event.impl.AnalyticsEventFactory.createPluginModule;
  31. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.BAMBOO;
  32. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.CONFLUENCE;
  33. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.CROWD;
  34. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.FECRU;
  35. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.JIRA;
  36. import static org.apache.commons.lang3.StringUtils.endsWith;
  37. /**
  38. * Adds a new plugin module to an existing plugin project.
  39. *
  40. * @since 3.6
  41. */
  42. @Mojo(name = "create-plugin-module", requiresDependencyResolution = ResolutionScope.COMPILE)
  43. public class PluginModuleGenerationMojo extends AbstractProductAwareMojo {
  44. private static final BinaryOperator<String> FIND_LAST = (first, second) -> second;
  45. @Component
  46. private PluginModuleSelectionQueryer pluginModuleSelectionQueryer;
  47. @Component
  48. private PluginModulePrompterFactory pluginModulePrompterFactory;
  49. @Component
  50. private PluginModuleCreatorFactory pluginModuleCreatorFactory;
  51. @Override
  52. public void execute() throws MojoExecutionException, MojoFailureException {
  53. trackFirstRunIfNeeded();
  54. Log log = getLog();
  55. //can't figure out how to get plexus to fire a method after injection, so doing it here
  56. pluginModulePrompterFactory.setLog(log);
  57. try {
  58. pluginModulePrompterFactory.scanForPrompters();
  59. } catch (Exception e) {
  60. String message = "Error initializing Plugin Module Prompters";
  61. log.error(message);
  62. throw new MojoExecutionException(message);
  63. }
  64. String productId = getProductId();
  65. MavenProject project = getMavenContext().getProject();
  66. File javaDir = getJavaSourceRoot(project);
  67. File testDir = getJavaTestRoot(project);
  68. File resourcesDir = getResourcesRoot(project);
  69. initHelperFactories(productId, project);
  70. PluginModuleLocation moduleLocation = new PluginModuleLocation.Builder(javaDir)
  71. .resourcesDirectory(resourcesDir)
  72. .testDirectory(testDir)
  73. .templateDirectory(new File(resourcesDir, "templates"))
  74. .groupAndArtifactId(project.getGroupId(), project.getArtifactId())
  75. .build();
  76. if (!moduleLocation.getPluginXml()
  77. .exists()) {
  78. String message = "Couldn't find the atlassian-plugin.xml, please run this goal in an atlassian plugin project root.";
  79. log.error(message);
  80. throw new MojoExecutionException(message);
  81. }
  82. runGeneration(productId, project, moduleLocation);
  83. }
  84. private void runGeneration(String productId, MavenProject project, PluginModuleLocation moduleLocation) throws MojoExecutionException {
  85. try {
  86. final PluginModuleCreator creator = pluginModuleSelectionQueryer.selectModule(
  87. pluginModuleCreatorFactory.getModuleCreatorsForProduct(productId));
  88. sendAnalyticsEvent(createPluginModule(getPluginInformation().getId(), creator.getModuleName()));
  89. final PluginModulePrompter<?> modulePrompter =
  90. pluginModulePrompterFactory.getPrompterForCreatorClass(creator.getClass());
  91. if (modulePrompter == null) {
  92. final String message = "Couldn't find an input prompter for: " + creator.getClass().getName();
  93. getLog().error(message);
  94. throw new MojoExecutionException(message);
  95. }
  96. modulePrompter.setDefaultBasePackage(project.getGroupId());
  97. modulePrompter.setPluginKey(project.getGroupId() + "." + project.getArtifactId());
  98. final PluginModuleProperties moduleProps = modulePrompter.getModulePropertiesFromInput(moduleLocation);
  99. moduleProps.setProductId(getGadgetCompatibleProductId(productId));
  100. final PluginProjectChangeset changeset = creator.createModule(moduleProps);
  101. getLog().info("Adding the following items to the project:");
  102. for (String desc : changeset.getChangeDescriptionsOrSummaries()) {
  103. getLog().info(" " + desc);
  104. }
  105. editPomIfNeeded(project, changeset);
  106. // apply changes to project files
  107. new ProjectFilesRewriter(moduleLocation).applyChanges(changeset);
  108. new PluginXmlRewriter(moduleLocation).applyChanges(changeset);
  109. if (pluginModuleSelectionQueryer.addAnotherModule()) {
  110. runGeneration(productId, project, moduleLocation);
  111. }
  112. } catch (Exception e) {
  113. throw new MojoExecutionException("Error creating plugin module", e);
  114. }
  115. }
  116. private void editPomIfNeeded(final MavenProject project, final PluginProjectChangeset changeset) {
  117. try {
  118. new MavenProjectRewriter(project.getFile()).applyChanges(changeset);
  119. } catch (final Exception e) {
  120. getLog().error("Unable to apply changes to POM: " + e);
  121. }
  122. }
  123. private String getGadgetCompatibleProductId(final String pid) {
  124. switch (pid) {
  125. case BAMBOO:
  126. return "Bamboo";
  127. case CONFLUENCE:
  128. return "Confluence";
  129. case CROWD:
  130. return "Crowd";
  131. case FECRU:
  132. return "FishEye";
  133. case JIRA:
  134. return "JIRA";
  135. default:
  136. return "Other";
  137. }
  138. }
  139. private File getJavaSourceRoot(MavenProject project) {
  140. return new File(project.getModel()
  141. .getBuild()
  142. .getSourceDirectory());
  143. }
  144. private File getJavaTestRoot(MavenProject project) {
  145. return new File(project.getModel()
  146. .getBuild()
  147. .getTestSourceDirectory());
  148. }
  149. private File getResourcesRoot(final MavenProject project) {
  150. final String pathToCheck = "src" + File.separator + "main" + File.separator + "resources";
  151. return project.getModel().getBuild().getResources().stream()
  152. .map(FileSet::getDirectory)
  153. .filter(dir -> endsWith(dir, pathToCheck))
  154. .reduce(FIND_LAST)
  155. .map(File::new)
  156. .orElse(null);
  157. }
  158. private void initHelperFactories(String productId, MavenProject project) throws MojoExecutionException {
  159. List<String> pluginClasspath;
  160. try {
  161. pluginClasspath = project.getCompileClasspathElements();
  162. } catch (DependencyResolutionRequiredException e) {
  163. throw new MojoExecutionException("Dependencies MUST be resolved", e);
  164. }
  165. try {
  166. ConditionFactory.locateAvailableConditions(productId, pluginClasspath);
  167. } catch (Exception e) {
  168. String message = "Error initializing Plugin Module Conditions";
  169. getLog().error(message);
  170. //keep going, doesn't matter
  171. }
  172. try {
  173. ContextProviderFactory.locateAvailableContextProviders(productId, pluginClasspath);
  174. } catch (Exception e) {
  175. String message = "Error initializing Plugin Module Context Providers";
  176. getLog().error(message);
  177. //keep going, doesn't matter
  178. }
  179. if (JIRA.equals(productId)) {
  180. try {
  181. ActionTypeFactory.locateAvailableActionTypes(pluginClasspath);
  182. } catch (Exception e) {
  183. String message = "Error initializing JIRA Action Types";
  184. getLog().error(message);
  185. //keep going, doesn't matter
  186. }
  187. try {
  188. CustomFieldTypeFactory.locateAvailableCustomFieldTypes(pluginClasspath);
  189. } catch (Exception e) {
  190. String message = "Error initializing JIRA Custom Field Types";
  191. getLog().error(message);
  192. //keep going, doesn't matter
  193. }
  194. try {
  195. CustomFieldSearcherFactory.locateAvailableCustomFieldSearchers(pluginClasspath);
  196. } catch (Exception e) {
  197. String message = "Error initializing JIRA Custom Field Searchers";
  198. getLog().error(message);
  199. //keep going, doesn't matter
  200. }
  201. }
  202. }
  203. }