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   */
44  @Component( role = ReleasePhase.class, hint = "rewrite-poms-for-branch" )
45  public class RewritePomsForBranchPhase
46      extends AbstractRewritePomsPhase
47  {
48      @Override
49      protected final String getPomSuffix()
50      {
51          return "branch";
52      }
53  
54      @Override
55      protected void transformScm( MavenProject project, Model modelTarget, ReleaseDescriptor releaseDescriptor,
56                                   String projectId, ScmRepository scmRepository, ReleaseResult result )
57      throws ReleaseExecutionException
58      {
59          // If SCM is null in original model, it is inherited, no mods needed
60          if ( project.getScm() != null )
61          {
62              Scm scmRoot = modelTarget.getScm();
63  
64              if ( scmRoot != null )
65              {
66                  try
67                  {
68                      translateScm( project, releaseDescriptor, scmRoot, scmRepository, result );
69                  }
70                  catch ( IOException e )
71                  {
72                      throw new ReleaseExecutionException( e.getMessage(), e );
73                  }
74              }
75              else
76              {
77                  MavenProject parent = project.getParent();
78                  if ( parent != null )
79                  {
80                      // If the SCM element is not present, only add it if the parent was not mapped (ie, it's external to
81                      // the release process and so has not been modified, so the values will not be correct on the tag),
82                      String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
83                      if ( releaseDescriptor.getOriginalScmInfo( parentId ) == null )
84                      {
85                          // we need to add it, since it has changed from the inherited value
86                          scmRoot = new Scm();
87                          // reset default value (HEAD)
88                          scmRoot.setTag( null );
89  
90                          try
91                          {
92                              if ( translateScm( project, releaseDescriptor, scmRoot, scmRepository, result ) )
93                              {
94                                  modelTarget.setScm( scmRoot );
95                              }
96                          }
97                          catch ( IOException e )
98                          {
99                              throw new ReleaseExecutionException( e.getMessage(), e );
100                         }
101                     }
102                 }
103             }
104         }
105     }
106 
107     private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Scm scmTarget,
108                                   ScmRepository scmRepository, ReleaseResult relResult )
109     throws IOException
110     {
111         ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
112         boolean result = false;
113         if ( translator != null )
114         {
115             Scm scm = project.getOriginalModel().getScm();
116             if ( scm == null )
117             {
118                 scm = project.getScm();
119             }
120 
121             String branchName = releaseDescriptor.getScmReleaseLabel();
122             String branchBase = releaseDescriptor.getScmBranchBase();
123 
124             // TODO: svn utils should take care of prepending this
125             if ( branchBase != null )
126             {
127                 branchBase = "scm:svn:" + branchBase;
128             }
129 
130             Path projectBasedir = project.getBasedir().toPath().toRealPath( LinkOption.NOFOLLOW_LINKS );
131             Path workingDirectory = Paths.get( releaseDescriptor.getWorkingDirectory() );
132 
133             int count = ReleaseUtil.getBaseWorkingDirectoryParentCount( workingDirectory, projectBasedir );
134             
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                     scmTarget.setConnection( value );
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                     scmTarget.setDeveloperConnection( value );
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                     scmTarget.setUrl( value );
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                     scmTarget.setTag( value );
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     @Override
239     protected String getOriginalVersion( ReleaseDescriptor releaseDescriptor, String projectKey, boolean simulate )
240     {
241         return releaseDescriptor.getProjectOriginalVersion( projectKey );
242     }
243 
244     @Override
245     protected String getNextVersion( ReleaseDescriptor releaseDescriptor, String key )
246     {
247         return releaseDescriptor.getProjectReleaseVersion( key );
248     }
249 
250     @Override
251     protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
252                                                  ReleaseDescriptor releaseDescriptor )
253     {
254         return releaseDescriptor.getDependencyReleaseVersion( artifactVersionlessKey );
255     }
256 }