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