View Javadoc

1   package org.apache.maven.plugins.site;
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.lang.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   * @version $Id: SiteStageDeployMojo.html 861484 2013-05-09 23:10:16Z hboutemy $
42   */
43  @Mojo( name = "stage-deploy", requiresDependencyResolution = ResolutionScope.TEST )
44  public class SiteStageDeployMojo
45      extends AbstractStagingMojo
46  {
47      /**
48       * The staged site will be deployed to this URL.
49       * <p/>
50       * If you don't specify this, the default-value will be
51       * "${project.distributionManagement.site.url}/staging", where "project" is
52       * either the current project or, in a reactor build, the top level project
53       * in the reactor.
54       * <p>
55       * Note that even if you specify this plugin parameter, you still need to indicate
56       * ${project.distributionManagement.site.url} at least in your top level project
57       * in order for relative links between modules to be resolved correctly.
58       * </p>
59       *
60       * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
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      {
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()
92          throws MojoExecutionException
93      {
94          if ( StringUtils.isNotEmpty( topSiteURL ) )
95          {
96              return topSiteURL;
97          }
98  
99          if ( StringUtils.isNotEmpty( stagingSiteURL ) )
100         {
101             // We need to calculate the first project that supplied same stagingSiteURL
102             return getSite( getTopMostParentWithSameStagingSiteURL( project ) ).getUrl();
103         }
104 
105         return super.determineTopDistributionManagementSiteUrl();
106     }
107 
108     @Override
109     protected Site determineDeploySite()
110         throws MojoExecutionException
111     {
112         Site top = new Site();
113 
114         top.setId( stagingRepoId() );
115         getLog().info( "Using this server ID for stage deploy: " + top.getId() );
116 
117         String stagingURL = determineStageDeploySiteURL();
118         getLog().info( "Using this base URL for stage deploy: " + stagingURL );
119 
120         top.setUrl( stagingURL );
121 
122         return top;
123     }
124 
125     /**
126      * Extract the distributionManagement.site of the top most project in the
127      * hierarchy that specifies a stagingSiteURL, starting at the given
128      * MavenProject.
129      * <p/>
130      * This climbs up the project hierarchy and returns the site of the top most
131      * project for which
132      * {@link #getStagingSiteURL(org.apache.maven.project.MavenProject)} returns
133      * same URL as actual.
134      *
135      * @param project the MavenProject. Not null.
136      * @return the site for the top most project that has a stagingSiteURL. Not null.
137      */
138     private MavenProject getTopMostParentWithSameStagingSiteURL( MavenProject project )
139     {
140         String actualStagingSiteURL = getStagingSiteURL( project );
141 
142         MavenProject parent = project;
143 
144         while ( parent != null
145                 && actualStagingSiteURL.equals( getStagingSiteURL( parent ) ) )
146         {
147             project = parent;
148  
149             // MSITE-585, MNG-1943
150             parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
151         }
152 
153         return project;
154     }
155 
156     /**
157      * Extract the value of the stagingSiteURL configuration parameter of
158      * maven-site-plugin for the given project.
159      *
160      * @param project The MavenProject, not null
161      * @return The stagingSiteURL for the project, or null if it doesn't have one
162      */
163     private String getStagingSiteURL( MavenProject project )
164     {
165         final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
166 
167         if ( project == null )
168         {
169             return null;
170         }
171 
172         final Build build = project.getBuild();
173         if ( build == null )
174         {
175             return null;
176         }
177 
178         Map<String, Plugin> plugins = build.getPluginsAsMap();
179 
180         Plugin sitePlugin = plugins.get( sitePluginKey );
181         if ( sitePlugin == null )
182         {
183             final PluginManagement buildPluginManagement = build.getPluginManagement();
184             if ( buildPluginManagement == null )
185             {
186                 return null;
187             }
188 
189             plugins = buildPluginManagement.getPluginsAsMap();
190             sitePlugin = plugins.get( sitePluginKey );
191         }
192 
193         if ( sitePlugin == null )
194         {
195             return null;
196         }
197 
198         final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
199         if ( sitePluginConfiguration == null )
200         {
201             return null;
202         }
203 
204         final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
205         if ( child == null )
206         {
207             return null;
208         }
209         else
210         {
211             return child.getValue();
212         }
213     }
214 
215     /**
216      * Find the URL where staging will take place.
217      *
218      * @return the site URL for staging
219      */
220     private String determineStageDeploySiteURL()
221         throws MojoExecutionException
222     {
223         if ( stagingSiteURL != null )
224         {
225             // the user has specified a stagingSiteURL - use it
226             getLog().debug( "stagingSiteURL specified by the user: " + stagingSiteURL );
227             return stagingSiteURL;
228         }
229 
230         // The user didn't specify a URL, use the top level site distribution URL and add "[/]staging/" to it
231         String defaultStagingSiteURL = appendSlash( getTopDistributionManagementSiteUrl() ) + DEFAULT_STAGING_DIRECTORY;
232         getLog().debug( "stagingSiteURL NOT specified, using the top level project: " + defaultStagingSiteURL );
233 
234         return defaultStagingSiteURL;
235     }
236 
237     private String stagingRepoId()
238     {
239         if ( stagingRepositoryId != null )
240         {
241             return stagingRepositoryId;
242         }
243 
244         try
245         {
246             return getSite( project ).getId();
247         }
248         catch ( MojoExecutionException ex )
249         {
250             return "stagingSite";
251         }
252     }
253 }