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 release.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   */
43  public class RewritePomsForReleasePhase
44      extends AbstractRewritePomsPhase
45  {
46  
47      @Override
48      protected void transformScm( MavenProject project, Element rootElement, Namespace namespace,
49                                   ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository,
50                                   ReleaseResult result, String commonBasedir )
51      throws ReleaseExecutionException
52      {
53          // If SCM is null in original model, it is inherited, no mods needed
54          if ( project.getScm() != null )
55          {
56              Element scmRoot = rootElement.getChild( "scm", namespace );
57              if ( scmRoot != null )
58              {
59                  Scm scm = buildScm( project );
60                  releaseDescriptor.mapOriginalScmInfo( projectId, scm );
61  
62                  try
63                  {
64                      translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
65                                    commonBasedir );
66                  }
67                  catch ( IOException e )
68                  {
69                      throw new ReleaseExecutionException( e.getMessage(), e );
70                  }
71              }
72              else
73              {
74                  releaseDescriptor.mapOriginalScmInfo( projectId, null );
75  
76                  MavenProject parent = project.getParent();
77                  if ( parent != null )
78                  {
79                      // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
80                      // the release process and so has not been modified, so the values will not be correct on the tag),
81                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
82                      if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
83                      {
84                          // we need to add it, since it has changed from the inherited value
85                          scmRoot = new Element( "scm" );
86                          scmRoot.addContent( "\n  " );
87  
88                          try
89                          {
90                              if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
91                                                 commonBasedir ) )
92                              {
93                                  rootElement.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
94                              }
95                          }
96                          catch ( IOException e )
97                          {
98                              throw new ReleaseExecutionException( e.getMessage(), e );
99                          }
100                     }
101                 }
102             }
103         }
104     }
105 
106     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
107                                   Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
108                                   String commonBasedir ) 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 tag = releaseDescriptor.getScmReleaseLabel();
121             String tagBase = releaseDescriptor.getScmTagBase();
122 
123             // TODO: svn utils should take care of prepending this
124             if ( tagBase != null )
125             {
126                 tagBase = "scm:svn:" + tagBase;
127             }
128 
129             String workingDirectory =
130                 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
131                                 : project.getBasedir().getAbsolutePath();
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 subDirectoryTag = scm.getConnection().substring( rootUrl.length() );
139                 if ( !subDirectoryTag.startsWith( "/" ) )
140                 {
141                     subDirectoryTag = "/" + subDirectoryTag;
142                 }
143 
144                 String scmConnectionTag = tagBase;
145                 if ( scmConnectionTag != null )
146                 {
147                     String trunkUrl = scm.getDeveloperConnection();
148                     if ( trunkUrl == null )
149                     {
150                         trunkUrl = scm.getConnection();
151                     }
152                     scmConnectionTag = translateUrlPath( trunkUrl, tagBase, scm.getConnection() );
153                 }
154                 String value =
155                     translator.translateTagUrl( scm.getConnection(), tag + subDirectoryTag, scmConnectionTag );
156 
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 subDirectoryTag = scm.getDeveloperConnection().substring( rootUrl.length() );
169                 if ( !subDirectoryTag.startsWith( "/" ) )
170                 {
171                     subDirectoryTag = "/" + subDirectoryTag;
172                 }
173 
174                 String value =
175                     translator.translateTagUrl( scm.getDeveloperConnection(), tag + subDirectoryTag, tagBase );
176 
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 subDirectoryTag = scm.getUrl().substring( rootUrl.length() );
189                 if ( !subDirectoryTag.startsWith( "/" ) )
190                 {
191                     subDirectoryTag = "/" + subDirectoryTag;
192                 }
193 
194                 String tagScmUrl = tagBase;
195                 if ( tagScmUrl != null )
196                 {
197                     String trunkUrl = scm.getDeveloperConnection();
198                     if ( trunkUrl == null )
199                     {
200                         trunkUrl = scm.getConnection();
201                     }
202                     tagScmUrl = translateUrlPath( trunkUrl, tagBase, scm.getUrl() );
203                 }
204                 // use original tag base without protocol
205                 String value = translator.translateTagUrl( scm.getUrl(), tag + subDirectoryTag, tagScmUrl );
206                 if ( !value.equals( scm.getUrl() ) )
207                 {
208                     rewriteElement( "url", value, scmRoot, namespace );
209                     result = true;
210                 }
211             }
212 
213             if ( tag != null )
214             {
215                 String value = translator.resolveTag( tag );
216                 if ( value != null && !value.equals( scm.getTag() ) )
217                 {
218                     rewriteElement( "tag", value, scmRoot, namespace );
219                     result = true;
220                 }
221             }
222         }
223         else
224         {
225             String message = "No SCM translator found - skipping rewrite";
226 
227             relResult.appendDebug( message );
228 
229             getLogger().debug( message );
230         }
231         return result;
232     }
233 
234     @Override
235     protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
236                                                          List<MavenProject> reactorProjects, boolean simulate )
237     {
238         return releaseDescriptor.getOriginalVersions( reactorProjects );
239     }
240 
241     @SuppressWarnings( "unchecked" )
242     @Override
243     protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
244     {
245         return releaseDescriptor.getReleaseVersions();
246     }
247 
248     @Override
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 }