1 package org.apache.maven.shared.release.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
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
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
79
80 String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
81 if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
82 {
83
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
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
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 }