View Javadoc

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  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          if ( project.getScm() != null )
59          {
60              Element scmRoot = rootElement.getChild( "scm", namespace );
61              if ( scmRoot != null )
62              {
63                  Scm scm = buildScm( project );
64                  releaseDescriptor.mapOriginalScmInfo( projectId, scm );
65  
66                  try
67                  {
68                      translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, commonBasedir );
69                  }
70                  catch ( IOException e )
71                  {
72                      throw new ReleaseExecutionException( e.getMessage(), e );
73                  }
74              }
75              else
76              {
77                  releaseDescriptor.mapOriginalScmInfo( projectId, null );
78  
79                  MavenProject parent = project.getParent();
80                  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                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
85                      if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
86                      {
87                          // we need to add it, since it has changed from the inherited value
88                          scmRoot = new Element( "scm" );
89                          scmRoot.addContent( "\n  " );
90  
91                          try
92                          {
93                              if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
94                                                 commonBasedir ) )
95                              {
96                                  rootElement.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
97                              }
98                          }
99                          catch ( IOException e )
100                         {
101                             throw new ReleaseExecutionException( e.getMessage(), e );
102                         }
103                     }
104                 }
105             }
106         }
107     }
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         ScmTranslator translator = scmTranslators.get( scmRepository.getProvider() );
115         boolean result = false;
116         if ( translator != null )
117         {
118             Scm scm = project.getScm();
119             String branchName = releaseDescriptor.getScmReleaseLabel();
120             String branchBase = releaseDescriptor.getScmBranchBase();
121 
122             // TODO: svn utils should take care of prepending this
123             if ( branchBase != null )
124             {
125                 branchBase = "scm:svn:" + branchBase;
126             }
127 
128             String workingDirectory =
129                 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
130                                 : project.getBasedir().getAbsolutePath();
131 
132             int count =
133                 ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
134             if ( scm.getConnection() != null )
135             {
136                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
137 
138                 String subDirectoryBranch = scm.getConnection().substring( rootUrl.length() );
139                 if ( !subDirectoryBranch.startsWith( "/" ) )
140                 {
141                     subDirectoryBranch = "/" + subDirectoryBranch;
142                 }
143 
144                 String value =
145                     translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch, branchBase );
146                 if ( !value.equals( scm.getConnection() ) )
147                 {
148                     rewriteElement( "connection", value, scmRoot, namespace );
149                     result = true;
150                 }
151             }
152 
153             if ( scm.getDeveloperConnection() != null )
154             {
155                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
156 
157                 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
158                 if ( !subDirectoryBranch.startsWith( "/" ) )
159                 {
160                     subDirectoryBranch = "/" + subDirectoryBranch;
161                 }
162 
163                 String value =
164                     translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
165                                                    branchBase );
166                 if ( !value.equals( scm.getDeveloperConnection() ) )
167                 {
168                     rewriteElement( "developerConnection", value, scmRoot, namespace );
169                     result = true;
170                 }
171             }
172 
173             if ( scm.getUrl() != null )
174             {
175                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
176 
177                 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
178                 if ( !subDirectoryBranch.startsWith( "/" ) )
179                 {
180                     subDirectoryBranch = "/" + subDirectoryBranch;
181                 }
182 
183                 // use original branch base without protocol
184                 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
185                                                               releaseDescriptor.getScmBranchBase() );
186                 if ( !value.equals( scm.getUrl() ) )
187                 {
188                     rewriteElement( "url", value, scmRoot, namespace );
189                     result = true;
190                 }
191             }
192 
193             if ( branchName != null )
194             {
195                 String value = translator.resolveTag( branchName );
196                 if ( value != null && !value.equals( scm.getTag() ) )
197                 {
198                     rewriteElement( "tag", value, scmRoot, namespace );
199                     result = true;
200                 }
201             }
202         }
203         else
204         {
205             String message = "No SCM translator found - skipping rewrite";
206 
207             relResult.appendDebug( message );
208 
209             getLogger().debug( message );
210         }
211         return result;
212     }
213 
214     protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
215                                                          List<MavenProject> reactorProjects, boolean simulate )
216     {
217         return releaseDescriptor.getOriginalVersions( reactorProjects );
218     }
219 
220     @SuppressWarnings( "unchecked" )
221     protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
222     {
223         return releaseDescriptor.getReleaseVersions();
224     }
225 
226     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
227                                                  Map<String, Map<String, String>> resolvedSnapshotsMap )
228     {
229         Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
230 
231         if ( versionsMap != null )
232         {
233             return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
234         }
235         else
236         {
237             return null;
238         }
239     }
240 }