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 | 0 | 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 | 0 | if ( !inputDirectory.exists() ) |
114 | |
{ |
115 | 0 | throw new MojoExecutionException( "The site does not exist, please run site:site first" ); |
116 | |
} |
117 | |
|
118 | 0 | DistributionManagement distributionManagement = project.getDistributionManagement(); |
119 | |
|
120 | 0 | if ( distributionManagement == null ) |
121 | |
{ |
122 | 0 | throw new MojoExecutionException( "Missing distribution management information in the project" ); |
123 | |
} |
124 | |
|
125 | 0 | Site site = distributionManagement.getSite(); |
126 | |
|
127 | 0 | if ( site == null ) |
128 | |
{ |
129 | 0 | throw new MojoExecutionException( |
130 | |
"Missing site information in the distribution management element in the project.." ); |
131 | |
} |
132 | |
|
133 | 0 | String url = site.getUrl(); |
134 | |
|
135 | 0 | String id = site.getId(); |
136 | |
|
137 | 0 | if ( url == null ) |
138 | |
{ |
139 | 0 | throw new MojoExecutionException( "The URL to the site is missing in the project descriptor." ); |
140 | |
} |
141 | 0 | getLog().debug( "The site will be deployed to '" + url + "'"); |
142 | |
|
143 | 0 | Repository repository = new Repository( id, url ); |
144 | |
|
145 | |
|
146 | |
|
147 | |
Wagon wagon; |
148 | |
|
149 | |
try |
150 | |
{ |
151 | 0 | wagon = wagonManager.getWagon( repository ); |
152 | 0 | configureWagon( wagon, repository.getId(), settings, container, getLog() ); |
153 | |
} |
154 | 0 | catch ( UnsupportedProtocolException e ) |
155 | |
{ |
156 | 0 | throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e ); |
157 | |
} |
158 | 0 | catch ( WagonConfigurationException e ) |
159 | |
{ |
160 | 0 | throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e ); |
161 | 0 | } |
162 | |
|
163 | 0 | if ( !wagon.supportsDirectoryCopy() ) |
164 | |
{ |
165 | 0 | throw new MojoExecutionException( |
166 | |
"Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" ); |
167 | |
} |
168 | |
|
169 | |
try |
170 | |
{ |
171 | 0 | Debug debug = new Debug(); |
172 | |
|
173 | 0 | wagon.addSessionListener( debug ); |
174 | |
|
175 | 0 | wagon.addTransferListener( debug ); |
176 | |
|
177 | 0 | ProxyInfo proxyInfo = getProxyInfo( repository, wagonManager ); |
178 | 0 | if ( proxyInfo != null ) |
179 | |
{ |
180 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( id ), proxyInfo ); |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( id ) ); |
185 | |
} |
186 | |
|
187 | 0 | wagon.putDirectory( inputDirectory, "." ); |
188 | |
|
189 | |
|
190 | |
|
191 | 0 | if ( wagon instanceof CommandExecutor ) |
192 | |
{ |
193 | 0 | CommandExecutor exec = (CommandExecutor) wagon; |
194 | 0 | exec.executeCommand( "chmod -Rf g+w,a+rX " + repository.getBasedir() ); |
195 | |
} |
196 | 0 | } |
197 | 0 | catch ( ResourceDoesNotExistException e ) |
198 | |
{ |
199 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
200 | |
} |
201 | 0 | catch ( TransferFailedException e ) |
202 | |
{ |
203 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
204 | |
} |
205 | 0 | catch ( AuthorizationException e ) |
206 | |
{ |
207 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
208 | |
} |
209 | 0 | catch ( ConnectionException e ) |
210 | |
{ |
211 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
212 | |
} |
213 | 0 | catch ( AuthenticationException e ) |
214 | |
{ |
215 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
216 | |
} |
217 | 0 | catch ( CommandExecutionException e ) |
218 | |
{ |
219 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
220 | |
} |
221 | |
finally |
222 | |
{ |
223 | 0 | try |
224 | |
{ |
225 | 0 | wagon.disconnect(); |
226 | |
} |
227 | 0 | catch ( ConnectionException e ) |
228 | |
{ |
229 | 0 | getLog().error( "Error disconnecting wagon - ignored", e ); |
230 | 0 | } |
231 | 0 | } |
232 | 0 | } |
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 | 22 | ProxyInfo proxyInfo = wagonManager.getProxy( repository.getProtocol() ); |
253 | |
|
254 | 22 | if ( proxyInfo == null ) |
255 | |
{ |
256 | 2 | return null; |
257 | |
} |
258 | |
|
259 | 20 | String host = repository.getHost(); |
260 | 20 | String nonProxyHostsAsString = proxyInfo.getNonProxyHosts(); |
261 | 20 | String[] nonProxyHosts = StringUtils.split( nonProxyHostsAsString, ",;|" ); |
262 | 38 | for ( int i = 0; i < nonProxyHosts.length; i++ ) |
263 | |
{ |
264 | 26 | String nonProxyHost = nonProxyHosts[i]; |
265 | 26 | if ( StringUtils.contains( nonProxyHost, "*" ) ) |
266 | |
{ |
267 | |
|
268 | 16 | String nonProxyHostPrefix = StringUtils.substringBefore( nonProxyHost, "*" ); |
269 | 16 | String nonProxyHostSuffix = StringUtils.substringAfter( nonProxyHost, "*" ); |
270 | |
|
271 | 16 | if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix ) |
272 | |
&& StringUtils.isEmpty( nonProxyHostSuffix ) ) |
273 | |
{ |
274 | 2 | return null; |
275 | |
} |
276 | |
|
277 | 14 | if ( StringUtils.isEmpty( nonProxyHostPrefix ) |
278 | |
&& StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) ) |
279 | |
{ |
280 | 2 | return null; |
281 | |
} |
282 | |
|
283 | 12 | if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix ) |
284 | |
&& StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) ) |
285 | |
{ |
286 | 2 | return null; |
287 | |
} |
288 | |
} |
289 | 10 | else if ( host.equals( nonProxyHost ) ) |
290 | |
{ |
291 | 2 | return null; |
292 | |
} |
293 | |
} |
294 | 12 | 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 | 0 | for ( int i = 0; i < settings.getServers().size(); i++ ) |
314 | |
{ |
315 | 0 | Server server = (Server) settings.getServers().get( i ); |
316 | 0 | String id = server.getId(); |
317 | 0 | if ( id != null && id.equals( repositoryId ) ) |
318 | |
{ |
319 | 0 | if ( server.getConfiguration() != null ) |
320 | |
{ |
321 | 0 | final PlexusConfiguration plexusConf = |
322 | |
new XmlPlexusConfiguration( (Xpp3Dom) server.getConfiguration() ); |
323 | |
|
324 | 0 | ComponentConfigurator componentConfigurator = null; |
325 | |
try |
326 | |
{ |
327 | 0 | componentConfigurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE ); |
328 | 0 | componentConfigurator.configureComponent( wagon, plexusConf, container.getContainerRealm() ); |
329 | 0 | } |
330 | 0 | catch ( final ComponentLookupException e ) |
331 | |
{ |
332 | 0 | throw new WagonConfigurationException( repositoryId, "Unable to lookup wagon configurator." |
333 | |
+ " Wagon configuration cannot be applied.", e ); |
334 | |
} |
335 | 0 | catch ( ComponentConfigurationException e ) |
336 | |
{ |
337 | 0 | throw new WagonConfigurationException( repositoryId, "Unable to apply wagon configuration.", |
338 | |
e ); |
339 | |
} |
340 | |
finally |
341 | |
{ |
342 | 0 | if ( componentConfigurator != null ) |
343 | |
{ |
344 | |
try |
345 | |
{ |
346 | 0 | container.release( componentConfigurator ); |
347 | |
} |
348 | 0 | catch ( ComponentLifecycleException e ) |
349 | |
{ |
350 | 0 | log.error( "Problem releasing configurator - ignoring: " + e.getMessage() ); |
351 | 0 | } |
352 | |
} |
353 | 0 | } |
354 | |
|
355 | |
} |
356 | |
|
357 | |
} |
358 | |
} |
359 | 0 | } |
360 | |
|
361 | |
public void contextualize( Context context ) |
362 | |
throws ContextException |
363 | |
{ |
364 | 0 | container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
365 | 0 | } |
366 | |
|
367 | |
} |