View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.deploy;
20  
21  import java.util.Map;
22  
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   * Deploys the generated site to a staging or mock URL to the site URL
37   * specified in the <code>&lt;distributionManagement&gt;</code> section of the
38   * POM, using <a href="/wagon/">wagon supported protocols</a>
39   *
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   *
42   * @since 2.0
43   */
44  @Mojo(name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST)
45  public class SiteStageDeployMojo extends AbstractStagingMojo {
46      /**
47       * The staged site will be deployed to this URL.
48       * <p/>
49       * If you don't specify this, the default-value will be
50       * "${project.distributionManagement.site.url}/staging", where "project" is
51       * either the current project or, in a reactor build, the top level project
52       * in the reactor.
53       * <p>
54       * Note that even if you specify this plugin parameter, you still need to indicate
55       * ${project.distributionManagement.site.url} at least in your top level project
56       * in order for relative links between modules to be resolved correctly.
57       * </p>
58       *
59       * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
60       *
61       * @since 2.3
62       */
63      @Parameter(property = "stagingSiteURL")
64      private String stagingSiteURL;
65  
66      /**
67       * The identifier of the repository where the staging site will be deployed. This id will be used to lookup a
68       * corresponding <code>&lt;server&gt;</code> entry from the <code>settings.xml</code>. If a matching
69       * <code>&lt;server&gt;</code> entry is found, its configured credentials will be used for authentication.
70       * <p/>
71       * If this is not specified, then the corresponding value of <code>distributionManagement.site.id</code>
72       * will be taken as default, unless this is not defined either then the String
73       * <code>"stagingSite"</code> is used. (<strong>Note</strong>:
74       * until v. 2.3 and 3.0-beta-3 the String <code>"stagingSite"</code> is always used.)
75       *
76       * @since 2.0.1
77       */
78      @Parameter(property = "stagingRepositoryId")
79      private String stagingRepositoryId;
80  
81      @Override
82      protected boolean isDeploy() {
83          return true;
84      }
85  
86      /**
87       * If <code>stagingSiteURL</code> is configured, top most parent with same staging site url
88       * will be used.
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              // We need to calculate the first project that supplied same stagingSiteURL
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      * Extract the distributionManagement.site of the top most project in the
126      * hierarchy that specifies a stagingSiteURL, starting at the actual MavenProject.
127      * <p/>
128      * This climbs up the project hierarchy and returns the site of the top most
129      * project for which
130      * {@link #getStagingSiteURL(org.apache.maven.project.MavenProject)} returns
131      * same URL as actual.
132      *
133      * @return the site for the top most project that has a stagingSiteURL. Not null.
134      */
135     private MavenProject getTopMostParentWithSameStagingSiteURL() {
136         MavenProject current = project;
137         MavenProject parent;
138 
139         // CHECKSTYLE_OFF: InnerAssignment
140         while ( // MSITE-585, MNG-1943
141         (parent = current.getParent()) != null && stagingSiteURL.equals(getStagingSiteURL(parent))) {
142             current = parent;
143         }
144         // CHECKSTYLE_ON: InnerAssignment
145 
146         return current;
147     }
148 
149     /**
150      * Extract the value of the stagingSiteURL configuration parameter of
151      * maven-site-plugin for the given project.
152      *
153      * @param project The MavenProject, not null
154      * @return The stagingSiteURL for the project, or null if it doesn't have one
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      * Find the URL where staging will take place.
200      *
201      * @return the site URL for staging
202      */
203     private String determineStageDeploySiteURL() throws MojoExecutionException {
204         if (stagingSiteURL != null) {
205             // the user has specified a stagingSiteURL - use it
206             getLog().debug("stagingSiteURL specified by the user: " + stagingSiteURL);
207             return stagingSiteURL;
208         }
209 
210         // The user didn't specify a URL, use the top level site distribution URL and add "[/]staging/" to it
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 }