View Javadoc
1   package org.apache.maven.plugins.site.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * 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
46      extends AbstractStagingMojo
47  {
48      /**
49       * The staged site will be deployed to this URL.
50       * <p/>
51       * If you don't specify this, the default-value will be
52       * "${project.distributionManagement.site.url}/staging", where "project" is
53       * either the current project or, in a reactor build, the top level project
54       * in the reactor.
55       * <p>
56       * Note that even if you specify this plugin parameter, you still need to indicate
57       * ${project.distributionManagement.site.url} at least in your top level project
58       * in order for relative links between modules to be resolved correctly.
59       * </p>
60       *
61       * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
62       *
63       * @since 2.3
64       */
65      @Parameter( property = "stagingSiteURL" )
66      private String stagingSiteURL;
67  
68      /**
69       * The identifier of the repository where the staging site will be deployed. This id will be used to lookup a
70       * corresponding <code>&lt;server&gt;</code> entry from the <code>settings.xml</code>. If a matching
71       * <code>&lt;server&gt;</code> entry is found, its configured credentials will be used for authentication.
72       * <p/>
73       * If this is not specified, then the corresponding value of <code>distributionManagement.site.id</code>
74       * will be taken as default, unless this is not defined either then the String
75       * <code>"stagingSite"</code> is used. (<strong>Note</strong>:
76       * until v. 2.3 and 3.0-beta-3 the String <code>"stagingSite"</code> is always used.)
77       *
78       * @since 2.0.1
79       */
80      @Parameter( property = "stagingRepositoryId" )
81      private String stagingRepositoryId;
82  
83      @Override
84      protected boolean isDeploy()
85      {
86          return true;
87      }
88  
89      /**
90       * If <code>stagingSiteURL</code> is configured, top most parent with same staging site url
91       * will be used.
92       */
93      @Override
94      protected String determineTopDistributionManagementSiteUrl()
95          throws MojoExecutionException
96      {
97          if ( StringUtils.isNotEmpty( topSiteURL ) )
98          {
99              getLog().debug( "stage-deploy top distributionManagement.site.url configured with topSiteURL parameter: "
100                 + topSiteURL );
101             return topSiteURL;
102         }
103 
104         if ( StringUtils.isNotEmpty( stagingSiteURL ) )
105         {
106             // We need to calculate the first project that supplied same stagingSiteURL
107             MavenProject topProject = getTopMostParentWithSameStagingSiteURL();
108             String url = getSite( topProject ).getUrl();
109             getLog().debug( "stage-deploy top stagingSiteURL found in " + topProject.getId() + " with value: " + url );
110             return url;
111         }
112 
113         return super.determineTopDistributionManagementSiteUrl();
114     }
115 
116     @Override
117     protected Site determineDeploySite()
118         throws MojoExecutionException
119     {
120         Site top = new Site();
121 
122         top.setId( stagingRepoId() );
123         getLog().info( "Using this server ID for stage deploy: " + top.getId() );
124 
125         String stagingURL = determineStageDeploySiteURL();
126         getLog().info( "Using this base URL for stage deploy: " + stagingURL );
127 
128         top.setUrl( stagingURL );
129 
130         return top;
131     }
132 
133     /**
134      * Extract the distributionManagement.site of the top most project in the
135      * hierarchy that specifies a stagingSiteURL, starting at the actual MavenProject.
136      * <p/>
137      * This climbs up the project hierarchy and returns the site of the top most
138      * project for which
139      * {@link #getStagingSiteURL(org.apache.maven.project.MavenProject)} returns
140      * same URL as actual.
141      *
142      * @return the site for the top most project that has a stagingSiteURL. Not null.
143      */
144     private MavenProject getTopMostParentWithSameStagingSiteURL()
145     {
146         MavenProject current = project;
147         MavenProject parent;
148 
149         // CHECKSTYLE_OFF: InnerAssignment
150         while (   // MSITE-585, MNG-1943
151                 ( parent = siteTool.getParentProject( current, reactorProjects, localRepository ) ) != null
152                 && stagingSiteURL.equals( getStagingSiteURL( parent ) ) )
153         {
154             current = parent;
155         }
156         // CHECKSTYLE_ON: InnerAssignment
157 
158         return current;
159     }
160 
161     /**
162      * Extract the value of the stagingSiteURL configuration parameter of
163      * maven-site-plugin for the given project.
164      *
165      * @param project The MavenProject, not null
166      * @return The stagingSiteURL for the project, or null if it doesn't have one
167      */
168     private String getStagingSiteURL( MavenProject project )
169     {
170         final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
171 
172         if ( project == null )
173         {
174             return null;
175         }
176 
177         final Build build = project.getBuild();
178         if ( build == null )
179         {
180             return null;
181         }
182 
183         Map<String, Plugin> plugins = build.getPluginsAsMap();
184 
185         Plugin sitePlugin = plugins.get( sitePluginKey );
186         if ( sitePlugin == null )
187         {
188             final PluginManagement buildPluginManagement = build.getPluginManagement();
189             if ( buildPluginManagement == null )
190             {
191                 return null;
192             }
193 
194             plugins = buildPluginManagement.getPluginsAsMap();
195             sitePlugin = plugins.get( sitePluginKey );
196         }
197 
198         if ( sitePlugin == null )
199         {
200             return null;
201         }
202 
203         final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
204         if ( sitePluginConfiguration == null )
205         {
206             return null;
207         }
208 
209         final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
210         if ( child == null )
211         {
212             return null;
213         }
214         else
215         {
216             return child.getValue();
217         }
218     }
219 
220     /**
221      * Find the URL where staging will take place.
222      *
223      * @return the site URL for staging
224      */
225     private String determineStageDeploySiteURL()
226         throws MojoExecutionException
227     {
228         if ( stagingSiteURL != null )
229         {
230             // the user has specified a stagingSiteURL - use it
231             getLog().debug( "stagingSiteURL specified by the user: " + stagingSiteURL );
232             return stagingSiteURL;
233         }
234 
235         // The user didn't specify a URL, use the top level site distribution URL and add "[/]staging/" to it
236         String defaultStagingSiteURL = appendSlash( getTopDistributionManagementSiteUrl() ) + DEFAULT_STAGING_DIRECTORY;
237         getLog().debug( "stagingSiteURL NOT specified, using the top level project: " + defaultStagingSiteURL );
238 
239         return defaultStagingSiteURL;
240     }
241 
242     private String stagingRepoId()
243     {
244         if ( stagingRepositoryId != null )
245         {
246             return stagingRepositoryId;
247         }
248 
249         try
250         {
251             return getSite( project ).getId();
252         }
253         catch ( MojoExecutionException ex )
254         {
255             return "stagingSite";
256         }
257     }
258 }