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