1 package org.apache.maven.plugins.site;
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.lang.StringUtils;
23 import org.apache.maven.artifact.manager.WagonConfigurationException;
24 import org.apache.maven.artifact.manager.WagonManager;
25 import org.apache.maven.model.DistributionManagement;
26 import org.apache.maven.model.Site;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.logging.Log;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.settings.Server;
32 import org.apache.maven.settings.Settings;
33 import org.apache.maven.wagon.CommandExecutionException;
34 import org.apache.maven.wagon.CommandExecutor;
35 import org.apache.maven.wagon.ConnectionException;
36 import org.apache.maven.wagon.ResourceDoesNotExistException;
37 import org.apache.maven.wagon.TransferFailedException;
38 import org.apache.maven.wagon.UnsupportedProtocolException;
39 import org.apache.maven.wagon.Wagon;
40 import org.apache.maven.wagon.authentication.AuthenticationException;
41 import org.apache.maven.wagon.authorization.AuthorizationException;
42 import org.apache.maven.wagon.observers.Debug;
43 import org.apache.maven.wagon.proxy.ProxyInfo;
44 import org.apache.maven.wagon.repository.Repository;
45 import org.codehaus.plexus.PlexusConstants;
46 import org.codehaus.plexus.PlexusContainer;
47 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
48 import org.codehaus.plexus.component.configurator.ComponentConfigurator;
49 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
50 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
51 import org.codehaus.plexus.configuration.PlexusConfiguration;
52 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
53 import org.codehaus.plexus.context.Context;
54 import org.codehaus.plexus.context.ContextException;
55 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
56 import org.codehaus.plexus.util.xml.Xpp3Dom;
57
58 import java.io.File;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class SiteDeployMojo
77 extends AbstractMojo implements Contextualizable
78 {
79
80
81
82
83
84
85 private File inputDirectory;
86
87
88
89
90
91
92 private MavenProject project;
93
94
95
96
97 private WagonManager wagonManager;
98
99
100
101
102
103
104
105
106 private Settings settings;
107
108 private PlexusContainer container;
109
110 public void execute()
111 throws MojoExecutionException
112 {
113 if ( !inputDirectory.exists() )
114 {
115 throw new MojoExecutionException( "The site does not exist, please run site:site first" );
116 }
117
118 DistributionManagement distributionManagement = project.getDistributionManagement();
119
120 if ( distributionManagement == null )
121 {
122 throw new MojoExecutionException( "Missing distribution management information in the project" );
123 }
124
125 Site site = distributionManagement.getSite();
126
127 if ( site == null )
128 {
129 throw new MojoExecutionException(
130 "Missing site information in the distribution management element in the project.." );
131 }
132
133 String url = site.getUrl();
134
135 String id = site.getId();
136
137 if ( url == null )
138 {
139 throw new MojoExecutionException( "The URL to the site is missing in the project descriptor." );
140 }
141 getLog().debug( "The site will be deployed to '" + url + "'");
142
143 Repository repository = new Repository( id, url );
144
145
146
147 Wagon wagon;
148
149 try
150 {
151 wagon = wagonManager.getWagon( repository );
152 configureWagon( wagon, repository.getId(), settings, container, getLog() );
153 }
154 catch ( UnsupportedProtocolException e )
155 {
156 throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e );
157 }
158 catch ( WagonConfigurationException e )
159 {
160 throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
161 }
162
163 if ( !wagon.supportsDirectoryCopy() )
164 {
165 throw new MojoExecutionException(
166 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
167 }
168
169 try
170 {
171 Debug debug = new Debug();
172
173 wagon.addSessionListener( debug );
174
175 wagon.addTransferListener( debug );
176
177 ProxyInfo proxyInfo = getProxyInfo( repository, wagonManager );
178 if ( proxyInfo != null )
179 {
180 wagon.connect( repository, wagonManager.getAuthenticationInfo( id ), proxyInfo );
181 }
182 else
183 {
184 wagon.connect( repository, wagonManager.getAuthenticationInfo( id ) );
185 }
186
187 wagon.putDirectory( inputDirectory, "." );
188
189
190
191 if ( wagon instanceof CommandExecutor )
192 {
193 CommandExecutor exec = (CommandExecutor) wagon;
194 exec.executeCommand( "chmod -Rf g+w,a+rX " + repository.getBasedir() );
195 }
196 }
197 catch ( ResourceDoesNotExistException e )
198 {
199 throw new MojoExecutionException( "Error uploading site", e );
200 }
201 catch ( TransferFailedException e )
202 {
203 throw new MojoExecutionException( "Error uploading site", e );
204 }
205 catch ( AuthorizationException e )
206 {
207 throw new MojoExecutionException( "Error uploading site", e );
208 }
209 catch ( ConnectionException e )
210 {
211 throw new MojoExecutionException( "Error uploading site", e );
212 }
213 catch ( AuthenticationException e )
214 {
215 throw new MojoExecutionException( "Error uploading site", e );
216 }
217 catch ( CommandExecutionException e )
218 {
219 throw new MojoExecutionException( "Error uploading site", e );
220 }
221 finally
222 {
223 try
224 {
225 wagon.disconnect();
226 }
227 catch ( ConnectionException e )
228 {
229 getLog().error( "Error disconnecting wagon - ignored", e );
230 }
231 }
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 public static ProxyInfo getProxyInfo( Repository repository, WagonManager wagonManager )
251 {
252 ProxyInfo proxyInfo = wagonManager.getProxy( repository.getProtocol() );
253
254 if ( proxyInfo == null )
255 {
256 return null;
257 }
258
259 String host = repository.getHost();
260 String nonProxyHostsAsString = proxyInfo.getNonProxyHosts();
261 String[] nonProxyHosts = StringUtils.split( nonProxyHostsAsString, ",;|" );
262 for ( int i = 0; i < nonProxyHosts.length; i++ )
263 {
264 String nonProxyHost = nonProxyHosts[i];
265 if ( StringUtils.contains( nonProxyHost, "*" ) )
266 {
267
268 String nonProxyHostPrefix = StringUtils.substringBefore( nonProxyHost, "*" );
269 String nonProxyHostSuffix = StringUtils.substringAfter( nonProxyHost, "*" );
270
271 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
272 && StringUtils.isEmpty( nonProxyHostSuffix ) )
273 {
274 return null;
275 }
276
277 if ( StringUtils.isEmpty( nonProxyHostPrefix )
278 && StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) )
279 {
280 return null;
281 }
282
283 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
284 && StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) )
285 {
286 return null;
287 }
288 }
289 else if ( host.equals( nonProxyHost ) )
290 {
291 return null;
292 }
293 }
294 return proxyInfo;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308 static void configureWagon( Wagon wagon, String repositoryId, Settings settings, PlexusContainer container,
309 Log log )
310 throws WagonConfigurationException
311 {
312
313 for ( int i = 0; i < settings.getServers().size(); i++ )
314 {
315 Server server = (Server) settings.getServers().get( i );
316 String id = server.getId();
317 if ( id != null && id.equals( repositoryId ) )
318 {
319 if ( server.getConfiguration() != null )
320 {
321 final PlexusConfiguration plexusConf =
322 new XmlPlexusConfiguration( (Xpp3Dom) server.getConfiguration() );
323
324 ComponentConfigurator componentConfigurator = null;
325 try
326 {
327 componentConfigurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE );
328 componentConfigurator.configureComponent( wagon, plexusConf, container.getContainerRealm() );
329 }
330 catch ( final ComponentLookupException e )
331 {
332 throw new WagonConfigurationException( repositoryId, "Unable to lookup wagon configurator."
333 + " Wagon configuration cannot be applied.", e );
334 }
335 catch ( ComponentConfigurationException e )
336 {
337 throw new WagonConfigurationException( repositoryId, "Unable to apply wagon configuration.",
338 e );
339 }
340 finally
341 {
342 if ( componentConfigurator != null )
343 {
344 try
345 {
346 container.release( componentConfigurator );
347 }
348 catch ( ComponentLifecycleException e )
349 {
350 log.error( "Problem releasing configurator - ignoring: " + e.getMessage() );
351 }
352 }
353 }
354
355 }
356
357 }
358 }
359 }
360
361 public void contextualize( Context context )
362 throws ContextException
363 {
364 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
365 }
366
367 }