Coverage Report - org.apache.maven.shared.release.phase.RewritePomsForBranchPhase
 
Classes in this File Line Coverage Branch Coverage Complexity
RewritePomsForBranchPhase
1%
1/77
0%
0/42
6,2
 
 1  
 package org.apache.maven.shared.release.phase;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.artifact.ArtifactUtils;
 27  
 import org.apache.maven.model.Scm;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.apache.maven.scm.repository.ScmRepository;
 30  
 import org.apache.maven.shared.release.ReleaseExecutionException;
 31  
 import org.apache.maven.shared.release.ReleaseResult;
 32  
 import org.apache.maven.shared.release.config.ReleaseDescriptor;
 33  
 import org.apache.maven.shared.release.scm.ScmTranslator;
 34  
 import org.apache.maven.shared.release.util.ReleaseUtil;
 35  
 import org.jdom.Element;
 36  
 import org.jdom.Namespace;
 37  
 
 38  
 /**
 39  
  * Rewrite POMs for branch.
 40  
  *
 41  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 42  
  * @version $Id: RewritePomsForBranchPhase.java 1339787 2012-05-17 18:39:18Z rfscholte $
 43  
  */
 44  58
 public class RewritePomsForBranchPhase
 45  
     extends AbstractRewritePomsPhase
 46  
 {
 47  
     /**
 48  
      * SCM URL translators mapped by provider name.
 49  
      */
 50  
     private Map<String, ScmTranslator> scmTranslators;
 51  
 
 52  
     protected void transformScm( MavenProject project, Element rootElement, Namespace namespace,
 53  
                                  ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository,
 54  
                                  ReleaseResult result, String commonBasedir ) 
 55  
     throws ReleaseExecutionException
 56  
     {
 57  
         // If SCM is null in original model, it is inherited, no mods needed
 58  0
         if ( project.getScm() != null )
 59  
         {
 60  0
             Element scmRoot = rootElement.getChild( "scm", namespace );
 61  0
             if ( scmRoot != null )
 62  
             {
 63  0
                 Scm scm = buildScm( project );
 64  0
                 releaseDescriptor.mapOriginalScmInfo( projectId, scm );
 65  
 
 66  
                 try
 67  
                 {
 68  0
                     translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, commonBasedir );
 69  
                 }
 70  0
                 catch ( IOException e )
 71  
                 {
 72  0
                     throw new ReleaseExecutionException( e.getMessage(), e );
 73  0
                 }
 74  0
             }
 75  
             else
 76  
             {
 77  0
                 releaseDescriptor.mapOriginalScmInfo( projectId, null );
 78  
 
 79  0
                 MavenProject parent = project.getParent();
 80  0
                 if ( parent != null )
 81  
                 {
 82  
                     // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
 83  
                     // the release process and so has not been modified, so the values will not be correct on the tag),
 84  0
                     String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
 85  0
                     if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
 86  
                     {
 87  
                         // we need to add it, since it has changed from the inherited value
 88  0
                         scmRoot = new Element( "scm" );
 89  0
                         scmRoot.addContent( "\n  " );
 90  
 
 91  
                         try
 92  
                         {
 93  0
                             if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
 94  
                                                commonBasedir ) )
 95  
                             {
 96  0
                                 rootElement.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
 97  
                             }
 98  
                         }
 99  0
                         catch ( IOException e )
 100  
                         {
 101  0
                             throw new ReleaseExecutionException( e.getMessage(), e );
 102  0
                         }
 103  
                     }
 104  
                 }
 105  
             }
 106  
         }
 107  0
     }
 108  
 
 109  
     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
 110  
                                   Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
 111  
                                   String commonBasedir ) 
 112  
     throws IOException
 113  
     {
 114  0
         ScmTranslator translator = scmTranslators.get( scmRepository.getProvider() );
 115  0
         boolean result = false;
 116  0
         if ( translator != null )
 117  
         {
 118  0
             Scm scm = project.getScm();
 119  0
             String branchName = releaseDescriptor.getScmReleaseLabel();
 120  0
             String branchBase = releaseDescriptor.getScmBranchBase();
 121  
 
 122  
             // TODO: svn utils should take care of prepending this
 123  0
             if ( branchBase != null )
 124  
             {
 125  0
                 branchBase = "scm:svn:" + branchBase;
 126  
             }
 127  
 
 128  0
             String workingDirectory =
 129  
                 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
 130  
                                 : project.getBasedir().getAbsolutePath();
 131  
 
 132  0
             int count =
 133  
                 ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
 134  0
             if ( scm.getConnection() != null )
 135  
             {
 136  0
                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
 137  
 
 138  0
                 String subDirectoryBranch = scm.getConnection().substring( rootUrl.length() );
 139  0
                 if ( !subDirectoryBranch.startsWith( "/" ) )
 140  
                 {
 141  0
                     subDirectoryBranch = "/" + subDirectoryBranch;
 142  
                 }
 143  
 
 144  0
                 String value =
 145  
                     translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch, branchBase );
 146  0
                 if ( !value.equals( scm.getConnection() ) )
 147  
                 {
 148  0
                     rewriteElement( "connection", value, scmRoot, namespace );
 149  0
                     result = true;
 150  
                 }
 151  
             }
 152  
 
 153  0
             if ( scm.getDeveloperConnection() != null )
 154  
             {
 155  0
                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
 156  
 
 157  0
                 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
 158  0
                 if ( !subDirectoryBranch.startsWith( "/" ) )
 159  
                 {
 160  0
                     subDirectoryBranch = "/" + subDirectoryBranch;
 161  
                 }
 162  
 
 163  0
                 String value =
 164  
                     translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
 165  
                                                    branchBase );
 166  0
                 if ( !value.equals( scm.getDeveloperConnection() ) )
 167  
                 {
 168  0
                     rewriteElement( "developerConnection", value, scmRoot, namespace );
 169  0
                     result = true;
 170  
                 }
 171  
             }
 172  
 
 173  0
             if ( scm.getUrl() != null )
 174  
             {
 175  0
                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
 176  
 
 177  0
                 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
 178  0
                 if ( !subDirectoryBranch.startsWith( "/" ) )
 179  
                 {
 180  0
                     subDirectoryBranch = "/" + subDirectoryBranch;
 181  
                 }
 182  
 
 183  
                 // use original branch base without protocol
 184  0
                 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
 185  
                                                               releaseDescriptor.getScmBranchBase() );
 186  0
                 if ( !value.equals( scm.getUrl() ) )
 187  
                 {
 188  0
                     rewriteElement( "url", value, scmRoot, namespace );
 189  0
                     result = true;
 190  
                 }
 191  
             }
 192  
 
 193  0
             if ( branchName != null )
 194  
             {
 195  0
                 String value = translator.resolveTag( branchName );
 196  0
                 if ( value != null && !value.equals( scm.getTag() ) )
 197  
                 {
 198  0
                     rewriteElement( "tag", value, scmRoot, namespace );
 199  0
                     result = true;
 200  
                 }
 201  
             }
 202  0
         }
 203  
         else
 204  
         {
 205  0
             String message = "No SCM translator found - skipping rewrite";
 206  
 
 207  0
             relResult.appendDebug( message );
 208  
 
 209  0
             getLogger().debug( message );
 210  
         }
 211  0
         return result;
 212  
     }
 213  
 
 214  
     protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
 215  
                                                          List<MavenProject> reactorProjects, boolean simulate )
 216  
     {
 217  0
         return releaseDescriptor.getOriginalVersions( reactorProjects );
 218  
     }
 219  
 
 220  
     @SuppressWarnings( "unchecked" )
 221  
     protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
 222  
     {
 223  0
         return releaseDescriptor.getReleaseVersions();
 224  
     }
 225  
 
 226  
     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
 227  
                                                  Map<String, Map<String, String>> resolvedSnapshotsMap )
 228  
     {
 229  0
         Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
 230  
 
 231  0
         if ( versionsMap != null )
 232  
         {
 233  0
             return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
 234  
         }
 235  
         else
 236  
         {
 237  0
             return null;
 238  
         }
 239  
     }
 240  
 }