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 1643023 2014-12-02 23:42:32Z hboutemy $
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,
64                                    commonBasedir );
65                  }
66                  catch ( IOException e )
67                  {
68                      throw new ReleaseExecutionException( e.getMessage(), e );
69                  }
70              }
71              else
72              {
73                  releaseDescriptor.mapOriginalScmInfo( projectId, null );
74  
75                  MavenProject parent = project.getParent();
76                  if ( parent != null )
77                  {
78                      // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
79                      // the release process and so has not been modified, so the values will not be correct on the tag),
80                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
81                      if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
82                      {
83                          // we need to add it, since it has changed from the inherited value
84                          scmRoot = new Element( "scm" );
85                          scmRoot.addContent( "\n  " );
86  
87                          try
88                          {
89                              if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
90                                                 commonBasedir ) )
91                              {
92                                  rootElement.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
93                              }
94                          }
95                          catch ( IOException e )
96                          {
97                              throw new ReleaseExecutionException( e.getMessage(), e );
98                          }
99                      }
100                 }
101             }
102         }
103     }
104 
105     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
106                                   Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
107                                   String commonBasedir ) 
108     throws IOException
109     {
110         ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
111         boolean result = false;
112         if ( translator != null )
113         {
114             Scm scm = project.getOriginalModel().getScm();
115             if ( scm == null )
116             {
117                 scm = project.getScm();
118             }
119             
120             String branchName = releaseDescriptor.getScmReleaseLabel();
121             String branchBase = releaseDescriptor.getScmBranchBase();
122 
123             // TODO: svn utils should take care of prepending this
124             if ( branchBase != null )
125             {
126                 branchBase = "scm:svn:" + branchBase;
127             }
128 
129             String workingDirectory =
130                 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
131                                 : project.getBasedir().getAbsolutePath();
132 
133             int count =
134                 ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
135             if ( scm.getConnection() != null )
136             {
137                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
138 
139                 String subDirectoryBranch = scm.getConnection().substring( rootUrl.length() );
140                 if ( !subDirectoryBranch.startsWith( "/" ) )
141                 {
142                     subDirectoryBranch = "/" + subDirectoryBranch;
143                 }
144 
145                 String scmConnectionBranch = branchBase;
146                 if ( scmConnectionBranch != null )
147                 {
148                     String trunkUrl = scm.getDeveloperConnection();
149                     if ( trunkUrl == null )
150                     {
151                         trunkUrl = scm.getConnection();
152                     }
153                     scmConnectionBranch = translateUrlPath( trunkUrl, branchBase, scm.getConnection() );
154                 }
155                 
156                 String value =
157                     translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch,
158                                                    scmConnectionBranch );
159                 if ( !value.equals( scm.getConnection() ) )
160                 {
161                     rewriteElement( "connection", value, scmRoot, namespace );
162                     result = true;
163                 }
164             }
165 
166             if ( scm.getDeveloperConnection() != null )
167             {
168                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
169 
170                 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
171                 if ( !subDirectoryBranch.startsWith( "/" ) )
172                 {
173                     subDirectoryBranch = "/" + subDirectoryBranch;
174                 }
175 
176                 String value =
177                     translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
178                                                    branchBase );
179                 if ( !value.equals( scm.getDeveloperConnection() ) )
180                 {
181                     rewriteElement( "developerConnection", value, scmRoot, namespace );
182                     result = true;
183                 }
184             }
185 
186             if ( scm.getUrl() != null )
187             {
188                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
189 
190                 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
191                 if ( !subDirectoryBranch.startsWith( "/" ) )
192                 {
193                     subDirectoryBranch = "/" + subDirectoryBranch;
194                 }
195                 
196                 String tagScmUrl = branchBase;
197                 if ( tagScmUrl != null )
198                 {
199                     String trunkUrl = scm.getDeveloperConnection();
200                     if ( trunkUrl == null )
201                     {
202                         trunkUrl = scm.getConnection();
203                     }
204                     tagScmUrl = translateUrlPath( trunkUrl, branchBase, scm.getUrl() );
205                 }
206 
207                 // use original branch base without protocol
208                 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
209                                                               tagScmUrl );
210                 if ( !value.equals( scm.getUrl() ) )
211                 {
212                     rewriteElement( "url", value, scmRoot, namespace );
213                     result = true;
214                 }
215             }
216 
217             if ( branchName != null )
218             {
219                 String value = translator.resolveTag( branchName );
220                 if ( value != null && !value.equals( scm.getTag() ) )
221                 {
222                     rewriteElement( "tag", value, scmRoot, namespace );
223                     result = true;
224                 }
225             }
226         }
227         else
228         {
229             String message = "No SCM translator found - skipping rewrite";
230 
231             relResult.appendDebug( message );
232 
233             getLogger().debug( message );
234         }
235         return result;
236     }
237 
238     protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
239                                                          List<MavenProject> reactorProjects, boolean simulate )
240     {
241         return releaseDescriptor.getOriginalVersions( reactorProjects );
242     }
243 
244     @SuppressWarnings( "unchecked" )
245     @Override
246     protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
247     {
248         return releaseDescriptor.getReleaseVersions();
249     }
250 
251     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
252                                                  Map<String, Map<String, String>> resolvedSnapshotsMap )
253     {
254         Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
255 
256         if ( versionsMap != null )
257         {
258             return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
259         }
260         else
261         {
262             return null;
263         }
264     }
265 }