1 package org.apache.maven.plugins.site.deploy;
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.lang3.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
44 @Mojo( name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST )
45 public class SiteStageDeployMojo
46 extends AbstractStagingMojo
47 {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @Parameter( property = "stagingSiteURL" )
66 private String stagingSiteURL;
67
68
69
70
71
72
73
74
75
76
77
78
79
80 @Parameter( property = "stagingRepositoryId" )
81 private String stagingRepositoryId;
82
83 @Override
84 protected boolean isDeploy()
85 {
86 return true;
87 }
88
89
90
91
92
93 @Override
94 protected String determineTopDistributionManagementSiteUrl()
95 throws MojoExecutionException
96 {
97 if ( StringUtils.isNotEmpty( topSiteURL ) )
98 {
99 return topSiteURL;
100 }
101
102 if ( StringUtils.isNotEmpty( stagingSiteURL ) )
103 {
104
105 return getSite( getTopMostParentWithSameStagingSiteURL() ).getUrl();
106 }
107
108 return super.determineTopDistributionManagementSiteUrl();
109 }
110
111 @Override
112 protected Site determineDeploySite()
113 throws MojoExecutionException
114 {
115 Site top = new Site();
116
117 top.setId( stagingRepoId() );
118 getLog().info( "Using this server ID for stage deploy: " + top.getId() );
119
120 String stagingURL = determineStageDeploySiteURL();
121 getLog().info( "Using this base URL for stage deploy: " + stagingURL );
122
123 top.setUrl( stagingURL );
124
125 return top;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 private MavenProject getTopMostParentWithSameStagingSiteURL()
140 {
141 MavenProject current = project;
142 MavenProject parent;
143
144
145 while (
146 ( parent = siteTool.getParentProject( current, reactorProjects, localRepository ) ) != null
147 && stagingSiteURL.equals( getStagingSiteURL( parent ) ) )
148 {
149 current = parent;
150 }
151
152
153 return current;
154 }
155
156
157
158
159
160
161
162
163 private String getStagingSiteURL( MavenProject project )
164 {
165 final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
166
167 if ( project == null )
168 {
169 return null;
170 }
171
172 final Build build = project.getBuild();
173 if ( build == null )
174 {
175 return null;
176 }
177
178 Map<String, Plugin> plugins = build.getPluginsAsMap();
179
180 Plugin sitePlugin = plugins.get( sitePluginKey );
181 if ( sitePlugin == null )
182 {
183 final PluginManagement buildPluginManagement = build.getPluginManagement();
184 if ( buildPluginManagement == null )
185 {
186 return null;
187 }
188
189 plugins = buildPluginManagement.getPluginsAsMap();
190 sitePlugin = plugins.get( sitePluginKey );
191 }
192
193 if ( sitePlugin == null )
194 {
195 return null;
196 }
197
198 final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
199 if ( sitePluginConfiguration == null )
200 {
201 return null;
202 }
203
204 final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
205 if ( child == null )
206 {
207 return null;
208 }
209 else
210 {
211 return child.getValue();
212 }
213 }
214
215
216
217
218
219
220 private String determineStageDeploySiteURL()
221 throws MojoExecutionException
222 {
223 if ( stagingSiteURL != null )
224 {
225
226 getLog().debug( "stagingSiteURL specified by the user: " + stagingSiteURL );
227 return stagingSiteURL;
228 }
229
230
231 String defaultStagingSiteURL = appendSlash( getTopDistributionManagementSiteUrl() ) + DEFAULT_STAGING_DIRECTORY;
232 getLog().debug( "stagingSiteURL NOT specified, using the top level project: " + defaultStagingSiteURL );
233
234 return defaultStagingSiteURL;
235 }
236
237 private String stagingRepoId()
238 {
239 if ( stagingRepositoryId != null )
240 {
241 return stagingRepositoryId;
242 }
243
244 try
245 {
246 return getSite( project ).getId();
247 }
248 catch ( MojoExecutionException ex )
249 {
250 return "stagingSite";
251 }
252 }
253 }