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.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   * Deploys the generated site to a staging or mock URL to the site URL
36   * specified in the <code>&lt;distributionManagement&gt;</code> section of the
37   * POM, using <a href="/wagon/">wagon supported protocols</a>
38   *
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   *
41   * @since 2.0
42   */
43  @Mojo(name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST)
44  public class SiteStageDeployMojo extends AbstractStagingMojo {
45      /**
46       * The staged site will be deployed to this URL.
47       * <p/>
48       * If you don't specify this, the default-value will be
49       * "${project.distributionManagement.site.url}/staging", where "project" is
50       * either the current project or, in a reactor build, the top level project
51       * in the reactor.
52       * <p>
53       * Note that even if you specify this plugin parameter, you still need to indicate
54       * ${project.distributionManagement.site.url} at least in your top level project
55       * in order for relative links between modules to be resolved correctly.
56       * </p>
57       *
58       * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
59       *
60       * @since 2.3
61       */
62      @Parameter(property = "stagingSiteURL")
63      private String stagingSiteURL;
64  
65      /**
66       * The identifier of the repository where the staging site will be deployed. This id will be used to lookup a
67       * corresponding <code>&lt;server&gt;</code> entry from the <code>settings.xml</code>. If a matching
68       * <code>&lt;server&gt;</code> entry is found, its configured credentials will be used for authentication.
69       * <p/>
70       * If this is not specified, then the corresponding value of <code>distributionManagement.site.id</code>
71       * will be taken as default, unless this is not defined either then the String
72       * <code>"stagingSite"</code> is used. (<strong>Note</strong>:
73       * until v. 2.3 and 3.0-beta-3 the String <code>"stagingSite"</code> is always used.)
74       *
75       * @since 2.0.1
76       */
77      @Parameter(property = "stagingRepositoryId")
78      private String stagingRepositoryId;
79  
80      @Override
81      protected boolean isDeploy() {
82          return true;
83      }
84  
85      /**
86       * If <code>stagingSiteURL</code> is configured, top most parent with same staging site url
87       * will be used.
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              // We need to calculate the first project that supplied same stagingSiteURL
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(stagingRepoId());
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      * Extract the distributionManagement.site of the top most project in the
125      * hierarchy that specifies a stagingSiteURL, starting at the actual MavenProject.
126      * <p/>
127      * This climbs up the project hierarchy and returns the site of the top most
128      * project for which
129      * {@link #getStagingSiteURL(org.apache.maven.project.MavenProject)} returns
130      * same URL as actual.
131      *
132      * @return the site for the top most project that has a stagingSiteURL. Not null.
133      */
134     private MavenProject getTopMostParentWithSameStagingSiteURL() {
135         MavenProject current = project;
136         MavenProject parent;
137 
138         // CHECKSTYLE_OFF: InnerAssignment
139         while ( // MSITE-585, MNG-1943
140         (parent = current.getParent()) != null && stagingSiteURL.equals(getStagingSiteURL(parent))) {
141             current = parent;
142         }
143         // CHECKSTYLE_ON: InnerAssignment
144 
145         return current;
146     }
147 
148     /**
149      * Extract the value of the stagingSiteURL configuration parameter of
150      * maven-site-plugin for the given project.
151      *
152      * @param project The MavenProject, not null
153      * @return The stagingSiteURL for the project, or null if it doesn't have one
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      * Find the URL where staging will take place.
199      *
200      * @return the site URL for staging
201      */
202     private String determineStageDeploySiteURL() throws MojoExecutionException {
203         if (stagingSiteURL != null) {
204             // the user has specified a stagingSiteURL - use it
205             getLog().debug("stagingSiteURL specified by the user: " + stagingSiteURL);
206             return stagingSiteURL;
207         }
208 
209         // The user didn't specify a URL, use the top level site distribution URL and add "[/]staging/" to it
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 stagingRepoId() {
217         if (stagingRepositoryId != null) {
218             return stagingRepositoryId;
219         }
220 
221         try {
222             return getSite(project).getId();
223         } catch (MojoExecutionException ex) {
224             return "stagingSite";
225         }
226     }
227 }