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.codehaus.plexus.util.StringUtils; |
36 | |
import org.jdom.Element; |
37 | |
import org.jdom.Namespace; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 190 | public class RewritePomsForReleasePhase |
45 | |
extends AbstractRewritePomsPhase |
46 | |
{ |
47 | |
|
48 | |
|
49 | |
|
50 | |
private Map<String, ScmTranslator> scmTranslators; |
51 | |
|
52 | |
protected void transformScm( MavenProject project, Element rootElement, Namespace namespace, |
53 | |
ReleaseDescriptor releaseDescriptor, String projectId, ScmRepository scmRepository, |
54 | |
ReleaseResult result, String commonBasedir ) |
55 | |
throws ReleaseExecutionException |
56 | |
{ |
57 | |
|
58 | 264 | if ( project.getScm() != null ) |
59 | |
{ |
60 | 96 | Element scmRoot = rootElement.getChild( "scm", namespace ); |
61 | 96 | if ( scmRoot != null ) |
62 | |
{ |
63 | 62 | Scm scm = buildScm( project ); |
64 | 62 | releaseDescriptor.mapOriginalScmInfo( projectId, scm ); |
65 | |
|
66 | |
try |
67 | |
{ |
68 | 62 | translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, commonBasedir ); |
69 | |
} |
70 | 0 | catch ( IOException e ) |
71 | |
{ |
72 | 0 | throw new ReleaseExecutionException( e.getMessage(), e ); |
73 | 62 | } |
74 | 62 | } |
75 | |
else |
76 | |
{ |
77 | 34 | releaseDescriptor.mapOriginalScmInfo( projectId, null ); |
78 | |
|
79 | 34 | MavenProject parent = project.getParent(); |
80 | 34 | if ( parent != null ) |
81 | |
{ |
82 | |
|
83 | |
|
84 | 34 | String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() ); |
85 | 34 | if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) ) |
86 | |
{ |
87 | |
|
88 | 4 | scmRoot = new Element( "scm" ); |
89 | 4 | scmRoot.addContent( "\n " ); |
90 | |
|
91 | |
try |
92 | |
{ |
93 | 4 | if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, |
94 | |
commonBasedir ) ) |
95 | |
{ |
96 | 4 | rootElement.addContent( "\n " ).addContent( scmRoot ).addContent( "\n" ); |
97 | |
} |
98 | |
} |
99 | 0 | catch ( IOException e ) |
100 | |
{ |
101 | 0 | throw new ReleaseExecutionException( e.getMessage(), e ); |
102 | 4 | } |
103 | |
} |
104 | |
} |
105 | |
} |
106 | |
} |
107 | 264 | } |
108 | |
|
109 | |
private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot, |
110 | |
Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult, |
111 | |
String commonBasedir ) throws IOException |
112 | |
{ |
113 | 66 | ScmTranslator translator = scmTranslators.get( scmRepository.getProvider() ); |
114 | 66 | boolean result = false; |
115 | 66 | if ( translator != null ) |
116 | |
{ |
117 | 60 | Scm scm = project.getOriginalModel().getScm(); |
118 | 60 | if ( scm == null ) |
119 | |
{ |
120 | 4 | scm = project.getScm(); |
121 | |
} |
122 | |
|
123 | 60 | String tag = releaseDescriptor.getScmReleaseLabel(); |
124 | 60 | String tagBase = releaseDescriptor.getScmTagBase(); |
125 | |
|
126 | |
|
127 | 60 | if ( tagBase != null ) |
128 | |
{ |
129 | 4 | tagBase = "scm:svn:" + tagBase; |
130 | |
} |
131 | |
|
132 | 60 | String workingDirectory = |
133 | |
ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath() |
134 | |
: project.getBasedir().getAbsolutePath(); |
135 | 60 | int count = |
136 | |
ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory ); |
137 | 60 | if ( scm.getConnection() != null ) |
138 | |
{ |
139 | 60 | String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() ); |
140 | |
|
141 | 60 | String subDirectoryTag = scm.getConnection().substring( rootUrl.length() ); |
142 | 60 | if ( !subDirectoryTag.startsWith( "/" ) ) |
143 | |
{ |
144 | 54 | subDirectoryTag = "/" + subDirectoryTag; |
145 | |
} |
146 | |
|
147 | 60 | String scmConnectionTag = tagBase; |
148 | 60 | if ( scmConnectionTag != null ) |
149 | |
{ |
150 | 4 | String trunkUrl = scm.getDeveloperConnection(); |
151 | 4 | if ( trunkUrl == null ) |
152 | |
{ |
153 | 0 | trunkUrl = scm.getConnection(); |
154 | |
} |
155 | 4 | scmConnectionTag = this.translateUrlPath( trunkUrl, tagBase, scm.getConnection() ); |
156 | |
} |
157 | 60 | String value = |
158 | |
translator.translateTagUrl( scm.getConnection(), tag + subDirectoryTag, scmConnectionTag ); |
159 | |
|
160 | 60 | if ( !value.equals( scm.getConnection() ) ) |
161 | |
{ |
162 | 56 | rewriteElement( "connection", value, scmRoot, namespace ); |
163 | 56 | result = true; |
164 | |
} |
165 | |
} |
166 | |
|
167 | 60 | if ( scm.getDeveloperConnection() != null ) |
168 | |
{ |
169 | 56 | String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() ); |
170 | |
|
171 | 56 | String subDirectoryTag = scm.getDeveloperConnection().substring( rootUrl.length() ); |
172 | 56 | if ( !subDirectoryTag.startsWith( "/" ) ) |
173 | |
{ |
174 | 50 | subDirectoryTag = "/" + subDirectoryTag; |
175 | |
} |
176 | |
|
177 | 56 | String value = |
178 | |
translator.translateTagUrl( scm.getDeveloperConnection(), tag + subDirectoryTag, tagBase ); |
179 | |
|
180 | 56 | if ( !value.equals( scm.getDeveloperConnection() ) ) |
181 | |
{ |
182 | 52 | rewriteElement( "developerConnection", value, scmRoot, namespace ); |
183 | 52 | result = true; |
184 | |
} |
185 | |
} |
186 | |
|
187 | 60 | if ( scm.getUrl() != null ) |
188 | |
{ |
189 | 54 | String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() ); |
190 | |
|
191 | 54 | String subDirectoryTag = scm.getUrl().substring( rootUrl.length() ); |
192 | 54 | if ( !subDirectoryTag.startsWith( "/" ) ) |
193 | |
{ |
194 | 48 | subDirectoryTag = "/" + subDirectoryTag; |
195 | |
} |
196 | |
|
197 | 54 | String tagScmUrl = tagBase; |
198 | 54 | if ( tagScmUrl != null ) |
199 | |
{ |
200 | 4 | String trunkUrl = scm.getDeveloperConnection(); |
201 | 4 | if ( trunkUrl == null ) |
202 | |
{ |
203 | 0 | trunkUrl = scm.getConnection(); |
204 | |
} |
205 | 4 | tagScmUrl = this.translateUrlPath( trunkUrl, tagBase, scm.getUrl() ); |
206 | |
} |
207 | |
|
208 | 54 | String value = translator.translateTagUrl( scm.getUrl(), tag + subDirectoryTag, tagScmUrl ); |
209 | 54 | if ( !value.equals( scm.getUrl() ) ) |
210 | |
{ |
211 | 50 | rewriteElement( "url", value, scmRoot, namespace ); |
212 | 50 | result = true; |
213 | |
} |
214 | |
} |
215 | |
|
216 | 60 | if ( tag != null ) |
217 | |
{ |
218 | 60 | String value = translator.resolveTag( tag ); |
219 | 60 | if ( value != null && !value.equals( scm.getTag() ) ) |
220 | |
{ |
221 | 4 | rewriteElement( "tag", value, scmRoot, namespace ); |
222 | 4 | result = true; |
223 | |
} |
224 | |
} |
225 | 60 | } |
226 | |
else |
227 | |
{ |
228 | 6 | String message = "No SCM translator found - skipping rewrite"; |
229 | |
|
230 | 6 | relResult.appendDebug( message ); |
231 | |
|
232 | 6 | getLogger().debug( message ); |
233 | |
} |
234 | 66 | return result; |
235 | |
} |
236 | |
|
237 | |
protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor, |
238 | |
List<MavenProject> reactorProjects, boolean simulate ) |
239 | |
{ |
240 | 284 | return releaseDescriptor.getOriginalVersions( reactorProjects ); |
241 | |
} |
242 | |
|
243 | |
@SuppressWarnings( "unchecked" ) |
244 | |
protected Map<String,String> getNextVersionMap( ReleaseDescriptor releaseDescriptor ) |
245 | |
{ |
246 | 284 | return releaseDescriptor.getReleaseVersions(); |
247 | |
} |
248 | |
|
249 | |
protected String getResolvedSnapshotVersion( String artifactVersionlessKey, |
250 | |
Map<String, Map<String, String>> resolvedSnapshotsMap ) |
251 | |
{ |
252 | 188 | Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey ); |
253 | |
|
254 | 188 | if ( versionsMap != null ) |
255 | |
{ |
256 | 2 | return versionsMap.get( ReleaseDescriptor.RELEASE_KEY ); |
257 | |
} |
258 | |
else |
259 | |
{ |
260 | 186 | return null; |
261 | |
} |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private String translateUrlPath( String trunkPath, String tagPath, String urlPath ) |
274 | |
{ |
275 | 8 | trunkPath = trunkPath.trim(); |
276 | 8 | tagPath = tagPath.trim(); |
277 | |
|
278 | 8 | if ( trunkPath.endsWith( "/" ) ) |
279 | |
{ |
280 | 0 | trunkPath = trunkPath.substring( 0, trunkPath.length() - 1 ); |
281 | |
} |
282 | 8 | if ( tagPath.endsWith( "/" ) ) |
283 | |
{ |
284 | 0 | tagPath = tagPath.substring( 0, tagPath.length() - 1 ); |
285 | |
} |
286 | 8 | char[] tagPathChars = trunkPath.toCharArray(); |
287 | 8 | char[] trunkPathChars = tagPath.toCharArray(); |
288 | |
|
289 | 8 | int i = 0; |
290 | 312 | while ( ( i < tagPathChars.length ) && ( i < trunkPathChars.length ) && tagPathChars[i] == trunkPathChars[i] ) |
291 | |
{ |
292 | 304 | ++i; |
293 | |
} |
294 | |
|
295 | |
|
296 | 8 | if ( i == 0 || urlPath.indexOf( trunkPath.substring( i ) ) < 0 ) |
297 | |
{ |
298 | 0 | return tagPath; |
299 | |
} |
300 | |
else |
301 | |
{ |
302 | 8 | return StringUtils.replace( urlPath, trunkPath.substring( i ), tagPath.substring( i ) ); |
303 | |
} |
304 | |
} |
305 | |
} |