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 | 0 | 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 | 0 | super.execute(); |
136 | |
|
137 | 0 | deployStagingSite(); |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
private void deployStagingSite() |
149 | |
throws MojoExecutionException, MojoFailureException |
150 | |
{ |
151 | 0 | stagingSiteURL = getStagingSiteURL( project, reactorProjects, stagingSiteURL ); |
152 | 0 | getLog().info( "Using this URL for staging: " + stagingSiteURL ); |
153 | |
|
154 | 0 | Repository repository = new Repository( stagingRepositoryId, stagingSiteURL ); |
155 | |
|
156 | |
Wagon wagon; |
157 | |
try |
158 | |
{ |
159 | 0 | wagon = wagonManager.getWagon( repository ); |
160 | 0 | SiteDeployMojo.configureWagon( wagon, stagingRepositoryId, settings, container, getLog() ); |
161 | |
} |
162 | 0 | catch ( UnsupportedProtocolException e ) |
163 | |
{ |
164 | 0 | throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e ); |
165 | |
} |
166 | 0 | catch ( WagonConfigurationException e ) |
167 | |
{ |
168 | 0 | throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e ); |
169 | 0 | } |
170 | |
|
171 | 0 | if ( !wagon.supportsDirectoryCopy() ) |
172 | |
{ |
173 | 0 | throw new MojoExecutionException( |
174 | |
"Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" ); |
175 | |
} |
176 | |
|
177 | |
try |
178 | |
{ |
179 | 0 | Debug debug = new Debug(); |
180 | |
|
181 | 0 | wagon.addSessionListener( debug ); |
182 | |
|
183 | 0 | wagon.addTransferListener( debug ); |
184 | |
|
185 | 0 | ProxyInfo proxyInfo = SiteDeployMojo.getProxyInfo( repository, wagonManager ); |
186 | 0 | if ( proxyInfo != null ) |
187 | |
{ |
188 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ), proxyInfo ); |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ) ); |
193 | |
} |
194 | |
|
195 | 0 | wagon.putDirectory( new File( stagingDirectory, getStructure( project, false ) ), "." ); |
196 | |
|
197 | 0 | if ( chmod && wagon instanceof CommandExecutor ) |
198 | |
{ |
199 | 0 | CommandExecutor exec = (CommandExecutor) wagon; |
200 | 0 | exec.executeCommand( "chmod " + chmodOptions + " " + chmodMode + " " + repository.getBasedir() ); |
201 | |
} |
202 | |
} |
203 | 0 | catch ( ResourceDoesNotExistException e ) |
204 | |
{ |
205 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
206 | |
} |
207 | 0 | catch ( TransferFailedException e ) |
208 | |
{ |
209 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
210 | |
} |
211 | 0 | catch ( AuthorizationException e ) |
212 | |
{ |
213 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
214 | |
} |
215 | 0 | catch ( ConnectionException e ) |
216 | |
{ |
217 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
218 | |
} |
219 | 0 | catch ( AuthenticationException e ) |
220 | |
{ |
221 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
222 | |
} |
223 | 0 | catch ( CommandExecutionException e ) |
224 | |
{ |
225 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
226 | |
} |
227 | |
finally |
228 | |
{ |
229 | 0 | try |
230 | |
{ |
231 | 0 | wagon.disconnect(); |
232 | |
} |
233 | 0 | catch ( ConnectionException e ) |
234 | |
{ |
235 | 0 | getLog().error( "Error disconnecting wagon - ignored", e ); |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |
239 | |
|
240 | |
public void contextualize( Context context ) |
241 | |
throws ContextException |
242 | |
{ |
243 | 0 | container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
244 | 0 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
protected String getStagingSiteURL( MavenProject currentProject, List<MavenProject> reactorProjects, |
255 | |
String usersStagingSiteURL ) |
256 | |
{ |
257 | 0 | String topLevelURL = null; |
258 | 0 | String relative = ""; |
259 | |
|
260 | |
|
261 | 0 | if ( usersStagingSiteURL != null ) |
262 | |
{ |
263 | 0 | getLog().debug( "stagingSiteURL specified by the user." ); |
264 | 0 | topLevelURL = usersStagingSiteURL; |
265 | |
} |
266 | 0 | getLog().debug( "stagingSiteURL NOT specified by the user." ); |
267 | |
|
268 | |
|
269 | 0 | MavenProject topLevelProject = getTopLevelProject( reactorProjects ); |
270 | |
|
271 | |
|
272 | |
|
273 | 0 | if ( topLevelProject == null ) |
274 | |
{ |
275 | 0 | if ( topLevelURL == null ) |
276 | |
{ |
277 | |
|
278 | |
|
279 | 0 | getLog().debug( "No top level project found in the reactor, using the current project." ); |
280 | 0 | topLevelURL = |
281 | |
currentProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY; |
282 | |
} |
283 | |
} |
284 | |
else |
285 | |
{ |
286 | |
|
287 | 0 | relative = "/" + siteTool.getRelativePath( currentProject.getDistributionManagement().getSite().getUrl(), |
288 | |
topLevelProject.getDistributionManagement().getSite().getUrl() ); |
289 | |
|
290 | |
|
291 | 0 | relative = relative.replace( '\\', '/' ); |
292 | |
|
293 | 0 | if ( topLevelURL == null ) |
294 | |
{ |
295 | |
|
296 | |
|
297 | 0 | getLog().debug( "Using the top level project found in the reactor." ); |
298 | 0 | topLevelURL = |
299 | |
topLevelProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY; |
300 | |
} |
301 | |
} |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | 0 | return topLevelURL + relative; |
308 | |
} |
309 | |
} |