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 java.util.Map;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.model.Build;
25 import org.apache.maven.model.Plugin;
26 import org.apache.maven.model.PluginManagement;
27 import org.apache.maven.model.Site;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugins.annotations.Mojo;
30 import org.apache.maven.plugins.annotations.Parameter;
31 import org.apache.maven.plugins.annotations.ResolutionScope;
32 import org.apache.maven.project.MavenProject;
33 import org.codehaus.plexus.util.xml.Xpp3Dom;
34
35
36
37
38
39
40
41
42
43 @Mojo( name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST )
44 public class SiteStageDeployMojo
45 extends AbstractDeployMojo
46 {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Parameter( property = "stagingSiteURL" )
63 private String stagingSiteURL;
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Parameter( property = "stagingRepositoryId" )
78 private String stagingRepositoryId;
79
80 @Override
81
82
83
84
85
86
87
88
89 protected String getDeployModuleDirectory()
90 throws MojoExecutionException
91 {
92
93 if ( StringUtils.isNotEmpty( stagingSiteURL ) )
94 {
95
96
97 String relative = siteTool.getRelativePath( getSite( project ).getUrl(),
98 getSiteForTopMostParentWithSameStagingSiteURL( project ).getUrl() );
99
100
101
102 relative = relative.replace( '\\', '/' );
103
104 getLog().debug( "The stagingSiteURL is configured, using special way to calculate relative path." );
105 return ( "".equals( relative ) ) ? "./" : relative;
106 }
107 else
108 {
109 getLog().debug( "No stagingSiteURL is configured, using standard way to calculate relative path." );
110 return super.getDeployModuleDirectory();
111 }
112 }
113
114 @Override
115 protected String getDeployRepositoryID()
116 throws MojoExecutionException
117 {
118 stagingRepositoryId = stagingRepoId( stagingRepositoryId );
119
120 getLog().info( "Using this server ID for stage deploy: " + stagingRepositoryId );
121
122 return stagingRepositoryId;
123 }
124
125 @Override
126 protected String getDeployRepositoryURL()
127 throws MojoExecutionException
128 {
129 String stagingURL = determineStagingSiteURL( stagingSiteURL );
130
131 getLog().info( "Using this base URL for stage deploy: " + stagingURL );
132
133 return stagingURL;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 private Site getSiteForTopMostParentWithSameStagingSiteURL( MavenProject project )
150 {
151 String actualStagingSiteURL = getStagingSiteURL( project );
152
153 MavenProject parent = project;
154 Site site = null;
155
156 while ( parent != null
157 && actualStagingSiteURL.equals( getStagingSiteURL( parent ) ) )
158 {
159 site = parent.getDistributionManagement().getSite();
160
161
162 parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
163 }
164
165 return site;
166 }
167
168
169
170
171
172
173
174
175 private String getStagingSiteURL( MavenProject project )
176 {
177 final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
178
179 if ( project == null )
180 {
181 return null;
182 }
183
184 final Build build = project.getBuild();
185 if ( build == null )
186 {
187 return null;
188 }
189
190 Map<String, Plugin> plugins = build.getPluginsAsMap();
191
192 Plugin sitePlugin = plugins.get( sitePluginKey );
193 if ( sitePlugin == null )
194 {
195 final PluginManagement buildPluginManagement = build.getPluginManagement();
196 if ( buildPluginManagement == null )
197 {
198 return null;
199 }
200
201 plugins = buildPluginManagement.getPluginsAsMap();
202 sitePlugin = plugins.get( sitePluginKey );
203 }
204
205 if ( sitePlugin == null )
206 {
207 return null;
208 }
209
210 final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
211 if ( sitePluginConfiguration == null )
212 {
213 return null;
214 }
215
216 final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
217 if ( child == null )
218 {
219 return null;
220 }
221 else
222 {
223 return child.getValue();
224 }
225 }
226
227
228
229
230
231
232
233
234 private String determineStagingSiteURL( final String usersStagingSiteURL )
235 throws MojoExecutionException
236 {
237 String topLevelURL = null;
238
239 if ( usersStagingSiteURL != null )
240 {
241
242 getLog().debug( "stagingSiteURL specified by the user: " + usersStagingSiteURL );
243 topLevelURL = usersStagingSiteURL;
244 }
245 else
246 {
247
248 topLevelURL = appendSlash( getRootSite( project ).getUrl() )
249 + DEFAULT_STAGING_DIRECTORY;
250 getLog().debug( "stagingSiteURL NOT specified, using the top level project: " + topLevelURL );
251 }
252
253
254
255
256
257 return topLevelURL;
258 }
259
260 private String stagingRepoId( final String stagingRepoId )
261 {
262 if ( stagingRepoId != null )
263 {
264 return stagingRepoId;
265 }
266
267 try
268 {
269 return getSite( project ).getId();
270 }
271 catch ( MojoExecutionException ex )
272 {
273 return "stagingSite";
274 }
275 }
276 }