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.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.model.Site;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.plugins.annotations.ResolutionScope;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Deploys the generated site to a local staging or mock directory based on the site URL
34   * specified in the <code>&lt;distributionManagement&gt;</code> section of the
35   * POM.
36   * <p>
37   * It can be used to test that links between module sites in a multi-module
38   * build work.
39   * </p>
40   *
41   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42   * @version $Id: SiteStageMojo.html 914946 2014-07-03 23:00:36Z hboutemy $
43   * @since 2.0
44   */
45  @Mojo( name = "stage", requiresDependencyResolution = ResolutionScope.TEST )
46  public class SiteStageMojo
47      extends AbstractStagingMojo
48  {
49      /**
50       * Staging directory location. This needs to be an absolute path, like
51       * <code>C:\stagingArea\myProject\</code> on Windows or
52       * <code>/stagingArea/myProject/</code> on Unix.
53       * If this is not specified, the site will be staged in ${project.build.directory}/staging.
54       *
55       * @since 2.3
56       */
57      @Parameter( property = "stagingDirectory" )
58      private File stagingDirectory;
59  
60      /**
61       * Set this to 'true' to skip site generation and staging.
62       *
63       * @since 3.2
64       */
65      @Parameter( property = "maven.site.skip", defaultValue = "false" )
66      private boolean skip;
67  
68      @Override
69      public void execute()
70          throws MojoExecutionException
71      {
72          if ( skip )
73          {
74              getLog().info( "maven.site.skip = true: Skipping site staging" );
75              return;
76          }
77  
78          super.execute();
79      }
80  
81      @Override
82      protected boolean isDeploy()
83      {
84          return false;
85      }
86  
87      @Override
88      protected Site determineDeploySite()
89          throws MojoExecutionException
90      {
91          Site staging = new Site();
92          staging.setId( "stagingLocal" );
93  
94          final File outputDirectory = determineStagingDirectory();
95          getLog().info( "Using this base directory for staging: " + outputDirectory );
96  
97          // Safety
98          if ( !outputDirectory.exists() )
99          {
100             outputDirectory.mkdirs();
101         }
102 
103         staging.setUrl( "file://" + outputDirectory.getAbsolutePath() );
104 
105         return staging;
106     }
107 
108     /**
109      * Find the directory where staging will take place.
110      *
111      * @return the directory for staging
112      */
113     private File determineStagingDirectory()
114     {
115         if ( stagingDirectory != null )
116         {
117             // the user has specified a stagingDirectory - use it
118             getLog().debug( "stagingDirectory specified by the user: " + stagingDirectory );
119             return stagingDirectory;
120         }
121 
122         // The user didn't specify a URL: calculate default in the execution root target dir
123         File defaultStagingDirectory = new File( getExecutionRootBuildDirectory(), DEFAULT_STAGING_DIRECTORY );
124         getLog().debug( "stagingDirectory NOT specified, using the execution root project: " + defaultStagingDirectory );
125         return defaultStagingDirectory;
126     }
127 
128     /**
129      * Find the build directory of the execution root project in the reactor.
130      * If no execution root project is found, the build directory of the current project is returned.
131      *
132      * @return the build directory of the execution root project.
133      */
134     protected File getExecutionRootBuildDirectory()
135     {
136         // Find the top level project in the reactor
137         final MavenProject executionRootProject = getExecutionRootProject( reactorProjects );
138 
139         // Use the top level project's build directory if there is one, otherwise use this project's build directory
140         final File buildDirectory;
141 
142         if ( executionRootProject == null )
143         {
144             getLog().debug( "No execution root project found in the reactor, using the current project." );
145 
146             buildDirectory = new File( project.getBuild().getDirectory() );
147         }
148         else
149         {
150             getLog().debug( "Using the execution root project found in the reactor: " + executionRootProject.getArtifactId() );
151 
152             buildDirectory = new File( executionRootProject.getBuild().getDirectory() );
153         }
154 
155         return buildDirectory;
156     }
157 
158     /**
159      * Find the execution root in the reactor.
160      *
161      * @param reactorProjects The projects in the reactor. May be <code>null</code> in which case <code>null</code> is returned.
162      * @return The execution root project in the reactor, or <code>null</code> if none can be found
163      */
164     private static MavenProject getExecutionRootProject( List<MavenProject> reactorProjects )
165     {
166         if ( reactorProjects == null )
167         {
168             return null;
169         }
170 
171         for ( MavenProject reactorProject : reactorProjects )
172         {
173             if ( reactorProject.isExecutionRoot() )
174             {
175                 return reactorProject;
176             }
177         }
178 
179         return null;
180     }
181 }