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