/maven-release-manager/src/main/java/org/apache/maven/shared/release/phase/RemoveReleasePomsPhase.java

https://gitlab.com/kidaa/maven-release · Java · 154 lines · 111 code · 18 blank · 25 comment · 7 complexity · 072b18540f41f30ea0be58ff0f59e7fa MD5 · raw file

  1. package org.apache.maven.shared.release.phase;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.apache.maven.project.MavenProject;
  24. import org.apache.maven.scm.ScmException;
  25. import org.apache.maven.scm.ScmFileSet;
  26. import org.apache.maven.scm.command.remove.RemoveScmResult;
  27. import org.apache.maven.scm.provider.ScmProvider;
  28. import org.apache.maven.scm.repository.ScmRepository;
  29. import org.apache.maven.shared.release.ReleaseExecutionException;
  30. import org.apache.maven.shared.release.ReleaseFailureException;
  31. import org.apache.maven.shared.release.ReleaseResult;
  32. import org.apache.maven.shared.release.config.ReleaseDescriptor;
  33. import org.apache.maven.shared.release.env.ReleaseEnvironment;
  34. import org.apache.maven.shared.release.scm.ReleaseScmCommandException;
  35. import org.apache.maven.shared.release.util.ReleaseUtil;
  36. /**
  37. * Remove release POMs.
  38. *
  39. * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  40. * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
  41. * @plexus.component role="org.apache.maven.shared.release.phase.ReleasePhase" role-hint="remove-release-poms"
  42. */
  43. public class RemoveReleasePomsPhase
  44. extends AbstractReleasePomsPhase
  45. {
  46. public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
  47. List<MavenProject> reactorProjects )
  48. throws ReleaseFailureException, ReleaseExecutionException
  49. {
  50. return execute( releaseDescriptor, releaseEnvironment, reactorProjects, false );
  51. }
  52. public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
  53. List<MavenProject> reactorProjects )
  54. throws ReleaseFailureException, ReleaseExecutionException
  55. {
  56. return execute( releaseDescriptor, releaseEnvironment, reactorProjects, true );
  57. }
  58. private ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
  59. List<MavenProject> reactorProjects, boolean simulate )
  60. throws ReleaseFailureException, ReleaseExecutionException
  61. {
  62. ReleaseResult result = new ReleaseResult();
  63. if ( releaseDescriptor.isGenerateReleasePoms() )
  64. {
  65. removeReleasePoms( releaseDescriptor, releaseEnvironment, simulate, result, reactorProjects );
  66. }
  67. else
  68. {
  69. logInfo( result, "Not removing release POMs" );
  70. }
  71. result.setResultCode( ReleaseResult.SUCCESS );
  72. return result;
  73. }
  74. private void removeReleasePoms( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
  75. boolean simulate, ReleaseResult result, List<MavenProject> projects )
  76. throws ReleaseFailureException, ReleaseExecutionException
  77. {
  78. List<File> releasePoms = new ArrayList<File>();
  79. for ( MavenProject project : projects )
  80. {
  81. logInfo( result, "Removing release POM for '" + project.getName() + "'..." );
  82. releasePoms.add( ReleaseUtil.getReleasePom( project ) );
  83. }
  84. if ( releaseDescriptor.isSuppressCommitBeforeTagOrBranch() )
  85. {
  86. removeReleasePomsFromFilesystem( simulate, result, releasePoms );
  87. }
  88. else
  89. {
  90. removeReleasePomsFromScm( releaseDescriptor, releaseEnvironment, simulate, result, releasePoms );
  91. }
  92. }
  93. private void removeReleasePomsFromFilesystem( boolean simulate, ReleaseResult result, List<File> releasePoms )
  94. {
  95. if ( simulate )
  96. {
  97. logInfo( result, "Full run would be removing " + releasePoms );
  98. }
  99. else
  100. {
  101. for ( File releasePom : releasePoms )
  102. {
  103. releasePom.delete();
  104. }
  105. }
  106. }
  107. private void removeReleasePomsFromScm( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
  108. boolean simulate, ReleaseResult result, List<File> releasePoms )
  109. throws ReleaseFailureException, ReleaseExecutionException
  110. {
  111. if ( simulate )
  112. {
  113. logInfo( result, "Full run would be removing " + releasePoms );
  114. }
  115. else
  116. {
  117. ScmRepository scmRepository = getScmRepository( releaseDescriptor, releaseEnvironment );
  118. ScmProvider scmProvider = getScmProvider( scmRepository );
  119. ScmFileSet scmFileSet = new ScmFileSet( new File( releaseDescriptor.getWorkingDirectory() ), releasePoms );
  120. try
  121. {
  122. RemoveScmResult scmResult =
  123. scmProvider.remove( scmRepository, scmFileSet, "Removing for next development iteration." );
  124. if ( !scmResult.isSuccess() )
  125. {
  126. throw new ReleaseScmCommandException( "Cannot remove release POMs from SCM", scmResult );
  127. }
  128. }
  129. catch ( ScmException exception )
  130. {
  131. throw new ReleaseExecutionException( "Cannot remove release POMs from SCM: " + exception.getMessage(),
  132. exception );
  133. }
  134. }
  135. }
  136. }