PageRenderTime 149ms CodeModel.GetById 97ms RepoModel.GetById 20ms app.codeStats 0ms

/maven-site-plugin/src/main/java/org/apache/maven/plugins/site/SiteStageDeployMojo.java

https://github.com/SciSysUK/maven-plugins
Java | 276 lines | 136 code | 33 blank | 107 comment | 22 complexity | 2e578605f66575ece028f62ce5f6e95f MD5 | raw file
  1. package org.apache.maven.plugins.site;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.Map;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.maven.model.Build;
  23. import org.apache.maven.model.Plugin;
  24. import org.apache.maven.model.PluginManagement;
  25. import org.apache.maven.model.Site;
  26. import org.apache.maven.plugin.MojoExecutionException;
  27. import org.apache.maven.project.MavenProject;
  28. import org.codehaus.plexus.util.xml.Xpp3Dom;
  29. /**
  30. * Deploys the generated site to a staging or mock directory to the site URL
  31. * specified in the <code>&lt;distributionManagement&gt;</code> section of the
  32. * POM. It supports <code>scp</code> and <code>file</code> protocols for
  33. * deployment.
  34. *
  35. * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  36. * @version $Id$
  37. * @goal stage-deploy
  38. * @requiresDependencyResolution test
  39. */
  40. public class SiteStageDeployMojo
  41. extends AbstractDeployMojo
  42. {
  43. /**
  44. * The staged site will be deployed to this URL.
  45. *
  46. * If you don't specify this, the default-value will be
  47. * "${project.distributionManagement.site.url}/staging", where "project" is
  48. * either the current project or, in a reactor build, the top level project
  49. * in the reactor.
  50. * <p>
  51. * Note that even if you specify this plugin parameter you still need to indicate
  52. * ${project.distributionManagement.site.url} at least in your top level project
  53. * in order for relative links between modules to be resolved correctly.
  54. * </p>
  55. *
  56. * @parameter expression="${stagingSiteURL}"
  57. * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
  58. */
  59. private String stagingSiteURL;
  60. /**
  61. * The identifier of the repository where the staging site will be deployed. This id will be used to lookup a
  62. * corresponding <code>&lt;server&gt;</code> entry from the <code>settings.xml</code>. If a matching
  63. * <code>&lt;server&gt;</code> entry is found, its configured credentials will be used for authentication.
  64. *
  65. * If this is not specified, then the corresponding value of <code>distributionManagement.site.id</code>
  66. * will be taken as default, unless this is not defined either then the String
  67. * <code>"stagingSite"</code> is used. (<strong>Note</strong>:
  68. * until v. 2.3 and 3.0-beta-3 the String <code>"stagingSite"</code> is always used.)
  69. *
  70. * @parameter expression="${stagingRepositoryId}"
  71. *
  72. * @since 2.0.1
  73. */
  74. private String stagingRepositoryId;
  75. @Override
  76. /**
  77. * Find the relative path between the distribution URLs of the parent that
  78. * supplied the staging deploy URL and the current project.
  79. *
  80. * @return the relative path or "./" if the two URLs are the same.
  81. *
  82. * @throws MojoExecutionException
  83. */
  84. protected String getDeployModuleDirectory()
  85. throws MojoExecutionException
  86. {
  87. // MSITE-602: If the user specified an explicit stagingSiteURL, use a special relative path
  88. if( StringUtils.isNotEmpty( stagingSiteURL ) )
  89. {
  90. // We need to calculate the relative path between this project and
  91. // the first one that supplied a stagingSiteURL
  92. String relative = siteTool.getRelativePath( getSite( project ).getUrl(),
  93. getSiteForTopMostParentWithStagingSiteURL( project ).getUrl() );
  94. // SiteTool.getRelativePath() uses File.separatorChar,
  95. // so we need to convert '\' to '/' in order for the URL to be valid for Windows users
  96. relative = relative.replace( '\\', '/' );
  97. getLog().debug( "The stagingSiteURL is configured, using special way to calculate relative path." );
  98. return ( "".equals( relative ) ) ? "./" : relative;
  99. }
  100. else
  101. {
  102. getLog().debug( "No stagingSiteURL is configured, using standard way to calculate relative path." );
  103. return super.getDeployModuleDirectory();
  104. }
  105. }
  106. @Override
  107. protected String getDeployRepositoryID()
  108. throws MojoExecutionException
  109. {
  110. stagingRepositoryId = stagingRepoId ( stagingRepositoryId );
  111. getLog().info( "Using this server ID for stage deploy: " + stagingRepositoryId );
  112. return stagingRepositoryId;
  113. }
  114. @Override
  115. protected String getDeployRepositoryURL()
  116. throws MojoExecutionException
  117. {
  118. String stagingURL = determineStagingSiteURL( stagingSiteURL );
  119. getLog().info( "Using this base URL for stage deploy: " + stagingURL );
  120. return stagingURL;
  121. }
  122. /**
  123. * Extract the distributionManagement.site of the top most project in the
  124. * hierarchy that specifies a stagingSiteURL, starting at the given
  125. * 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. * a URL.
  131. *
  132. * @param project the MavenProject. Not null.
  133. * @return the site for the top most project that has a stagingSiteURL. Not null.
  134. */
  135. private Site getSiteForTopMostParentWithStagingSiteURL( MavenProject project )
  136. {
  137. Site site = project.getDistributionManagement().getSite();
  138. MavenProject parent = project;
  139. // @todo Should we check that the stagingSiteURL equals the one in this project instead of being non-empty?
  140. while ( parent != null
  141. && StringUtils.isNotEmpty( getStagingSiteURL( parent ) ) )
  142. {
  143. site = parent.getDistributionManagement().getSite();
  144. // MSITE-585, MNG-1943
  145. parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
  146. }
  147. return site;
  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. {
  158. final String sitePluginKey = "org.apache.maven.plugins:maven-site-plugin";
  159. if ( project == null )
  160. {
  161. return null;
  162. }
  163. final Build build = project.getBuild();
  164. if ( build == null )
  165. {
  166. return null;
  167. }
  168. Map<String, Plugin> plugins = build.getPluginsAsMap();
  169. Plugin sitePlugin = plugins.get( sitePluginKey );
  170. if ( sitePlugin == null ) {
  171. final PluginManagement buildPluginManagement = build.getPluginManagement();
  172. if ( buildPluginManagement == null )
  173. {
  174. return null;
  175. }
  176. plugins = buildPluginManagement.getPluginsAsMap();
  177. sitePlugin = plugins.get( sitePluginKey );
  178. }
  179. if( sitePlugin == null )
  180. {
  181. return null;
  182. }
  183. final Xpp3Dom sitePluginConfiguration = (Xpp3Dom) sitePlugin.getConfiguration();
  184. if ( sitePluginConfiguration == null )
  185. {
  186. return null;
  187. }
  188. final Xpp3Dom child = sitePluginConfiguration.getChild( "stagingSiteURL" );
  189. if ( child == null )
  190. {
  191. return null;
  192. }
  193. else
  194. {
  195. return child.getValue();
  196. }
  197. }
  198. /**
  199. * Find the URL where staging will take place.
  200. *
  201. * @param usersStagingSiteURL The staging site URL as suggested by the user's configuration
  202. *
  203. * @return the site URL for staging
  204. */
  205. private String determineStagingSiteURL( final String usersStagingSiteURL )
  206. throws MojoExecutionException
  207. {
  208. String topLevelURL = null;
  209. if ( usersStagingSiteURL != null )
  210. {
  211. // the user has specified a stagingSiteURL - use it
  212. getLog().debug( "stagingSiteURL specified by the user: " + usersStagingSiteURL );
  213. topLevelURL = usersStagingSiteURL;
  214. }
  215. else
  216. {
  217. // The user didn't specify a URL, use the top level site distribution URL and add "[/]staging/" to it
  218. topLevelURL = appendSlash( getRootSite( project ).getUrl() )
  219. + DEFAULT_STAGING_DIRECTORY;
  220. getLog().debug( "stagingSiteURL NOT specified, using the top level project: " + topLevelURL );
  221. }
  222. // Return either
  223. // usersURL
  224. // or
  225. // topLevelProjectURL + "staging"
  226. return topLevelURL;
  227. }
  228. private String stagingRepoId( final String stagingRepoId )
  229. {
  230. if ( stagingRepoId != null )
  231. {
  232. return stagingRepoId;
  233. }
  234. try
  235. {
  236. return getSite( project ).getId();
  237. }
  238. catch ( MojoExecutionException ex )
  239. {
  240. return "stagingSite";
  241. }
  242. }
  243. }