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.maven.artifact.manager.WagonConfigurationException;
23 import org.apache.maven.artifact.manager.WagonManager;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.project.MavenProject;
27 import org.apache.maven.settings.Settings;
28 import org.apache.maven.wagon.CommandExecutionException;
29 import org.apache.maven.wagon.CommandExecutor;
30 import org.apache.maven.wagon.ConnectionException;
31 import org.apache.maven.wagon.ResourceDoesNotExistException;
32 import org.apache.maven.wagon.TransferFailedException;
33 import org.apache.maven.wagon.UnsupportedProtocolException;
34 import org.apache.maven.wagon.Wagon;
35 import org.apache.maven.wagon.authentication.AuthenticationException;
36 import org.apache.maven.wagon.authorization.AuthorizationException;
37 import org.apache.maven.wagon.observers.Debug;
38 import org.apache.maven.wagon.proxy.ProxyInfo;
39 import org.apache.maven.wagon.repository.Repository;
40 import org.codehaus.plexus.PlexusConstants;
41 import org.codehaus.plexus.PlexusContainer;
42 import org.codehaus.plexus.context.Context;
43 import org.codehaus.plexus.context.ContextException;
44 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
45
46 import java.io.File;
47 import java.util.List;
48
49
50
51
52
53
54
55
56
57
58
59
60 public class SiteStageDeployMojo
61 extends SiteStageMojo implements Contextualizable
62 {
63
64
65
66
67
68
69
70
71
72
73
74 private String stagingSiteURL;
75
76
77
78
79
80
81
82
83
84 private String stagingRepositoryId;
85
86
87
88
89
90
91
92
93 private boolean chmod;
94
95
96
97
98
99
100
101
102 private String chmodMode;
103
104
105
106
107
108
109
110
111 private String chmodOptions;
112
113
114
115
116 private WagonManager wagonManager;
117
118
119
120
121
122
123
124
125 private Settings settings;
126
127 private PlexusContainer container;
128
129
130
131
132 public void execute()
133 throws MojoExecutionException, MojoFailureException
134 {
135 super.execute();
136
137 deployStagingSite();
138 }
139
140
141
142
143
144
145
146
147
148 private void deployStagingSite()
149 throws MojoExecutionException, MojoFailureException
150 {
151 stagingSiteURL = getStagingSiteURL( project, reactorProjects, stagingSiteURL );
152 getLog().info( "Using this URL for staging: " + stagingSiteURL );
153
154 Repository repository = new Repository( stagingRepositoryId, stagingSiteURL );
155
156 Wagon wagon;
157 try
158 {
159 wagon = wagonManager.getWagon( repository );
160 SiteDeployMojo.configureWagon( wagon, stagingRepositoryId, settings, container, getLog() );
161 }
162 catch ( UnsupportedProtocolException e )
163 {
164 throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e );
165 }
166 catch ( WagonConfigurationException e )
167 {
168 throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
169 }
170
171 if ( !wagon.supportsDirectoryCopy() )
172 {
173 throw new MojoExecutionException(
174 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
175 }
176
177 try
178 {
179 Debug debug = new Debug();
180
181 wagon.addSessionListener( debug );
182
183 wagon.addTransferListener( debug );
184
185 ProxyInfo proxyInfo = SiteDeployMojo.getProxyInfo( repository, wagonManager );
186 if ( proxyInfo != null )
187 {
188 wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ), proxyInfo );
189 }
190 else
191 {
192 wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ) );
193 }
194
195 wagon.putDirectory( new File( stagingDirectory, getStructure( project, false ) ), "." );
196
197 if ( chmod && wagon instanceof CommandExecutor )
198 {
199 CommandExecutor exec = (CommandExecutor) wagon;
200 exec.executeCommand( "chmod " + chmodOptions + " " + chmodMode + " " + repository.getBasedir() );
201 }
202 }
203 catch ( ResourceDoesNotExistException e )
204 {
205 throw new MojoExecutionException( "Error uploading site", e );
206 }
207 catch ( TransferFailedException e )
208 {
209 throw new MojoExecutionException( "Error uploading site", e );
210 }
211 catch ( AuthorizationException e )
212 {
213 throw new MojoExecutionException( "Error uploading site", e );
214 }
215 catch ( ConnectionException e )
216 {
217 throw new MojoExecutionException( "Error uploading site", e );
218 }
219 catch ( AuthenticationException e )
220 {
221 throw new MojoExecutionException( "Error uploading site", e );
222 }
223 catch ( CommandExecutionException e )
224 {
225 throw new MojoExecutionException( "Error uploading site", e );
226 }
227 finally
228 {
229 try
230 {
231 wagon.disconnect();
232 }
233 catch ( ConnectionException e )
234 {
235 getLog().error( "Error disconnecting wagon - ignored", e );
236 }
237 }
238 }
239
240 public void contextualize( Context context )
241 throws ContextException
242 {
243 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
244 }
245
246
247
248
249
250
251
252
253
254 protected String getStagingSiteURL( MavenProject currentProject, List reactorProjects, String usersStagingSiteURL )
255 {
256 String topLevelURL = null;
257 String relative = "";
258
259
260 if ( usersStagingSiteURL != null )
261 {
262 getLog().debug( "stagingSiteURL specified by the user." );
263 topLevelURL = usersStagingSiteURL;
264 }
265 getLog().debug( "stagingSiteURL NOT specified by the user." );
266
267
268 MavenProject topLevelProject = getTopLevelProject( reactorProjects );
269
270
271
272 if ( topLevelProject == null )
273 {
274 if ( topLevelURL == null )
275 {
276
277
278 getLog().debug( "No top level project found in the reactor, using the current project." );
279 topLevelURL =
280 currentProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY;
281 }
282 }
283 else
284 {
285
286 relative = "/" + siteTool.getRelativePath( currentProject.getDistributionManagement().getSite().getUrl(),
287 topLevelProject.getDistributionManagement().getSite().getUrl() );
288
289
290 relative = relative.replace( '\\', '/' );
291
292 if ( topLevelURL == null )
293 {
294
295
296 getLog().debug( "Using the top level project found in the reactor." );
297 topLevelURL =
298 topLevelProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY;
299 }
300 }
301
302
303
304
305
306 return topLevelURL + relative;
307 }
308 }