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.nio.file.LinkOption;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  
27  import org.apache.maven.artifact.ArtifactUtils;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.Scm;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.scm.repository.ScmRepository;
32  import org.apache.maven.shared.release.ReleaseExecutionException;
33  import org.apache.maven.shared.release.ReleaseResult;
34  import org.apache.maven.shared.release.config.ReleaseDescriptor;
35  import org.apache.maven.shared.release.scm.ScmTranslator;
36  import org.apache.maven.shared.release.util.ReleaseUtil;
37  import org.codehaus.plexus.component.annotations.Component;
38  
39  /**
40   * Rewrite POMs for branch.
41   *
42   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
43   * @version $Id$
44   */
45  @Component( role = ReleasePhase.class, hint = "rewrite-poms-for-branch" )
46  public class RewritePomsForBranchPhase
47      extends AbstractRewritePomsPhase
48  {
49      @Override
50      protected final String getPomSuffix()
51      {
52          return "branch";
53      }
54  
55      @Override
56      protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
57                                   String projectId, ScmRepository scmRepository, ReleaseResult result )
58      throws ReleaseExecutionException
59      {
60          // If SCM is null in original model, it is inherited, no mods needed
61          if ( project.getScm() != null )
62          {
63              Scm scmRoot = modelTarget.getScm();
64  
65              if ( scmRoot != null )
66              {
67                  try
68                  {
69                      translateScm( project, releaseDescriptor, scmRoot, scmRepository, result );
70                  }
71                  catch ( IOException e )
72                  {
73                      throw new ReleaseExecutionException( e.getMessage(), e );
74                  }
75              }
76              else
77              {
78                  MavenProject parent = project.getParent();
79                  if ( parent != null )
80                  {
81                      // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
82                      // the release process and so has not been modified, so the values will not be correct on the tag),
83                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
84                      if ( releaseDescriptor.getOriginalScmInfo( parentId ) == null )
85                      {
86                          // we need to add it, since it has changed from the inherited value
87                          scmRoot = new Scm();
88                          // reset default value (HEAD)
89                          scmRoot.setTag( null );
90  
91                          try
92                          {
93                              if ( translateScm( project, releaseDescriptor, scmRoot, scmRepository, result ) )
94                              {
95                                  modelTarget.setScm( scmRoot );
96                              }
97                          }
98                          catch ( IOException e )
99                          {
100                             throw new ReleaseExecutionException( e.getMessage(), e );
101                         }
102                     }
103                 }
104             }
105         }
106     }
107 
108     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget,
109                                   ScmRepository scmRepository, ReleaseResult relResult )
110     throws IOException
111     {
112         ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
113         boolean result = false;
114         if ( translator != null )
115         {
116             Scm scm = project.getOriginalModel().getScm();
117             if ( scm == null )
118             {
119                 scm = project.getScm();
120             }
121 
122             String branchName = releaseDescriptor.getScmReleaseLabel();
123             String branchBase = releaseDescriptor.getScmBranchBase();
124 
125             // TODO: svn utils should take care of prepending this
126             if ( branchBase != null )
127             {
128                 branchBase = "scm:svn:" + branchBase;
129             }
130 
131             Path projectBasedir = project.getBasedir().toPath().toRealPath( LinkOption.NOFOLLOW_LINKS );
132             Path workingDirectory = Paths.get( releaseDescriptor.getWorkingDirectory() );
133 
134             int count = ReleaseUtil.getBaseWorkingDirectoryParentCount( workingDirectory, projectBasedir );
135             
136             if ( scm.getConnection() != null )
137             {
138                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
139 
140                 String subDirectoryBranch = scm.getConnection().substring( rootUrl.length() );
141                 if ( !subDirectoryBranch.startsWith( "/" ) )
142                 {
143                     subDirectoryBranch = "/" + subDirectoryBranch;
144                 }
145 
146                 String scmConnectionBranch = branchBase;
147                 if ( scmConnectionBranch != null )
148                 {
149                     String trunkUrl = scm.getDeveloperConnection();
150                     if ( trunkUrl == null )
151                     {
152                         trunkUrl = scm.getConnection();
153                     }
154                     scmConnectionBranch = translateUrlPath( trunkUrl, branchBase, scm.getConnection() );
155                 }
156 
157                 String value =
158                     translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch,
159                                                    scmConnectionBranch );
160                 if ( !value.equals( scm.getConnection() ) )
161                 {
162                     scmTarget.setConnection( value );
163                     result = true;
164                 }
165             }
166 
167             if ( scm.getDeveloperConnection() != null )
168             {
169                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
170 
171                 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
172                 if ( !subDirectoryBranch.startsWith( "/" ) )
173                 {
174                     subDirectoryBranch = "/" + subDirectoryBranch;
175                 }
176 
177                 String value =
178                     translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
179                                                    branchBase );
180                 if ( !value.equals( scm.getDeveloperConnection() ) )
181                 {
182                     scmTarget.setDeveloperConnection( value );
183                     result = true;
184                 }
185             }
186 
187             if ( scm.getUrl() != null )
188             {
189                 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
190 
191                 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
192                 if ( !subDirectoryBranch.startsWith( "/" ) )
193                 {
194                     subDirectoryBranch = "/" + subDirectoryBranch;
195                 }
196 
197                 String tagScmUrl = branchBase;
198                 if ( tagScmUrl != null )
199                 {
200                     String trunkUrl = scm.getDeveloperConnection();
201                     if ( trunkUrl == null )
202                     {
203                         trunkUrl = scm.getConnection();
204                     }
205                     tagScmUrl = translateUrlPath( trunkUrl, branchBase, scm.getUrl() );
206                 }
207 
208                 // use original branch base without protocol
209                 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
210                                                               tagScmUrl );
211                 if ( !value.equals( scm.getUrl() ) )
212                 {
213                     scmTarget.setUrl( value );
214                     result = true;
215                 }
216             }
217 
218             if ( branchName != null )
219             {
220                 String value = translator.resolveTag( branchName );
221                 if ( value != null && !value.equals( scm.getTag() ) )
222                 {
223                     scmTarget.setTag( value );
224                     result = true;
225                 }
226             }
227         }
228         else
229         {
230             String message = "No SCM translator found - skipping rewrite";
231 
232             relResult.appendDebug( message );
233 
234             getLogger().debug( message );
235         }
236         return result;
237     }
238 
239     @Override
240     protected String getOriginalVersion( ReleaseDescriptor releaseDescriptor, String projectKey, boolean simulate )
241     {
242         return releaseDescriptor.getProjectOriginalVersion( projectKey );
243     }
244 
245     @Override
246     protected String getNextVersion( ReleaseDescriptor releaseDescriptor, String key )
247     {
248         return releaseDescriptor.getProjectReleaseVersion( key );
249     }
250 
251     @Override
252     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
253                                                  ReleaseDescriptor releaseDescriptor )
254     {
255         return releaseDescriptor.getDependencyReleaseVersion( artifactVersionlessKey );
256     }
257 }