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, commonBasedir );
64 }
65 catch ( IOException e )
66 {
67 throw new ReleaseExecutionException( e.getMessage(), e );
68 }
69 }
70 else
71 {
72 releaseDescriptor.mapOriginalScmInfo( projectId, null );
73
74 MavenProject parent = project.getParent();
75 if ( parent != null )
76 {
77
78
79 String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
80 if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
81 {
82
83 scmRoot = new Element( "scm" );
84 scmRoot.addContent( "\n " );
85
86 try
87 {
88 if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
89 commonBasedir ) )
90 {
91 rootElement.addContent( "\n " ).addContent( scmRoot ).addContent( "\n" );
92 }
93 }
94 catch ( IOException e )
95 {
96 throw new ReleaseExecutionException( e.getMessage(), e );
97 }
98 }
99 }
100 }
101 }
102 }
103
104 private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
105 Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
106 String commonBasedir )
107 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 branchName = releaseDescriptor.getScmReleaseLabel();
120 String branchBase = releaseDescriptor.getScmBranchBase();
121
122
123 if ( branchBase != null )
124 {
125 branchBase = "scm:svn:" + branchBase;
126 }
127
128 String workingDirectory =
129 ReleaseUtil.isSymlink( project.getBasedir() ) ? project.getBasedir().getCanonicalPath()
130 : project.getBasedir().getAbsolutePath();
131
132 int count =
133 ReleaseUtil.getBaseWorkingDirectoryParentCount( commonBasedir, workingDirectory );
134 if ( scm.getConnection() != null )
135 {
136 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getConnection() );
137
138 String subDirectoryBranch = scm.getConnection().substring( rootUrl.length() );
139 if ( !subDirectoryBranch.startsWith( "/" ) )
140 {
141 subDirectoryBranch = "/" + subDirectoryBranch;
142 }
143
144 String scmConnectionBranch = branchBase;
145 if ( scmConnectionBranch != null )
146 {
147 String trunkUrl = scm.getDeveloperConnection();
148 if ( trunkUrl == null )
149 {
150 trunkUrl = scm.getConnection();
151 }
152 scmConnectionBranch = translateUrlPath( trunkUrl, branchBase, scm.getConnection() );
153 }
154
155 String value =
156 translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch, scmConnectionBranch );
157 if ( !value.equals( scm.getConnection() ) )
158 {
159 rewriteElement( "connection", value, scmRoot, namespace );
160 result = true;
161 }
162 }
163
164 if ( scm.getDeveloperConnection() != null )
165 {
166 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
167
168 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
169 if ( !subDirectoryBranch.startsWith( "/" ) )
170 {
171 subDirectoryBranch = "/" + subDirectoryBranch;
172 }
173
174 String value =
175 translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
176 branchBase );
177 if ( !value.equals( scm.getDeveloperConnection() ) )
178 {
179 rewriteElement( "developerConnection", value, scmRoot, namespace );
180 result = true;
181 }
182 }
183
184 if ( scm.getUrl() != null )
185 {
186 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
187
188 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
189 if ( !subDirectoryBranch.startsWith( "/" ) )
190 {
191 subDirectoryBranch = "/" + subDirectoryBranch;
192 }
193
194 String tagScmUrl = branchBase;
195 if ( tagScmUrl != null )
196 {
197 String trunkUrl = scm.getDeveloperConnection();
198 if ( trunkUrl == null )
199 {
200 trunkUrl = scm.getConnection();
201 }
202 tagScmUrl = translateUrlPath( trunkUrl, branchBase, scm.getUrl() );
203 }
204
205
206 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
207 tagScmUrl );
208 if ( !value.equals( scm.getUrl() ) )
209 {
210 rewriteElement( "url", value, scmRoot, namespace );
211 result = true;
212 }
213 }
214
215 if ( branchName != null )
216 {
217 String value = translator.resolveTag( branchName );
218 if ( value != null && !value.equals( scm.getTag() ) )
219 {
220 rewriteElement( "tag", value, scmRoot, namespace );
221 result = true;
222 }
223 }
224 }
225 else
226 {
227 String message = "No SCM translator found - skipping rewrite";
228
229 relResult.appendDebug( message );
230
231 getLogger().debug( message );
232 }
233 return result;
234 }
235
236 protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
237 List<MavenProject> reactorProjects, boolean simulate )
238 {
239 return releaseDescriptor.getOriginalVersions( reactorProjects );
240 }
241
242 @SuppressWarnings( "unchecked" )
243 @Override
244 protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
245 {
246 return releaseDescriptor.getReleaseVersions();
247 }
248
249 protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
250 Map<String, Map<String, String>> resolvedSnapshotsMap )
251 {
252 Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
253
254 if ( versionsMap != null )
255 {
256 return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
257 }
258 else
259 {
260 return null;
261 }
262 }
263 }