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