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
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 if ( project.getScm() != null )
59 {
60 Element scmRoot = rootElement.getChild( "scm", namespace );
61 if ( scmRoot != null )
62 {
63 Scm scm = buildScm( project );
64 releaseDescriptor.mapOriginalScmInfo( projectId, scm );
65
66 try
67 {
68 translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result, commonBasedir );
69 }
70 catch ( IOException e )
71 {
72 throw new ReleaseExecutionException( e.getMessage(), e );
73 }
74 }
75 else
76 {
77 releaseDescriptor.mapOriginalScmInfo( projectId, null );
78
79 MavenProject parent = project.getParent();
80 if ( parent != null )
81 {
82
83
84 String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() );
85 if ( !releaseDescriptor.getOriginalScmInfo().containsKey( parentId ) )
86 {
87
88 scmRoot = new Element( "scm" );
89 scmRoot.addContent( "\n " );
90
91 try
92 {
93 if ( translateScm( project, releaseDescriptor, scmRoot, namespace, scmRepository, result,
94 commonBasedir ) )
95 {
96 rootElement.addContent( "\n " ).addContent( scmRoot ).addContent( "\n" );
97 }
98 }
99 catch ( IOException e )
100 {
101 throw new ReleaseExecutionException( e.getMessage(), e );
102 }
103 }
104 }
105 }
106 }
107 }
108
109 private boolean translateScm( MavenProject project, ReleaseDescriptor releaseDescriptor, Element scmRoot,
110 Namespace namespace, ScmRepository scmRepository, ReleaseResult relResult,
111 String commonBasedir )
112 throws IOException
113 {
114 ScmTranslator translator = scmTranslators.get( scmRepository.getProvider() );
115 boolean result = false;
116 if ( translator != null )
117 {
118 Scm scm = project.getScm();
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 value =
145 translator.translateBranchUrl( scm.getConnection(), branchName + subDirectoryBranch, branchBase );
146 if ( !value.equals( scm.getConnection() ) )
147 {
148 rewriteElement( "connection", value, scmRoot, namespace );
149 result = true;
150 }
151 }
152
153 if ( scm.getDeveloperConnection() != null )
154 {
155 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getDeveloperConnection() );
156
157 String subDirectoryBranch = scm.getDeveloperConnection().substring( rootUrl.length() );
158 if ( !subDirectoryBranch.startsWith( "/" ) )
159 {
160 subDirectoryBranch = "/" + subDirectoryBranch;
161 }
162
163 String value =
164 translator.translateBranchUrl( scm.getDeveloperConnection(), branchName + subDirectoryBranch,
165 branchBase );
166 if ( !value.equals( scm.getDeveloperConnection() ) )
167 {
168 rewriteElement( "developerConnection", value, scmRoot, namespace );
169 result = true;
170 }
171 }
172
173 if ( scm.getUrl() != null )
174 {
175 String rootUrl = ReleaseUtil.realignScmUrl( count, scm.getUrl() );
176
177 String subDirectoryBranch = scm.getUrl().substring( rootUrl.length() );
178 if ( !subDirectoryBranch.startsWith( "/" ) )
179 {
180 subDirectoryBranch = "/" + subDirectoryBranch;
181 }
182
183
184 String value = translator.translateBranchUrl( scm.getUrl(), branchName + subDirectoryBranch,
185 releaseDescriptor.getScmBranchBase() );
186 if ( !value.equals( scm.getUrl() ) )
187 {
188 rewriteElement( "url", value, scmRoot, namespace );
189 result = true;
190 }
191 }
192
193 if ( branchName != null )
194 {
195 String value = translator.resolveTag( branchName );
196 if ( value != null && !value.equals( scm.getTag() ) )
197 {
198 rewriteElement( "tag", value, scmRoot, namespace );
199 result = true;
200 }
201 }
202 }
203 else
204 {
205 String message = "No SCM translator found - skipping rewrite";
206
207 relResult.appendDebug( message );
208
209 getLogger().debug( message );
210 }
211 return result;
212 }
213
214 protected Map<String, String> getOriginalVersionMap( ReleaseDescriptor releaseDescriptor,
215 List<MavenProject> reactorProjects, boolean simulate )
216 {
217 return releaseDescriptor.getOriginalVersions( reactorProjects );
218 }
219
220 @SuppressWarnings( "unchecked" )
221 protected Map<String, String> getNextVersionMap( ReleaseDescriptor releaseDescriptor )
222 {
223 return releaseDescriptor.getReleaseVersions();
224 }
225
226 protected String getResolvedSnapshotVersion( String artifactVersionlessKey,
227 Map<String, Map<String, String>> resolvedSnapshotsMap )
228 {
229 Map<String, String> versionsMap = resolvedSnapshotsMap.get( artifactVersionlessKey );
230
231 if ( versionsMap != null )
232 {
233 return versionsMap.get( ReleaseDescriptor.RELEASE_KEY );
234 }
235 else
236 {
237 return null;
238 }
239 }
240 }