1 | |
package org.apache.maven.plugins.scmpublish; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.io.FileUtils; |
23 | |
import org.apache.maven.execution.MavenSession; |
24 | |
import org.apache.maven.plugin.AbstractMojo; |
25 | |
import org.apache.maven.plugin.MojoExecutionException; |
26 | |
import org.apache.maven.plugin.MojoFailureException; |
27 | |
import org.apache.maven.plugins.annotations.Component; |
28 | |
import org.apache.maven.plugins.annotations.Parameter; |
29 | |
import org.apache.maven.scm.CommandParameter; |
30 | |
import org.apache.maven.scm.CommandParameters; |
31 | |
import org.apache.maven.scm.ScmBranch; |
32 | |
import org.apache.maven.scm.ScmException; |
33 | |
import org.apache.maven.scm.ScmFileSet; |
34 | |
import org.apache.maven.scm.ScmResult; |
35 | |
import org.apache.maven.scm.manager.NoSuchScmProviderException; |
36 | |
import org.apache.maven.scm.manager.ScmManager; |
37 | |
import org.apache.maven.scm.provider.ScmProvider; |
38 | |
import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider; |
39 | |
import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; |
40 | |
import org.apache.maven.scm.repository.ScmRepository; |
41 | |
import org.apache.maven.scm.repository.ScmRepositoryException; |
42 | |
import org.apache.maven.settings.Settings; |
43 | |
import org.apache.maven.shared.release.config.ReleaseDescriptor; |
44 | |
import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator; |
45 | |
|
46 | |
import java.io.File; |
47 | |
import java.io.IOException; |
48 | |
import java.util.Map; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public abstract class AbstractScmPublishMojo |
54 | |
extends AbstractMojo |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Parameter ( property = "scmpublish.inventoryFile", |
61 | |
defaultValue = "${project.build.directory}/scmpublish-inventory.js" ) |
62 | |
protected File inventoryFile; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
@Parameter ( property = "scmpublish.pubScmUrl", defaultValue = "${project.distributionManagement.site.url}", |
68 | |
required = true ) |
69 | |
protected String pubScmUrl; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Parameter ( property = "scmpublish.checkoutDirectory", |
75 | |
defaultValue = "${project.build.directory}/scmpublish-checkout" ) |
76 | |
protected File checkoutDirectory; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@Parameter |
82 | |
protected String excludes; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
@Parameter |
88 | |
protected String includes; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Parameter |
94 | |
private Map<String, String> providerImplementations; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
@Component |
100 | |
private ScmManager scmManager; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Component |
106 | |
protected ScmRepositoryConfigurator scmRepositoryConfigurator; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
@Parameter ( property = "username" ) |
112 | |
protected String username; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
@Parameter ( property = "password" ) |
118 | |
protected String password; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@Parameter ( property = "localCheckout", defaultValue = "false" ) |
126 | |
protected boolean localCheckout; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
@Parameter ( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" ) |
133 | |
protected String siteOutputEncoding; |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
@Parameter ( property = "scmpublish.tryUpdate", defaultValue = "false" ) |
140 | |
protected boolean tryUpdate; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
@Parameter ( property = "scmpublish.skipDeletedFiles", defaultValue = "false" ) |
146 | |
protected boolean skipDeletedFiles; |
147 | |
|
148 | |
|
149 | |
|
150 | |
@Parameter ( defaultValue = "${basedir}", readonly = true ) |
151 | |
protected File basedir; |
152 | |
|
153 | |
|
154 | |
|
155 | |
@Component |
156 | |
protected Settings settings; |
157 | |
|
158 | |
|
159 | |
|
160 | |
@Component |
161 | |
protected MavenSession session; |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
@Parameter |
168 | |
protected String[] ignorePathsToDelete; |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
@Parameter ( property = "scmpublish.scm.branch" ) |
174 | |
protected String scmBranch; |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
@Parameter ( property = "scmpublish.automaticRemotePathCreation", defaultValue = "true" ) |
180 | |
protected boolean automaticRemotePathCreation; |
181 | |
|
182 | |
protected ScmProvider scmProvider; |
183 | |
|
184 | |
protected ScmRepository scmRepository; |
185 | |
|
186 | |
protected AbstractScmPublishMojo() |
187 | |
{ |
188 | 0 | super(); |
189 | 0 | } |
190 | |
|
191 | |
protected void logInfo( String format, Object... params ) |
192 | |
{ |
193 | 0 | getLog().info( String.format( format, params ) ); |
194 | 0 | } |
195 | |
|
196 | |
protected void logError( String format, Object... params ) |
197 | |
{ |
198 | 0 | getLog().error( String.format( format, params ) ); |
199 | 0 | } |
200 | |
|
201 | |
protected ReleaseDescriptor setupScm() |
202 | |
throws ScmRepositoryException, NoSuchScmProviderException |
203 | |
{ |
204 | |
String scmUrl; |
205 | 0 | if ( localCheckout ) |
206 | |
{ |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | 0 | String providerPart = pubScmUrl.substring( 0, pubScmUrl.indexOf( ':', 4 ) ); |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | 0 | scmUrl = providerPart + ":file://" + "target/localCheckout"; |
218 | 0 | logInfo( "Performing a LOCAL checkout from " + scmUrl ); |
219 | |
} |
220 | |
|
221 | 0 | ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor(); |
222 | 0 | releaseDescriptor.setInteractive( settings.isInteractiveMode() ); |
223 | |
|
224 | |
|
225 | |
|
226 | 0 | releaseDescriptor.setScmPassword( password ); |
227 | 0 | releaseDescriptor.setScmUsername( username ); |
228 | |
|
229 | 0 | releaseDescriptor.setWorkingDirectory( basedir.getAbsolutePath() ); |
230 | 0 | releaseDescriptor.setLocalCheckout( localCheckout ); |
231 | 0 | releaseDescriptor.setScmSourceUrl( pubScmUrl ); |
232 | |
|
233 | 0 | if ( providerImplementations != null ) |
234 | |
{ |
235 | 0 | for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() ) |
236 | |
{ |
237 | 0 | getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '" |
238 | |
+ providerEntry.getValue() + "'." ); |
239 | 0 | scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() ); |
240 | |
} |
241 | |
} |
242 | |
|
243 | 0 | scmRepository = scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, settings ); |
244 | |
|
245 | 0 | scmProvider = scmRepositoryConfigurator.getRepositoryProvider( scmRepository ); |
246 | |
|
247 | 0 | return releaseDescriptor; |
248 | |
} |
249 | |
|
250 | |
protected void checkoutExisting() |
251 | |
throws MojoExecutionException |
252 | |
{ |
253 | |
|
254 | 0 | if ( scmProvider instanceof AbstractSvnScmProvider ) |
255 | |
{ |
256 | 0 | File baseDir = null; |
257 | |
try |
258 | |
{ |
259 | |
|
260 | 0 | getLog().debug( "use AbstractSvnScmProvider so we can check if remote url exists and create it" ); |
261 | 0 | AbstractSvnScmProvider svnScmProvider = (AbstractSvnScmProvider) scmProvider; |
262 | 0 | String remoteUrl = ( (SvnScmProviderRepository) scmRepository.getProviderRepository() ).getUrl(); |
263 | 0 | boolean remoteExists = svnScmProvider.remoteUrlExist( scmRepository.getProviderRepository(), null ); |
264 | |
|
265 | 0 | if ( !remoteExists ) |
266 | |
{ |
267 | 0 | if ( automaticRemotePathCreation ) |
268 | |
{ |
269 | 0 | logInfo( "remote url %s not exists so create it", remoteUrl ); |
270 | |
|
271 | |
|
272 | 0 | baseDir = File.createTempFile( "scm", "tmp" ); |
273 | 0 | baseDir.delete(); |
274 | 0 | baseDir.mkdirs(); |
275 | |
|
276 | 0 | ScmFileSet scmFileSet = new ScmFileSet( baseDir, new File( "" ) ); |
277 | |
|
278 | 0 | CommandParameters commandParameters = new CommandParameters(); |
279 | 0 | commandParameters.setString( CommandParameter.SCM_MKDIR_CREATE_IN_LOCAL, |
280 | |
Boolean.FALSE.toString() ); |
281 | 0 | commandParameters.setString( CommandParameter.MESSAGE, |
282 | |
"automatic path creation: " + remoteUrl ); |
283 | 0 | svnScmProvider.mkdir( scmRepository.getProviderRepository(), scmFileSet, commandParameters ); |
284 | |
|
285 | |
|
286 | 0 | if ( checkoutDirectory.exists() ) |
287 | |
{ |
288 | 0 | FileUtils.deleteDirectory( checkoutDirectory ); |
289 | |
} |
290 | 0 | } |
291 | |
else |
292 | |
{ |
293 | |
|
294 | 0 | logInfo( "remote url %s not exists and not create it as field %s configured to false", |
295 | |
remoteUrl, Boolean.toString( automaticRemotePathCreation ) ); |
296 | |
} |
297 | |
} |
298 | |
} |
299 | 0 | catch ( IOException e ) |
300 | |
{ |
301 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
302 | |
} |
303 | 0 | catch ( ScmException e ) |
304 | |
{ |
305 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
306 | |
} |
307 | |
|
308 | |
finally |
309 | |
|
310 | |
{ |
311 | 0 | if ( baseDir != null ) |
312 | |
{ |
313 | |
try |
314 | |
{ |
315 | 0 | FileUtils.forceDeleteOnExit( baseDir ); |
316 | |
} |
317 | 0 | catch ( IOException e ) |
318 | |
{ |
319 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
320 | 0 | } |
321 | |
} |
322 | |
} |
323 | |
} |
324 | |
|
325 | 0 | logInfo( "%s the pub tree from %s ...", ( tryUpdate ? "Updating" : "Checking out" ), pubScmUrl ); |
326 | |
|
327 | 0 | if ( checkoutDirectory.exists() && !tryUpdate ) |
328 | |
|
329 | |
{ |
330 | |
try |
331 | |
{ |
332 | 0 | FileUtils.deleteDirectory( checkoutDirectory ); |
333 | |
} |
334 | 0 | catch ( IOException e ) |
335 | |
{ |
336 | 0 | logError( e.getMessage() ); |
337 | |
|
338 | 0 | throw new MojoExecutionException( "Unable to remove old checkout directory: " + e.getMessage(), e ); |
339 | 0 | } |
340 | |
} |
341 | |
|
342 | 0 | boolean forceCheckout = false; |
343 | |
|
344 | 0 | if ( !checkoutDirectory.exists() ) |
345 | |
|
346 | |
{ |
347 | 0 | if ( tryUpdate ) |
348 | |
{ |
349 | 0 | logInfo( "tryUpdate is configured but no local copy currently available so forcing checkout" ); |
350 | |
} |
351 | 0 | checkoutDirectory.mkdirs(); |
352 | 0 | forceCheckout = true; |
353 | |
} |
354 | |
|
355 | |
ScmResult scmResult; |
356 | |
|
357 | |
try |
358 | |
|
359 | |
{ |
360 | 0 | ScmFileSet fileSet = new ScmFileSet( checkoutDirectory, includes, excludes ); |
361 | 0 | if ( tryUpdate && !forceCheckout ) |
362 | |
{ |
363 | 0 | scmResult = scmProvider.update( scmRepository, fileSet ); |
364 | |
} |
365 | |
else |
366 | |
{ |
367 | 0 | if ( scmBranch == null ) |
368 | |
{ |
369 | 0 | scmResult = scmProvider.checkOut( scmRepository, fileSet ); |
370 | |
} |
371 | |
else |
372 | |
{ |
373 | |
|
374 | 0 | ScmBranch scmBranch = new ScmBranch( this.scmBranch ); |
375 | 0 | scmResult = scmProvider.checkOut( scmRepository, fileSet, scmBranch ); |
376 | |
} |
377 | |
} |
378 | |
} |
379 | |
|
380 | 0 | catch ( ScmException e ) |
381 | |
{ |
382 | 0 | logError( e.getMessage() ); |
383 | |
|
384 | 0 | throw new MojoExecutionException( "An error is occurred in the checkout process: " + e.getMessage(), e ); |
385 | |
} |
386 | |
|
387 | 0 | catch ( IOException e ) |
388 | |
{ |
389 | 0 | logError( e.getMessage() ); |
390 | |
|
391 | 0 | throw new MojoExecutionException( "An error is occurred in the checkout process: " + e.getMessage(), e ); |
392 | 0 | } |
393 | |
|
394 | 0 | if ( !scmResult.isSuccess() ) |
395 | |
|
396 | |
{ |
397 | 0 | logError( scmResult.getProviderMessage() ); |
398 | |
|
399 | 0 | throw new MojoExecutionException( |
400 | |
"Unable to checkout from SCM" + "\nProvider message:\n" + scmResult.getProviderMessage() |
401 | |
+ "\nCommand output:\n" + scmResult.getCommandOutput() ); |
402 | |
} |
403 | |
|
404 | 0 | } |
405 | |
|
406 | |
public void execute() |
407 | |
throws MojoExecutionException, MojoFailureException |
408 | |
{ |
409 | |
|
410 | |
try |
411 | |
{ |
412 | 0 | setupScm(); |
413 | |
} |
414 | 0 | catch ( ScmRepositoryException e ) |
415 | |
{ |
416 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
417 | |
} |
418 | 0 | catch ( NoSuchScmProviderException e ) |
419 | |
{ |
420 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
421 | 0 | } |
422 | |
|
423 | 0 | boolean tmpCheckout = false; |
424 | |
|
425 | 0 | if ( checkoutDirectory.getPath().contains( "${project." ) ) |
426 | |
{ |
427 | |
try |
428 | |
{ |
429 | 0 | tmpCheckout = true; |
430 | 0 | checkoutDirectory = File.createTempFile( "maven-scm-publish", ".checkout" ); |
431 | 0 | checkoutDirectory.delete(); |
432 | 0 | checkoutDirectory.mkdir(); |
433 | |
} |
434 | 0 | catch ( IOException ioe ) |
435 | |
{ |
436 | 0 | throw new MojoExecutionException( ioe.getMessage(), ioe ); |
437 | 0 | } |
438 | |
} |
439 | |
|
440 | |
try |
441 | |
{ |
442 | 0 | scmPublishExecute(); |
443 | |
} |
444 | |
finally |
445 | |
{ |
446 | 0 | if ( tmpCheckout ) |
447 | |
{ |
448 | 0 | FileUtils.deleteQuietly( checkoutDirectory ); |
449 | |
} |
450 | |
} |
451 | 0 | } |
452 | |
|
453 | |
public abstract void scmPublishExecute() |
454 | |
throws MojoExecutionException, MojoFailureException; |
455 | |
} |