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