1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.site.deploy;
20
21 import java.util.Map;
22
23 import org.apache.maven.model.Build;
24 import org.apache.maven.model.Plugin;
25 import org.apache.maven.model.PluginManagement;
26 import org.apache.maven.model.Site;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.shared.utils.StringUtils;
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 extends AbstractStagingMojo {
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @Parameter(property = "stagingSiteURL")
64 private String stagingSiteURL;
65
66
67
68
69
70
71
72
73
74
75
76
77
78 @Parameter(property = "stagingRepositoryId")
79 private String stagingRepositoryId;
80
81 @Override
82 protected boolean isDeploy() {
83 return true;
84 }
85
86
87
88
89
90 @Override
91 protected String determineTopDistributionManagementSiteUrl() throws MojoExecutionException {
92 if (StringUtils.isNotEmpty(topSiteURL)) {
93 getLog().debug("stage-deploy top distributionManagement.site.url configured with topSiteURL parameter: "
94 + topSiteURL);
95 return topSiteURL;
96 }
97
98 if (StringUtils.isNotEmpty(stagingSiteURL)) {
99
100 MavenProject topProject = getTopMostParentWithSameStagingSiteURL();
101 String url = getSite(topProject).getUrl();
102 getLog().debug("stage-deploy top stagingSiteURL found in " + topProject.getId() + " with value: " + url);
103 return url;
104 }
105
106 return super.determineTopDistributionManagementSiteUrl();
107 }
108
109 @Override
110 protected Site determineDeploySite() throws MojoExecutionException {
111 Site top = new Site();
112
113 top.setId(stagingRepoId());
114 getLog().info("Using this server ID for stage deploy: " + top.getId());
115
116 String stagingURL = determineStageDeploySiteURL();
117 getLog().info("Using this base URL for stage deploy: " + stagingURL);
118
119 top.setUrl(stagingURL);
120
121 return top;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 private MavenProject getTopMostParentWithSameStagingSiteURL() {
136 MavenProject current = project;
137 MavenProject parent;
138
139
140 while (
141 (parent = current.getParent()) != null && stagingSiteURL.equals(getStagingSiteURL(parent))) {
142 current = parent;
143 }
144
145
146 return current;
147 }
148
149
150
151
152
153
154
155
156 private String getStagingSiteURL(MavenProject project) {
157 final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
158
159 if (project == null) {
160 return null;
161 }
162
163 final Build build = project.getBuild();
164 if (build == null) {
165 return null;
166 }
167
168 Map<String, Plugin> plugins = build.getPluginsAsMap();
169
170 Plugin sitePlugin = plugins.get(sitePluginKey);
171 if (sitePlugin == null) {
172 final PluginManagement buildPluginManagement = build.getPluginManagement();
173 if (buildPluginManagement == null) {
174 return null;
175 }
176
177 plugins = buildPluginManagement.getPluginsAsMap();
178 sitePlugin = plugins.get(sitePluginKey);
179 }
180
181 if (sitePlugin == null) {
182 return null;
183 }
184
185 final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
186 if (sitePluginConfiguration == null) {
187 return null;
188 }
189
190 final Xpp3Dom child = sitePluginConfiguration.getChild("stagingSiteURL");
191 if (child == null) {
192 return null;
193 } else {
194 return child.getValue();
195 }
196 }
197
198
199
200
201
202
203 private String determineStageDeploySiteURL() throws MojoExecutionException {
204 if (stagingSiteURL != null) {
205
206 getLog().debug("stagingSiteURL specified by the user: " + stagingSiteURL);
207 return stagingSiteURL;
208 }
209
210
211 String defaultStagingSiteURL = appendSlash(getTopDistributionManagementSiteUrl()) + DEFAULT_STAGING_DIRECTORY;
212 getLog().debug("stagingSiteURL NOT specified, using the top level project: " + defaultStagingSiteURL);
213
214 return defaultStagingSiteURL;
215 }
216
217 private String stagingRepoId() {
218 if (stagingRepositoryId != null) {
219 return stagingRepositoryId;
220 }
221
222 try {
223 return getSite(project).getId();
224 } catch (MojoExecutionException ex) {
225 return "stagingSite";
226 }
227 }
228 }