PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/amps
Java | 160 lines | 128 code | 18 blank | 14 comment | 21 complexity | b860e1be999b872732b6c0652de45831 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.product.ProductHandler;
  3. import com.atlassian.maven.plugins.amps.util.AmpsCreatePluginPrompter;
  4. import org.apache.maven.artifact.Artifact;
  5. import org.apache.maven.plugin.MojoExecutionException;
  6. import org.apache.maven.plugins.annotations.Component;
  7. import org.apache.maven.plugins.annotations.Mojo;
  8. import javax.xml.stream.XMLEventReader;
  9. import javax.xml.stream.XMLInputFactory;
  10. import javax.xml.stream.XMLStreamException;
  11. import javax.xml.stream.events.XMLEvent;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.Optional;
  15. import java.util.Properties;
  16. import static com.atlassian.maven.plugins.amps.analytics.event.impl.AnalyticsEventFactory.createPlugin;
  17. import static javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES;
  18. import static javax.xml.stream.XMLStreamConstants.CHARACTERS;
  19. import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
  20. import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
  21. import static org.apache.commons.lang3.StringUtils.isNotBlank;
  22. /**
  23. * Creates a new plugin project.
  24. */
  25. @Mojo(name = "create", requiresProject = false)
  26. public class CreateMojo extends AbstractProductHandlerMojo {
  27. @Component
  28. private AmpsCreatePluginPrompter ampsCreatePluginPrompter;
  29. @Override
  30. protected void doExecute() throws MojoExecutionException {
  31. trackFirstRunIfNeeded();
  32. sendAnalyticsEvent(createPlugin());
  33. // this is the name of a product (refapp, jira, confluence, etc)
  34. String pid = getProductId();
  35. // first try to get manual version
  36. Application a = getManualVersion(pid);
  37. final Properties userProperties =
  38. getMavenContext().getExecutionEnvironment().getMavenSession().getUserProperties();
  39. if (a != null) {
  40. if (isNotBlank(a.latest)) {
  41. getLog().info("using stable product version: " + a.latest);
  42. userProperties.setProperty(pid + "Version", a.latest);
  43. }
  44. if (isNotBlank(a.data)) {
  45. getLog().info("using stable data version: " + a.data);
  46. userProperties.setProperty(pid + "DataVersion", a.data);
  47. }
  48. } else {
  49. // use the old way (grab version from artifact)
  50. final Product product = getProductContexts().get(pid);
  51. getLog().info("determining latest stable product version...");
  52. final String stableVersion = getStableProductVersion(product);
  53. if (isNotBlank(stableVersion)) {
  54. getLog().info("using latest stable product version: " + stableVersion);
  55. userProperties.setProperty(pid + "Version", stableVersion);
  56. }
  57. getLog().info("determining latest stable data version...");
  58. final String stableDataVersion = getStableDataVersion(product);
  59. if (isNotBlank(stableDataVersion)) {
  60. getLog().info("using latest stable data version: " + stableDataVersion);
  61. userProperties.setProperty(pid + "DataVersion", stableDataVersion);
  62. }
  63. }
  64. getMavenGoals().createPlugin(getProductId(), ampsCreatePluginPrompter);
  65. }
  66. /**
  67. * @param searchString The name of a product (refapp, jira, confluence, etc)
  68. * @return An Application object holding latest stable/data version strings
  69. * and some other bits of information.
  70. * Possibly null if we cannot find it in the xml file.
  71. */
  72. private Application getManualVersion(final String searchString) {
  73. final XMLInputFactory f = XMLInputFactory.newInstance();
  74. f.setProperty(IS_SUPPORTING_EXTERNAL_ENTITIES, false);
  75. try (InputStream versionsStream = CreateMojo.class.getClassLoader().getResourceAsStream("application-versions.xml")) {
  76. final XMLEventReader r = f.createXMLEventReader(versionsStream);
  77. Application a = new Application();
  78. String name = "";
  79. while (r.hasNext()) {
  80. final XMLEvent e = r.nextEvent();
  81. switch (e.getEventType()) {
  82. case START_ELEMENT:
  83. name = e.asStartElement().getName().getLocalPart();
  84. if (name.equalsIgnoreCase("application"))
  85. a = new Application();
  86. break;
  87. case END_ELEMENT:
  88. if (e.asEndElement().getName().getLocalPart().equalsIgnoreCase("application") &&
  89. a.name.equals(searchString))
  90. return a;
  91. name = "";
  92. break;
  93. case CHARACTERS:
  94. final String value = e.asCharacters().getData().trim();
  95. if (name.equalsIgnoreCase("name"))
  96. a.name = value;
  97. else if (name.equalsIgnoreCase("latest"))
  98. a.latest = value;
  99. else if (name.equalsIgnoreCase("data"))
  100. a.data = value;
  101. else if (name.equalsIgnoreCase("mvn-artifact"))
  102. a.mvnArtifact = value;
  103. break;
  104. default:
  105. // Ignore
  106. }
  107. }
  108. } catch (final IOException | XMLStreamException e) {
  109. // this can either be from closing the stream of an invalid XML file; either way, we just ignore it
  110. }
  111. return null;
  112. }
  113. protected final String getStableProductVersion(final Product product) throws MojoExecutionException {
  114. final ProductHandler handler = getProductHandler(product.getId());
  115. final ProductArtifact artifact = handler.getArtifact();
  116. if (artifact == null) {
  117. return "";
  118. }
  119. Artifact warArtifact = repositorySystem.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), "LATEST");
  120. return product.getArtifactRetriever().getLatestStableVersion(warArtifact);
  121. }
  122. protected final String getStableDataVersion(final Product product) throws MojoExecutionException {
  123. final ProductHandler handler = getProductHandler(product.getId());
  124. final Optional<ProductArtifact> maybeTestResourcesArtifact = handler.getTestResourcesArtifact();
  125. if (maybeTestResourcesArtifact.isPresent()) { // no lambda because exception thrown
  126. final ProductArtifact testResourcesArtifact = maybeTestResourcesArtifact.get();
  127. final Artifact warArtifact = repositorySystem.createProjectArtifact(
  128. testResourcesArtifact.getGroupId(), testResourcesArtifact.getArtifactId(), "LATEST");
  129. return product.getArtifactRetriever().getLatestStableVersion(warArtifact);
  130. }
  131. return "";
  132. }
  133. private static class Application {
  134. String name = "";
  135. String latest = "";
  136. String data = "";
  137. String mvnArtifact = "";
  138. public String toString() {
  139. return "name=" + name + " latest=" + latest + " data=" + data + " mvn-artifact=" + mvnArtifact;
  140. }
  141. }
  142. }