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 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
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, 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 ) throws IOException
108 {
109 ScmTranslator translator = getScmTranslators().get( scmRepository.getProvider() );
110 boolean result = false;
111 if ( translator != null )
112 {
113 Scm scm = project.getOriginalModel().getScm();
114 if ( scm == null )
115 {
116 scm = project.getScm();
117 }
118
119 String tag = releaseDescriptor.getScmReleaseLabel();
120 String tagBase = releaseDescriptor.getScmTagBase();
121
122
123 if ( tagBase != null )
124 {
125 tagBase = "scm:svn:" + tagBase;
126 }
127
128 String workingDirectory =
129 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
130 : project.getBasedir().getAbsolutePath();
131 int count =
132 ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
133 if ( scm.getConnection() != null )
134 {
135 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
136
137 String subDirectoryTag = scm.getConnection().substring( rootUrl.length() );
138 if ( !subDirectoryTag.startsWith( "/" ) )
139 {
140 subDirectoryTag = "/" + subDirectoryTag;
141 }
142
143 String scmConnectionTag = tagBase;
144 if ( scmConnectionTag != null )
145 {
146 String trunkUrl = scm.getDeveloperConnection();
147 if ( trunkUrl == null )
148 {
149 trunkUrl = scm.getConnection();
150 }
151 scmConnectionTag = translateUrlPath( trunkUrl, tagBase, scm.getConnection() );
152 }
153 String value =
154 translator.translateTagUrl( scm.getConnection(), tag + subDirectoryTag, scmConnectionTag );
155
156 if ( !value.equals( scm.getConnection() ) )
157 {
158 rewriteElement( "connection", value, scmRoot, namespace );
159 result = true;
160 }
161 }
162
163 if ( scm.getDeveloperConnection() != null )
164 {
165 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
166
167 String subDirectoryTag = scm.getDeveloperConnection().substring( rootUrl.length() );
168 if ( !subDirectoryTag.startsWith( "/" ) )
169 {
170 subDirectoryTag = "/" + subDirectoryTag;
171 }
172
173 String value =
174 translator.translateTagUrl( scm.getDeveloperConnection(), tag + subDirectoryTag, tagBase );
175
176 if ( !value.equals( scm.getDeveloperConnection() ) )
177 {
178 rewriteElement( "developerConnection", value, scmRoot, namespace );
179 result = true;
180 }
181 }
182
183 if ( scm.getUrl() != null )
184 {
185 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
186
187 String subDirectoryTag = scm.getUrl().substring( rootUrl.length() );
188 if ( !subDirectoryTag.startsWith( "/" ) )
189 {
190 subDirectoryTag = "/" + subDirectoryTag;
191 }
192
193 String tagScmUrl = tagBase;
194 if ( tagScmUrl != null )
195 {
196 String trunkUrl = scm.getDeveloperConnection();
197 if ( trunkUrl == null )
198 {
199 trunkUrl = scm.getConnection();
200 }
201 tagScmUrl = translateUrlPath( trunkUrl, tagBase, scm.getUrl() );
202 }
203
204 String value = translator.translateTagUrl( scm.getUrl(), tag + subDirectoryTag, tagScmUrl );
205 if ( !value.equals( scm.getUrl() ) )
206 {
207 rewriteElement( "url", value, scmRoot, namespace );
208 result = true;
209 }
210 }
211
212 if ( tag != null )
213 {
214 String value = translator.resolveTag( tag );
215 if ( value != null && !value.equals( scm.getTag() ) )
216 {
217 rewriteElement( "tag", value, scmRoot, namespace );
218 result = true;
219 }
220 }
221 }
222 else
223 {
224 String message = "No SCM translator found - skipping rewrite";
225
226 relResult.appendDebug( message );
227
228 getLogger().debug( message );
229 }
230 return result;
231 }
232
233 @Override
234 protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
235 List<MavenProject> reactorProjects, boolean simulate )
236 {
237 return releaseDescriptor.getOriginalVersions( reactorProjects );
238 }
239
240 @SuppressWarnings( "unchecked" )
241 @Override
242 protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
243 {
244 return releaseDescriptor.getReleaseVersions();
245 }
246
247 @Override
248 protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
249 Map<String, Map<String, String>> resolvedSnapshotsMap )
250 {
251 Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
252
253 if ( versionsMap != null )
254 {
255 return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
256 }
257 else
258 {
259 return null;
260 }
261 }
262 }