PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/maven/plugins/jgitflow/rewrite/ArtifactReleaseVersionChange.java

https://bitbucket.org/ronsmits/maven-jgitflow-plugin
Java | 247 lines | 206 code | 38 blank | 3 comment | 38 complexity | 19af012ae278c1ae6f4e1f292f8961aa MD5 | raw file
  1. package com.atlassian.maven.plugins.jgitflow.rewrite;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import com.atlassian.maven.plugins.jgitflow.exception.ProjectRewriteException;
  6. import org.apache.maven.artifact.Artifact;
  7. import org.apache.maven.artifact.ArtifactUtils;
  8. import org.apache.maven.model.Model;
  9. import org.apache.maven.project.MavenProject;
  10. import org.apache.maven.shared.release.ReleaseExecutionException;
  11. import org.apache.maven.shared.release.util.ReleaseUtil;
  12. import org.jdom2.Element;
  13. import org.jdom2.Namespace;
  14. import static com.atlassian.maven.plugins.jgitflow.rewrite.ProjectChangeUtils.getElementListOrEmpty;
  15. import static com.atlassian.maven.plugins.jgitflow.rewrite.ProjectChangeUtils.getNamespaceOrNull;
  16. /**
  17. * @since version
  18. */
  19. public class ArtifactReleaseVersionChange implements ProjectChange
  20. {
  21. private static final String LF = System.getProperty("line.separator") + " - ";
  22. private final Map<String, String> originalVersions;
  23. private final Map<String, String> releaseVersions;
  24. private final boolean updateDependencies;
  25. private StringBuilder workLog;
  26. private ArtifactReleaseVersionChange(Map<String, String> originalVersions, Map<String, String> releaseVersions, boolean updateDependencies)
  27. {
  28. this.originalVersions = originalVersions;
  29. this.releaseVersions = releaseVersions;
  30. this.updateDependencies = updateDependencies;
  31. this.workLog = new StringBuilder("[Update Artifact Versions]");
  32. }
  33. public static ArtifactReleaseVersionChange artifactReleaseVersionChange(Map<String, String> originalVersions, Map<String, String> releaseVersions, boolean updateDependencies)
  34. {
  35. return new ArtifactReleaseVersionChange(originalVersions,releaseVersions,updateDependencies);
  36. }
  37. @Override
  38. public boolean applyChange(MavenProject project, Element root) throws ProjectRewriteException
  39. {
  40. boolean modified = false;
  41. Namespace ns = getNamespaceOrNull(root);
  42. Element properties = root.getChild("properties", ns);
  43. List<Element> artifactContainers = new ArrayList<Element>();
  44. artifactContainers.add(root);
  45. artifactContainers.addAll(getElementListOrEmpty(root, "profiles/profile",ns));
  46. for(Element artifactContainer : artifactContainers)
  47. {
  48. modified |= rewriteDependencies(artifactContainer, project, properties, ns);
  49. modified |= rewriteDependencyManagement(artifactContainer, project, properties, ns);
  50. modified |= rewriteBuildExtensions(artifactContainer, project, properties, ns);
  51. modified |= rewritePluginElements(artifactContainer, project, properties, ns);
  52. modified |= rewriteReportingPlugins(artifactContainer, project, properties, ns);
  53. }
  54. return modified; //To change body of implemented methods use File | Settings | File Templates.
  55. }
  56. private boolean rewriteDependencies(Element artifactContainer, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  57. {
  58. List<Element> artifacts = getElementListOrEmpty(artifactContainer, "dependencies/dependency",ns);
  59. return rewriteArtifactVersions(artifacts, project, properties, ns);
  60. }
  61. private boolean rewriteDependencyManagement(Element artifactContainer, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  62. {
  63. List<Element> artifacts = getElementListOrEmpty(artifactContainer, "dependencyManagement/dependencies/dependency",ns);
  64. return rewriteArtifactVersions(artifacts, project, properties, ns);
  65. }
  66. private boolean rewriteBuildExtensions(Element artifactContainer, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  67. {
  68. List<Element> artifacts = getElementListOrEmpty(artifactContainer, "build/extensions/extension",ns);
  69. return rewriteArtifactVersions(artifacts, project, properties, ns);
  70. }
  71. private boolean rewritePluginElements(Element artifactContainer, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  72. {
  73. boolean modified = false;
  74. List<Element> pluginElements = new ArrayList<Element>();
  75. pluginElements.addAll(getElementListOrEmpty(artifactContainer, "build/plugins/plugin",ns));
  76. pluginElements.addAll(getElementListOrEmpty(artifactContainer, "build/pluginManagement/plugins/plugin",ns));
  77. modified |= rewriteArtifactVersions(pluginElements, project, properties, ns);
  78. for(Element pluginElement : pluginElements)
  79. {
  80. List<Element> artifacts = getElementListOrEmpty(pluginElement, "dependencies/dependency",ns);
  81. modified |= rewriteArtifactVersions(artifacts, project, properties,ns);
  82. }
  83. return modified;
  84. }
  85. private boolean rewriteReportingPlugins(Element artifactContainer, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  86. {
  87. List<Element> artifacts = getElementListOrEmpty(artifactContainer, "reporting/plugins/plugin",ns);
  88. return rewriteArtifactVersions(artifacts, project, properties, ns);
  89. }
  90. private boolean rewriteArtifactVersions(List<Element> artifacts, MavenProject project, Element properties, Namespace ns) throws ProjectRewriteException
  91. {
  92. boolean modified = false;
  93. Model projectModel = project.getModel();
  94. if(null == artifacts || artifacts.isEmpty())
  95. {
  96. return false;
  97. }
  98. String projectId = ArtifactUtils.versionlessKey(project.getGroupId(),project.getArtifactId());
  99. for(Element artifact : artifacts)
  100. {
  101. Element versionElement = artifact.getChild("version", ns);
  102. if(null == versionElement)
  103. {
  104. continue;
  105. }
  106. String rawVersion = versionElement.getTextTrim();
  107. Element groupIdElement = artifact.getChild("groupId", ns);
  108. if(null == groupIdElement)
  109. {
  110. if("plugin".equals(artifact.getName()))
  111. {
  112. groupIdElement = new Element("groupId");
  113. groupIdElement.setText("org.apache.maven.plugins");
  114. }
  115. else
  116. {
  117. continue;
  118. }
  119. }
  120. String groupId = null;
  121. String artifactId = null;
  122. try
  123. {
  124. groupId = ReleaseUtil.interpolate(groupIdElement.getTextTrim(),projectModel);
  125. Element artifactIdElement = artifact.getChild("artifactId", ns);
  126. if(null == artifactIdElement)
  127. {
  128. continue;
  129. }
  130. artifactId = ReleaseUtil.interpolate(artifactIdElement.getTextTrim(),projectModel);
  131. }
  132. catch (ReleaseExecutionException e)
  133. {
  134. throw new ProjectRewriteException("error interpolating pom variable: " + e.getMessage(),e);
  135. }
  136. String artifactKey = ArtifactUtils.versionlessKey(groupId,artifactId);
  137. String mappedVersion = releaseVersions.get(artifactKey);
  138. String originalVersion = originalVersions.get(artifactKey);
  139. if(null != mappedVersion
  140. && mappedVersion.endsWith(Artifact.SNAPSHOT_VERSION)
  141. && !rawVersion.endsWith(Artifact.SNAPSHOT_VERSION)
  142. && !updateDependencies)
  143. {
  144. continue;
  145. }
  146. if(null != mappedVersion)
  147. {
  148. if(rawVersion.equals(originalVersion))
  149. {
  150. workLog.append(LF).append("updating ").append(artifactId).append(" to ").append(mappedVersion);
  151. versionElement.setText(mappedVersion);
  152. modified = true;
  153. }
  154. else if(rawVersion.matches("\\$\\{.+\\}"))
  155. {
  156. String propName = rawVersion.substring(2,rawVersion.length() - 1);
  157. if(propName.startsWith("project.")
  158. || propName.startsWith("pom.")
  159. || "version".equals(propName)
  160. )
  161. {
  162. if(!mappedVersion.equals(releaseVersions.get(projectId)))
  163. {
  164. workLog.append(LF).append("updating ").append(artifactId).append(" to ").append(mappedVersion);
  165. versionElement.setText(mappedVersion);
  166. modified = true;
  167. }
  168. }
  169. else if(null != properties)
  170. {
  171. Element prop = properties.getChild(propName, ns);
  172. if(null != prop)
  173. {
  174. String propValue = prop.getTextTrim();
  175. if(propValue.equals(originalVersion))
  176. {
  177. workLog.append(LF).append("updating ").append(rawVersion).append(" to ").append(mappedVersion);
  178. prop.setText(mappedVersion);
  179. modified = true;
  180. }
  181. else if(!mappedVersion.equals(rawVersion))
  182. {
  183. if(!mappedVersion.matches("\\$\\{project.+\\}")
  184. && !mappedVersion.matches("\\$\\{pom.+\\}")
  185. && !"${version}".equals(mappedVersion))
  186. {
  187. throw new ProjectRewriteException("The artifact (" + artifactKey + ") requires a "
  188. + "different version (" + mappedVersion + ") than what is found ("
  189. + propValue + ") for the expression (" + propName + ") in the "
  190. + "project (" + projectId + ").");
  191. }
  192. }
  193. }
  194. else
  195. {
  196. throw new ProjectRewriteException("Error updating version '" + rawVersion + "' for artifact " + artifactKey);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. return modified;
  203. }
  204. @Override
  205. public String toString()
  206. {
  207. return workLog.toString();
  208. }
  209. }