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.codehaus.plexus.util.xml.Xpp3Dom;
33
34
35
36
37
38
39
40
41
42
43 @Mojo(name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST)
44 public class SiteStageDeployMojo extends AbstractStagingMojo {
45
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 = "stagingSiteId", alias = "stagingRepositoryId")
78 private String stagingSiteId;
79
80 @Override
81 protected boolean isDeploy() {
82 return true;
83 }
84
85
86
87
88
89 @Override
90 protected String determineTopDistributionManagementSiteUrl() throws MojoExecutionException {
91 if (topSiteURL != null && !topSiteURL.isEmpty()) {
92 getLog().debug("stage-deploy top distributionManagement.site.url configured with topSiteURL parameter: "
93 + topSiteURL);
94 return topSiteURL;
95 }
96
97 if (stagingSiteURL != null && !stagingSiteURL.isEmpty()) {
98
99 MavenProject topProject = getTopMostParentWithSameStagingSiteURL();
100 String url = getSite(topProject).getUrl();
101 getLog().debug("stage-deploy top stagingSiteURL found in " + topProject.getId() + " with value: " + url);
102 return url;
103 }
104
105 return super.determineTopDistributionManagementSiteUrl();
106 }
107
108 @Override
109 protected Site determineDeploySite() throws MojoExecutionException {
110 Site top = new Site();
111
112 top.setId(determineStagingSiteId());
113 getLog().info("Using this server ID for stage deploy: " + top.getId());
114
115 String stagingURL = determineStageDeploySiteURL();
116 getLog().info("Using this base URL for stage deploy: " + stagingURL);
117
118 top.setUrl(stagingURL);
119
120 return top;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134 private MavenProject getTopMostParentWithSameStagingSiteURL() {
135 MavenProject current = project;
136 MavenProject parent;
137
138
139 while (
140 (parent = current.getParent()) != null && stagingSiteURL.equals(getStagingSiteURL(parent))) {
141 current = parent;
142 }
143
144
145 return current;
146 }
147
148
149
150
151
152
153
154
155 private String getStagingSiteURL(MavenProject project) {
156 final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
157
158 if (project == null) {
159 return null;
160 }
161
162 final Build build = project.getBuild();
163 if (build == null) {
164 return null;
165 }
166
167 Map<String, Plugin> plugins = build.getPluginsAsMap();
168
169 Plugin sitePlugin = plugins.get(sitePluginKey);
170 if (sitePlugin == null) {
171 final PluginManagement buildPluginManagement = build.getPluginManagement();
172 if (buildPluginManagement == null) {
173 return null;
174 }
175
176 plugins = buildPluginManagement.getPluginsAsMap();
177 sitePlugin = plugins.get(sitePluginKey);
178 }
179
180 if (sitePlugin == null) {
181 return null;
182 }
183
184 final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
185 if (sitePluginConfiguration == null) {
186 return null;
187 }
188
189 final Xpp3Dom child = sitePluginConfiguration.getChild("stagingSiteURL");
190 if (child == null) {
191 return null;
192 } else {
193 return child.getValue();
194 }
195 }
196
197
198
199
200
201
202 private String determineStageDeploySiteURL() throws MojoExecutionException {
203 if (stagingSiteURL != null) {
204
205 getLog().debug("stagingSiteURL specified by the user: " + stagingSiteURL);
206 return stagingSiteURL;
207 }
208
209
210 String defaultStagingSiteURL = appendSlash(getTopDistributionManagementSiteUrl()) + DEFAULT_STAGING_DIRECTORY;
211 getLog().debug("stagingSiteURL NOT specified, using the top level project: " + defaultStagingSiteURL);
212
213 return defaultStagingSiteURL;
214 }
215
216 private String determineStagingSiteId() {
217 if (stagingSiteId != null) {
218 return stagingSiteId;
219 }
220
221 try {
222 return getSite(project).getId();
223 } catch (MojoExecutionException ex) {
224 return "stagingSite";
225 }
226 }
227 }