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 | |
private WagonManager wagonManager; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private Settings settings; |
99 | |
|
100 | |
private PlexusContainer container; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void execute() |
106 | |
throws MojoExecutionException, MojoFailureException |
107 | |
{ |
108 | 0 | super.execute(); |
109 | |
|
110 | 0 | deployStagingSite(); |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private void deployStagingSite() |
122 | |
throws MojoExecutionException, MojoFailureException |
123 | |
{ |
124 | 0 | stagingSiteURL = getStagingSiteURL( project, reactorProjects, stagingSiteURL ); |
125 | 0 | getLog().info( "Using this URL for staging: " + stagingSiteURL ); |
126 | |
|
127 | 0 | Repository repository = new Repository( stagingRepositoryId, stagingSiteURL ); |
128 | |
|
129 | |
Wagon wagon; |
130 | |
try |
131 | |
{ |
132 | 0 | wagon = wagonManager.getWagon( repository ); |
133 | 0 | SiteDeployMojo.configureWagon( wagon, stagingRepositoryId, settings, container, getLog() ); |
134 | |
} |
135 | 0 | catch ( UnsupportedProtocolException e ) |
136 | |
{ |
137 | 0 | throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e ); |
138 | |
} |
139 | 0 | catch ( WagonConfigurationException e ) |
140 | |
{ |
141 | 0 | throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e ); |
142 | 0 | } |
143 | |
|
144 | 0 | if ( !wagon.supportsDirectoryCopy() ) |
145 | |
{ |
146 | 0 | throw new MojoExecutionException( |
147 | |
"Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" ); |
148 | |
} |
149 | |
|
150 | |
try |
151 | |
{ |
152 | 0 | Debug debug = new Debug(); |
153 | |
|
154 | 0 | wagon.addSessionListener( debug ); |
155 | |
|
156 | 0 | wagon.addTransferListener( debug ); |
157 | |
|
158 | 0 | ProxyInfo proxyInfo = SiteDeployMojo.getProxyInfo( repository, wagonManager ); |
159 | 0 | if ( proxyInfo != null ) |
160 | |
{ |
161 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ), proxyInfo ); |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | 0 | wagon.connect( repository, wagonManager.getAuthenticationInfo( stagingRepositoryId ) ); |
166 | |
} |
167 | |
|
168 | 0 | wagon.putDirectory( new File( stagingDirectory, getStructure( project, false ) ), "." ); |
169 | |
|
170 | |
|
171 | |
|
172 | 0 | if ( wagon instanceof CommandExecutor ) |
173 | |
{ |
174 | 0 | CommandExecutor exec = (CommandExecutor) wagon; |
175 | 0 | exec.executeCommand( "chmod -Rf g+w,a+rX " + repository.getBasedir() ); |
176 | |
} |
177 | 0 | } |
178 | 0 | catch ( ResourceDoesNotExistException e ) |
179 | |
{ |
180 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
181 | |
} |
182 | 0 | catch ( TransferFailedException e ) |
183 | |
{ |
184 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
185 | |
} |
186 | 0 | catch ( AuthorizationException e ) |
187 | |
{ |
188 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
189 | |
} |
190 | 0 | catch ( ConnectionException e ) |
191 | |
{ |
192 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
193 | |
} |
194 | 0 | catch ( AuthenticationException e ) |
195 | |
{ |
196 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
197 | |
} |
198 | 0 | catch ( CommandExecutionException e ) |
199 | |
{ |
200 | 0 | throw new MojoExecutionException( "Error uploading site", e ); |
201 | |
} |
202 | |
finally |
203 | |
{ |
204 | 0 | try |
205 | |
{ |
206 | 0 | wagon.disconnect(); |
207 | |
} |
208 | 0 | catch ( ConnectionException e ) |
209 | |
{ |
210 | 0 | getLog().error( "Error disconnecting wagon - ignored", e ); |
211 | 0 | } |
212 | 0 | } |
213 | 0 | } |
214 | |
|
215 | |
public void contextualize( Context context ) |
216 | |
throws ContextException |
217 | |
{ |
218 | 0 | container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
protected String getStagingSiteURL( MavenProject currentProject, List reactorProjects, String usersStagingSiteURL ) |
230 | |
{ |
231 | 0 | String topLevelURL = null; |
232 | 0 | String relative = ""; |
233 | |
|
234 | |
|
235 | 0 | if ( usersStagingSiteURL != null ) |
236 | |
{ |
237 | 0 | getLog().debug( "stagingSiteURL specified by the user." ); |
238 | 0 | topLevelURL = usersStagingSiteURL; |
239 | |
} |
240 | 0 | getLog().debug( "stagingSiteURL NOT specified by the user." ); |
241 | |
|
242 | |
|
243 | 0 | MavenProject topLevelProject = getTopLevelProject( reactorProjects ); |
244 | |
|
245 | |
|
246 | |
|
247 | 0 | if ( topLevelProject == null ) |
248 | |
{ |
249 | 0 | if ( topLevelURL == null ) |
250 | |
{ |
251 | |
|
252 | |
|
253 | 0 | getLog().debug( "No top level project found in the reactor, using the current project." ); |
254 | 0 | topLevelURL = |
255 | |
currentProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY; |
256 | |
} |
257 | |
} |
258 | |
else |
259 | |
{ |
260 | |
|
261 | 0 | relative = "/" + siteTool.getRelativePath( currentProject.getDistributionManagement().getSite().getUrl(), |
262 | |
topLevelProject.getDistributionManagement().getSite().getUrl() ); |
263 | |
|
264 | |
|
265 | 0 | relative = relative.replace( '\\', '/' ); |
266 | |
|
267 | 0 | if ( topLevelURL == null ) |
268 | |
{ |
269 | |
|
270 | |
|
271 | 0 | getLog().debug( "Using the top level project found in the reactor." ); |
272 | 0 | topLevelURL = |
273 | |
topLevelProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY; |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 0 | return topLevelURL + relative; |
282 | |
} |
283 | |
} |