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 1391602 2012-09-28 19:09:25Z rfscholte $
43   */
44  public class RewritePomsForBranchPhase
45      extends AbstractRewritePomsPhase
46  {
47      protected void transformScm( MavenProject project, Element rootElement, Namespace namespace,
48                                   ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository,
49                                   ReleaseResult result, String commonBasedir ) 
50      throws ReleaseExecutionException
51      {
52          // If SCM is null in original model, it is inherited, no mods needed
53          if ( project.getScm() != null )
54          {
55              Element scmRoot = rootElement.getChild( "scm", namespace );
56              if ( scmRoot != null )
57              {
58                  Scm scm = buildScm( project );
59                  releaseDescriptor.mapOriginalScmInfo( projectId, scm );
60  
61                  try
62                  {
63                      translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, commonBasedir );
64                  }
65                  catch ( IOException e )
66                  {
67                      throw new ReleaseExecutionException( e.getMessage(), e );
68                  }
69              }
70              else
71              {
72                  releaseDescriptor.mapOriginalScmInfo( projectId, null );
73  
74                  MavenProject parent = project.getParent();
75                  if ( parent != null )
76                  {
77                      // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
78                      // the release process and so has not been modified, so the values will not be correct on the tag),
79                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
80                      if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
81                      {
82                          // we need to add it, since it has changed from the inherited value
83                          scmRoot = new Element( "scm" );
84                          scmRoot.addContent( "\n  " );
85  
86                          try
87                          {
88                              if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
89                                                 commonBasedir ) )
90                              {
91                                  rootElement.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
92                              }
93                          }
94                          catch ( IOException e )
95                          {
96                              throw new ReleaseExecutionException( e.getMessage(), e );
97                          }
98                      }
99                  }
100             }
101         }
102     }
103 
104     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
105                                   Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
106                                   String commonBasedir ) 
107     throws IOException
108     {
109         ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
110         boolean result = false;
111         if ( translator != null )
112         {
113             Scm scm = project.getOriginalModel().getScm();
114             if ( scm == null )
115             {
116                 scm = project.getScm();
117             }
118             
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 scmConnectionBranch = branchBase;
145                 if ( scmConnectionBranch != null )
146                 {
147                     String trunkUrl = scm.getDeveloperConnection();
148                     if ( trunkUrl == null )
149                     {
150                         trunkUrl = scm.getConnection();
151                     }
152                     scmConnectionBranch = translateUrlPath( trunkUrl, branchBase, scm.getConnection() );
153                 }
154                 
155                 String value =
156                     translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch, scmConnectionBranch );
157                 if ( !value.equals( scm.getConnection() ) )
158                 {
159                     rewriteElement( "connection", value, scmRoot, namespace );
160                     result = true;
161                 }
162             }
163 
164             if ( scm.getDeveloperConnection() != null )
165             {
166                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
167 
168                 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
169                 if ( !subDirectoryBranch.startsWith( "/" ) )
170                 {
171                     subDirectoryBranch = "/" + subDirectoryBranch;
172                 }
173 
174                 String value =
175                     translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
176                                                    branchBase );
177                 if ( !value.equals( scm.getDeveloperConnection() ) )
178                 {
179                     rewriteElement( "developerConnection", value, scmRoot, namespace );
180                     result = true;
181                 }
182             }
183 
184             if ( scm.getUrl() != null )
185             {
186                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
187 
188                 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
189                 if ( !subDirectoryBranch.startsWith( "/" ) )
190                 {
191                     subDirectoryBranch = "/" + subDirectoryBranch;
192                 }
193                 
194                 String tagScmUrl = branchBase;
195                 if ( tagScmUrl != null )
196                 {
197                     String trunkUrl = scm.getDeveloperConnection();
198                     if ( trunkUrl == null )
199                     {
200                         trunkUrl = scm.getConnection();
201                     }
202                     tagScmUrl = translateUrlPath( trunkUrl, branchBase, scm.getUrl() );
203                 }
204 
205                 // use original branch base without protocol
206                 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
207                                                               tagScmUrl );
208                 if ( !value.equals( scm.getUrl() ) )
209                 {
210                     rewriteElement( "url", value, scmRoot, namespace );
211                     result = true;
212                 }
213             }
214 
215             if ( branchName != null )
216             {
217                 String value = translator.resolveTag( branchName );
218                 if ( value != null && !value.equals( scm.getTag() ) )
219                 {
220                     rewriteElement( "tag", value, scmRoot, namespace );
221                     result = true;
222                 }
223             }
224         }
225         else
226         {
227             String message = "No SCM translator found - skipping rewrite";
228 
229             relResult.appendDebug( message );
230 
231             getLogger().debug( message );
232         }
233         return result;
234     }
235 
236     protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
237                                                          List<MavenProject> reactorProjects, boolean simulate )
238     {
239         return releaseDescriptor.getOriginalVersions( reactorProjects );
240     }
241 
242     @SuppressWarnings( "unchecked" )
243     @Override
244     protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
245     {
246         return releaseDescriptor.getReleaseVersions();
247     }
248 
249     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
250                                                  Map<String, Map<String, String>> resolvedSnapshotsMap )
251     {
252         Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
253 
254         if ( versionsMap != null )
255         {
256             return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
257         }
258         else
259         {
260             return null;
261         }
262     }
263 }