1 package org.apache.maven.plugins.site.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.manager.WagonManager;
23 import org.apache.maven.doxia.site.decoration.inheritance.URIPathDescriptor;
24 import org.apache.maven.execution.MavenExecutionRequest;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.model.DistributionManagement;
27 import org.apache.maven.model.Site;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.logging.Log;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.site.AbstractSiteMojo;
33 import org.apache.maven.plugins.site.deploy.wagon.BugFixedRepository;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.settings.Proxy;
36 import org.apache.maven.settings.Server;
37 import org.apache.maven.settings.Settings;
38 import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
39 import org.apache.maven.settings.crypto.SettingsDecrypter;
40 import org.apache.maven.settings.crypto.SettingsDecryptionResult;
41 import org.apache.maven.wagon.CommandExecutionException;
42 import org.apache.maven.wagon.CommandExecutor;
43 import org.apache.maven.wagon.ConnectionException;
44 import org.apache.maven.wagon.ResourceDoesNotExistException;
45 import org.apache.maven.wagon.TransferFailedException;
46 import org.apache.maven.wagon.UnsupportedProtocolException;
47 import org.apache.maven.wagon.Wagon;
48 import org.apache.maven.wagon.authentication.AuthenticationException;
49 import org.apache.maven.wagon.authentication.AuthenticationInfo;
50 import org.apache.maven.wagon.authorization.AuthorizationException;
51 import org.apache.maven.wagon.observers.Debug;
52 import org.apache.maven.wagon.proxy.ProxyInfo;
53 import org.apache.maven.wagon.repository.Repository;
54 import org.codehaus.classworlds.ClassRealm;
55 import org.codehaus.plexus.PlexusConstants;
56 import org.codehaus.plexus.PlexusContainer;
57 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
58 import org.codehaus.plexus.component.configurator.ComponentConfigurator;
59 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
60 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
61 import org.codehaus.plexus.configuration.PlexusConfiguration;
62 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
63 import org.codehaus.plexus.context.Context;
64 import org.codehaus.plexus.context.ContextException;
65 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
66 import org.codehaus.plexus.util.StringUtils;
67 import org.codehaus.plexus.util.xml.Xpp3Dom;
68
69 import java.io.File;
70 import java.lang.reflect.Method;
71 import java.net.MalformedURLException;
72 import java.net.URL;
73 import java.util.List;
74 import java.util.Locale;
75 import java.util.Set;
76
77
78
79
80
81
82
83
84 public abstract class AbstractDeployMojo
85 extends AbstractSiteMojo
86 implements Contextualizable
87 {
88
89
90
91
92
93 @Parameter( alias = "outputDirectory", defaultValue = "${project.reporting.outputDirectory}", required = true )
94 private File inputDirectory;
95
96
97
98
99
100
101
102 @Parameter( property = "maven.site.chmod", defaultValue = "true" )
103 private boolean chmod;
104
105
106
107
108
109
110
111 @Parameter( property = "maven.site.chmod.mode", defaultValue = "g+w,a+rX" )
112 private String chmodMode;
113
114
115
116
117
118
119
120 @Parameter( property = "maven.site.chmod.options", defaultValue = "-Rf" )
121 private String chmodOptions;
122
123
124
125
126
127
128 @Parameter( property = "maven.site.deploy.skip", defaultValue = "false" )
129 private boolean skipDeploy;
130
131
132
133 @Component
134 private WagonManager wagonManager;
135
136
137
138
139 @Parameter( defaultValue = "${settings}", readonly = true )
140 private Settings settings;
141
142
143
144
145 @Parameter( defaultValue = "${session}", readonly = true )
146 protected MavenSession mavenSession;
147
148 private String topDistributionManagementSiteUrl;
149
150 private Site deploySite;
151
152 private PlexusContainer container;
153
154
155
156
157 public void execute()
158 throws MojoExecutionException
159 {
160 if ( skip && isDeploy() )
161 {
162 getLog().info( "maven.site.skip = true: Skipping site deployment" );
163 return;
164 }
165
166 if ( skipDeploy && isDeploy() )
167 {
168 getLog().info( "maven.site.deploy.skip = true: Skipping site deployment" );
169 return;
170 }
171
172 deployTo( new BugFixedRepository( getDeploySite().getId(), getDeploySite().getUrl() ) );
173 }
174
175
176
177
178
179
180
181
182 protected static String appendSlash( final String url )
183 {
184 if ( url.endsWith( "/" ) )
185 {
186 return url;
187 }
188 else
189 {
190 return url + "/";
191 }
192 }
193
194
195
196
197
198
199
200 protected abstract boolean isDeploy();
201
202
203
204
205
206
207
208
209
210
211 protected String getTopDistributionManagementSiteUrl()
212 throws MojoExecutionException
213 {
214 if ( topDistributionManagementSiteUrl == null )
215 {
216 topDistributionManagementSiteUrl = determineTopDistributionManagementSiteUrl();
217
218 if ( !isDeploy() )
219 {
220 getLog().debug( "distributionManagement.site.url relative path: " + getDeployModuleDirectory() );
221 }
222 }
223 return topDistributionManagementSiteUrl;
224 }
225
226 protected abstract String determineTopDistributionManagementSiteUrl()
227 throws MojoExecutionException;
228
229
230
231
232
233
234
235
236
237
238 protected Site getDeploySite()
239 throws MojoExecutionException
240 {
241 if ( deploySite == null )
242 {
243 deploySite = determineDeploySite();
244 }
245 return deploySite;
246 }
247
248 protected abstract Site determineDeploySite()
249 throws MojoExecutionException;
250
251
252
253
254
255
256
257 protected String getDeployModuleDirectory()
258 throws MojoExecutionException
259 {
260 String to = getSite( project ).getUrl();
261
262 getLog().debug( "Mapping url source calculation: " );
263 String from = getTopDistributionManagementSiteUrl();
264
265 String relative = siteTool.getRelativePath( to, from );
266
267
268
269 relative = relative.replace( '\\', '/' );
270
271 return ( "".equals( relative ) ) ? "./" : relative;
272 }
273
274
275
276
277
278
279
280
281
282
283 private void deployTo( final Repository repository )
284 throws MojoExecutionException
285 {
286 if ( !inputDirectory.exists() )
287 {
288 throw new MojoExecutionException( "The site does not exist, please run site:site first" );
289 }
290
291 if ( getLog().isDebugEnabled() )
292 {
293 getLog().debug( "Deploying to '" + repository.getUrl() + "',\n Using credentials from server id '"
294 + repository.getId() + "'" );
295 }
296
297 deploy( inputDirectory, repository );
298 }
299
300 private void deploy( final File directory, final Repository repository )
301 throws MojoExecutionException
302 {
303
304 final Wagon wagon = getWagon( repository, wagonManager );
305
306 try
307 {
308 configureWagon( wagon, repository.getId(), settings, container, getLog() );
309 }
310 catch ( TransferFailedException e )
311 {
312 throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
313 }
314
315 try
316 {
317 final ProxyInfo proxyInfo;
318 if ( !isMaven3OrMore() )
319 {
320 proxyInfo = getProxyInfo( repository, wagonManager );
321 }
322 else
323 {
324 try
325 {
326
327
328
329 SettingsDecrypter settingsDecrypter =
330 (SettingsDecrypter) container.lookup( SettingsDecrypter.class );
331
332 proxyInfo = getProxy( repository, settingsDecrypter );
333 }
334 catch ( ComponentLookupException cle )
335 {
336 throw new MojoExecutionException( "Unable to lookup SettingsDecrypter: " + cle.getMessage(), cle );
337 }
338 }
339
340 push( directory, repository, wagon, proxyInfo, getLocales(), getDeployModuleDirectory() );
341
342 if ( chmod )
343 {
344 chmod( wagon, repository, chmodOptions, chmodMode );
345 }
346 }
347 finally
348 {
349 try
350 {
351 wagon.disconnect();
352 }
353 catch ( ConnectionException e )
354 {
355 getLog().error( "Error disconnecting wagon - ignored", e );
356 }
357 }
358 }
359
360 private Wagon getWagon( final Repository repository, final WagonManager manager )
361 throws MojoExecutionException
362 {
363 final Wagon wagon;
364
365 try
366 {
367 wagon = manager.getWagon( repository );
368 }
369 catch ( UnsupportedProtocolException e )
370 {
371 String shortMessage = "Unsupported protocol: '" + repository.getProtocol() + "' for site deployment to "
372 + "distributionManagement.site.url=" + repository.getUrl() + ".";
373 String longMessage =
374 "\n" + shortMessage + "\n" + "Currently supported protocols are: " + getSupportedProtocols() + ".\n"
375 + " Protocols may be added through wagon providers.\n" + " For more information, see "
376 + "http://maven.apache.org/plugins/maven-site-plugin/examples/adding-deploy-protocol.html";
377
378 getLog().error( longMessage );
379
380 throw new MojoExecutionException( shortMessage );
381 }
382 catch ( TransferFailedException e )
383 {
384 throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
385 }
386
387 if ( !wagon.supportsDirectoryCopy() )
388 {
389 throw new MojoExecutionException(
390 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
391 }
392
393 return wagon;
394 }
395
396 private String getSupportedProtocols()
397 {
398 try
399 {
400 Set<String> protocols = container.lookupMap( Wagon.class ).keySet();
401
402 return StringUtils.join( protocols.iterator(), ", " );
403 }
404 catch ( ComponentLookupException e )
405 {
406
407 getLog().error( e );
408 }
409 return "";
410 }
411
412 private void push( final File inputDirectory, final Repository repository, final Wagon wagon,
413 final ProxyInfo proxyInfo, final List<Locale> localesList, final String relativeDir )
414 throws MojoExecutionException
415 {
416 AuthenticationInfo authenticationInfo = wagonManager.getAuthenticationInfo( repository.getId() );
417 getLog().debug( "authenticationInfo with id '" + repository.getId() + "': "
418 + ( ( authenticationInfo == null ) ? "-" : authenticationInfo.getUserName() ) );
419
420 try
421 {
422 if ( getLog().isDebugEnabled() )
423 {
424 Debug debug = new Debug();
425
426 wagon.addSessionListener( debug );
427
428 wagon.addTransferListener( debug );
429 }
430
431 if ( proxyInfo != null )
432 {
433 getLog().debug( "connect with proxyInfo" );
434 wagon.connect( repository, authenticationInfo, proxyInfo );
435 }
436 else if ( proxyInfo == null && authenticationInfo != null )
437 {
438 getLog().debug( "connect with authenticationInfo and without proxyInfo" );
439 wagon.connect( repository, authenticationInfo );
440 }
441 else
442 {
443 getLog().debug( "connect without authenticationInfo and without proxyInfo" );
444 wagon.connect( repository );
445 }
446
447 getLog().info( "Pushing " + inputDirectory );
448
449
450 final String defaultLocale = localesList.get( 0 ).getLanguage();
451
452 for ( Locale locale : localesList )
453 {
454 if ( locale.getLanguage().equals( defaultLocale ) )
455 {
456
457
458 getLog().info( " >>> to " + appendSlash( repository.getUrl() ) + relativeDir );
459
460 wagon.putDirectory( inputDirectory, relativeDir );
461 }
462 else
463 {
464 getLog().info( " >>> to " + appendSlash( repository.getUrl() ) + locale.getLanguage() + "/"
465 + relativeDir );
466
467 wagon.putDirectory( new File( inputDirectory, locale.getLanguage() ),
468 locale.getLanguage() + "/" + relativeDir );
469 }
470 }
471 }
472 catch ( ResourceDoesNotExistException | TransferFailedException | AuthorizationException | ConnectionException
473 | AuthenticationException e )
474 {
475 throw new MojoExecutionException( "Error uploading site", e );
476 }
477 }
478
479 private static void chmod( final Wagon wagon, final Repository repository, final String chmodOptions,
480 final String chmodMode )
481 throws MojoExecutionException
482 {
483 try
484 {
485 if ( wagon instanceof CommandExecutor )
486 {
487 CommandExecutor exec = (CommandExecutor) wagon;
488 exec.executeCommand( "chmod " + chmodOptions + " " + chmodMode + " " + repository.getBasedir() );
489 }
490
491 }
492 catch ( CommandExecutionException e )
493 {
494 throw new MojoExecutionException( "Error uploading site", e );
495 }
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516 public static ProxyInfo getProxyInfo( Repository repository, WagonManager wagonManager )
517 {
518 ProxyInfo proxyInfo = wagonManager.getProxy( repository.getProtocol() );
519
520 if ( proxyInfo == null )
521 {
522 return null;
523 }
524
525 String host = repository.getHost();
526 String nonProxyHostsAsString = proxyInfo.getNonProxyHosts();
527 for ( String nonProxyHost : StringUtils.split( nonProxyHostsAsString, ",;|" ) )
528 {
529 if ( StringUtils.contains( nonProxyHost, "*" ) )
530 {
531
532 final int pos = nonProxyHost.indexOf( '*' );
533 String nonProxyHostPrefix = nonProxyHost.substring( 0, pos );
534 String nonProxyHostSuffix = nonProxyHost.substring( pos + 1 );
535
536 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
537 && StringUtils.isEmpty( nonProxyHostSuffix ) )
538 {
539 return null;
540 }
541
542 if ( StringUtils.isEmpty( nonProxyHostPrefix ) && StringUtils.isNotEmpty( nonProxyHostSuffix )
543 && host.endsWith( nonProxyHostSuffix ) )
544 {
545 return null;
546 }
547
548 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
549 && StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) )
550 {
551 return null;
552 }
553 }
554 else if ( host.equals( nonProxyHost ) )
555 {
556 return null;
557 }
558 }
559 return proxyInfo;
560 }
561
562
563
564
565
566
567
568
569 private ProxyInfo getProxy( Repository repository, SettingsDecrypter settingsDecrypter )
570 {
571 String protocol = repository.getProtocol();
572 String url = repository.getUrl();
573
574 getLog().debug( "repository protocol " + protocol );
575
576 String originalProtocol = protocol;
577
578
579
580
581 if ( StringUtils.equalsIgnoreCase( "dav", protocol ) && url.startsWith( "dav:" ) )
582 {
583 url = url.substring( 4 );
584 if ( url.startsWith( "http" ) )
585 {
586 try
587 {
588 URL urlSite = new URL( url );
589 protocol = urlSite.getProtocol();
590 getLog().debug( "found dav protocol so transform to real transport protocol " + protocol );
591 }
592 catch ( MalformedURLException e )
593 {
594 getLog().warn( "fail to build URL with " + url );
595 }
596
597 }
598 }
599 else
600 {
601 getLog().debug( "getProxy 'protocol': " + protocol );
602 }
603 if ( mavenSession != null && protocol != null )
604 {
605 MavenExecutionRequest request = mavenSession.getRequest();
606
607 if ( request != null )
608 {
609 List<Proxy> proxies = request.getProxies();
610
611 if ( proxies != null )
612 {
613 for ( Proxy proxy : proxies )
614 {
615 if ( proxy.isActive() && ( protocol.equalsIgnoreCase( proxy.getProtocol() )
616 || originalProtocol.equalsIgnoreCase( proxy.getProtocol() ) ) )
617 {
618 SettingsDecryptionResult result =
619 settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( proxy ) );
620 proxy = result.getProxy();
621
622 ProxyInfo proxyInfo = new ProxyInfo();
623 proxyInfo.setHost( proxy.getHost() );
624
625
626 proxyInfo.setType( protocol );
627 proxyInfo.setPort( proxy.getPort() );
628 proxyInfo.setNonProxyHosts( proxy.getNonProxyHosts() );
629 proxyInfo.setUserName( proxy.getUsername() );
630 proxyInfo.setPassword( proxy.getPassword() );
631
632 getLog().debug( "found proxyInfo "
633 + ( "host:port " + proxyInfo.getHost() + ":" + proxyInfo.getPort()
634 + ", " + proxyInfo.getUserName() ) );
635
636 return proxyInfo;
637 }
638 }
639 }
640 }
641 }
642 getLog().debug( "getProxy 'protocol': " + protocol + " no ProxyInfo found" );
643 return null;
644 }
645
646
647
648
649
650
651
652
653
654
655
656
657 private static void configureWagon( Wagon wagon, String repositoryId, Settings settings, PlexusContainer container,
658 Log log )
659 throws TransferFailedException
660 {
661 log.debug( " configureWagon " );
662
663
664 for ( Server server : settings.getServers() )
665 {
666 String id = server.getId();
667
668 log.debug( "configureWagon server " + id );
669
670 if ( id != null && id.equals( repositoryId ) && ( server.getConfiguration() != null ) )
671 {
672 final PlexusConfiguration plexusConf =
673 new XmlPlexusConfiguration( (Xpp3Dom) server.getConfiguration() );
674
675 ComponentConfigurator componentConfigurator = null;
676 try
677 {
678 componentConfigurator =
679 (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE, "basic" );
680 if ( isMaven3OrMore() )
681 {
682 componentConfigurator.configureComponent( wagon, plexusConf,
683 container.getContainerRealm() );
684 }
685 else
686 {
687 configureWagonWithMaven2( componentConfigurator, wagon, plexusConf, container );
688 }
689 }
690 catch ( final ComponentLookupException e )
691 {
692 throw new TransferFailedException(
693 "While configuring wagon for \'" + repositoryId + "\': Unable to lookup wagon configurator."
694 + " Wagon configuration cannot be applied.", e );
695 }
696 catch ( ComponentConfigurationException e )
697 {
698 throw new TransferFailedException( "While configuring wagon for \'" + repositoryId
699 + "\': Unable to apply wagon configuration.", e );
700 }
701 finally
702 {
703 if ( componentConfigurator != null )
704 {
705 try
706 {
707 container.release( componentConfigurator );
708 }
709 catch ( ComponentLifecycleException e )
710 {
711 log.error( "Problem releasing configurator - ignoring: " + e.getMessage() );
712 }
713 }
714 }
715 }
716 }
717 }
718
719 private static void configureWagonWithMaven2( ComponentConfigurator componentConfigurator, Wagon wagon,
720 PlexusConfiguration plexusConf, PlexusContainer container )
721 throws ComponentConfigurationException
722 {
723
724
725
726
727 try
728 {
729 Method methodContainerRealm = container.getClass().getMethod( "getContainerRealm" );
730 ClassRealm realm = (ClassRealm) methodContainerRealm.invoke( container );
731
732 Method methodConfigure = componentConfigurator.getClass().getMethod( "configureComponent",
733 new Class[]{ Object.class,
734 PlexusConfiguration.class,
735 ClassRealm.class } );
736
737 methodConfigure.invoke( componentConfigurator, wagon, plexusConf, realm );
738 }
739 catch ( Exception e )
740 {
741 throw new ComponentConfigurationException(
742 "Failed to configure wagon component for a Maven2 use " + e.getMessage(), e );
743 }
744 }
745
746
747
748
749 public void contextualize( Context context )
750 throws ContextException
751 {
752 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
753 }
754
755
756
757
758
759
760
761
762
763 protected static Site getSite( final MavenProject project )
764 throws MojoExecutionException
765 {
766 final DistributionManagement distributionManagement = project.getDistributionManagement();
767
768 if ( distributionManagement == null )
769 {
770 throw new MojoExecutionException( "Missing distribution management in project " + getFullName( project ) );
771 }
772
773 final Site site = distributionManagement.getSite();
774
775 if ( site == null )
776 {
777 throw new MojoExecutionException( "Missing site information in the distribution management of the project "
778 + getFullName( project ) );
779 }
780
781 if ( site.getUrl() == null || site.getId() == null )
782 {
783 throw new MojoExecutionException( "Missing site data: specify url and id for project "
784 + getFullName( project ) );
785 }
786
787 return site;
788 }
789
790 private static String getFullName( MavenProject project )
791 {
792 return project.getName() + " (" + project.getGroupId() + ':' + project.getArtifactId() + ':'
793 + project.getVersion() + ')';
794 }
795
796
797
798
799
800
801
802
803
804
805
806
807
808 protected MavenProject getTopLevelProject( MavenProject project )
809 throws MojoExecutionException
810 {
811 Site site = getSite( project );
812
813 MavenProject parent = project;
814
815 while ( parent.getParent() != null )
816 {
817 MavenProject oldProject = parent;
818
819 parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
820
821 Site oldSite = site;
822
823 try
824 {
825 site = getSite( parent );
826 }
827 catch ( MojoExecutionException e )
828 {
829 return oldProject;
830 }
831
832
833 URIPathDescriptor siteURI = new URIPathDescriptor( URIEncoder.encodeURI( site.getUrl() ), "" );
834 URIPathDescriptor oldSiteURI = new URIPathDescriptor( URIEncoder.encodeURI( oldSite.getUrl() ), "" );
835
836 if ( !siteURI.sameSite( oldSiteURI.getBaseURI() ) )
837 {
838 return oldProject;
839 }
840 }
841
842 return parent;
843 }
844
845 private static class URIEncoder
846 {
847 private static final String MARK = "-_.!~*'()";
848 private static final String RESERVED = ";/?:@&=+$,";
849
850 public static String encodeURI( final String uriString )
851 {
852 final char[] chars = uriString.toCharArray();
853 final StringBuilder uri = new StringBuilder( chars.length );
854
855
856 if ( uriString.startsWith( "dav:http" ) )
857 {
858
859 chars[3] = '-';
860 }
861
862 for ( char c : chars )
863 {
864 if ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' )
865 || MARK.indexOf( c ) != -1 || RESERVED.indexOf( c ) != -1 )
866 {
867 uri.append( c );
868 }
869 else
870 {
871 uri.append( '%' );
872 uri.append( Integer.toHexString( (int) c ) );
873 }
874 }
875 return uri.toString();
876 }
877 }
878 }