View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.deploy;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.model.Site;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * Deploys the generated site to a local staging or mock directory based on the site URL
33   * specified in the <code>&lt;distributionManagement&gt;</code> section of the
34   * POM.
35   * <p>It can be used to test that links between module sites in a multi-module
36   * build work.
37   * </p>
38   * <p>This goal requires the site to already have been generated using the site goal,
39   * such as by calling <code>mvn site</code>.</p>
40   *
41   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42   *
43   * @since 2.0
44   */
45  @Mojo(name = "stage", requiresDependencyResolution = ResolutionScope.TEST)
46  public class SiteStageMojo extends AbstractStagingMojo {
47      /**
48       * Staging directory location. This needs to be an absolute path, like
49       * <code>C:\stagingArea\myProject\</code> on Windows or
50       * <code>/stagingArea/myProject/</code> on Unix.
51       * If this is not specified, the site will be staged in ${project.build.directory}/staging.
52       *
53       * @since 2.3
54       */
55      @Parameter(property = "stagingDirectory")
56      private File stagingDirectory;
57  
58      @Override
59      public void execute() throws MojoExecutionException {
60          if (skip) {
61              getLog().info("maven.site.skip = true: Skipping site staging");
62              return;
63          }
64  
65          super.execute();
66      }
67  
68      @Override
69      protected boolean isDeploy() {
70          return false;
71      }
72  
73      @Override
74      protected Site determineDeploySite() throws MojoExecutionException {
75          Site staging = new Site();
76          staging.setId("stagingLocal");
77  
78          final File outputDirectory = determineStagingDirectory();
79          getLog().info("Using this base directory for staging: " + outputDirectory);
80  
81          // Safety
82          if (!outputDirectory.exists()) {
83              outputDirectory.mkdirs();
84          }
85  
86          staging.setUrl("file://" + outputDirectory.getAbsolutePath());
87  
88          return staging;
89      }
90  
91      /**
92       * Find the directory where staging will take place.
93       *
94       * @return the directory for staging
95       */
96      private File determineStagingDirectory() {
97          if (stagingDirectory != null) {
98              // the user has specified a stagingDirectory - use it
99              getLog().debug("stagingDirectory specified by the user: " + stagingDirectory);
100             return stagingDirectory;
101         }
102 
103         // The user didn't specify a URL: calculate default in the execution root target dir
104         File defaultStagingDirectory = new File(getExecutionRootBuildDirectory(), DEFAULT_STAGING_DIRECTORY);
105         getLog().debug("stagingDirectory NOT specified, using the execution root project: " + defaultStagingDirectory);
106         return defaultStagingDirectory;
107     }
108 
109     /**
110      * Find the build directory of the execution root project in the reactor.
111      * If no execution root project is found, the build directory of the current project is returned.
112      *
113      * @return the build directory of the execution root project.
114      */
115     protected File getExecutionRootBuildDirectory() {
116         // Find the top level project in the reactor
117         final MavenProject executionRootProject = getExecutionRootProject(reactorProjects);
118 
119         // Use the top level project's build directory if there is one, otherwise use this project's build directory
120         final File buildDirectory;
121 
122         if (executionRootProject == null) {
123             getLog().debug("No execution root project found in the reactor, using the current project.");
124 
125             buildDirectory = new File(project.getBuild().getDirectory());
126         } else {
127             getLog().debug("Using the execution root project found in the reactor: "
128                     + executionRootProject.getArtifactId());
129 
130             buildDirectory = new File(executionRootProject.getBuild().getDirectory());
131         }
132 
133         return buildDirectory;
134     }
135 
136     /**
137      * Find the execution root in the reactor.
138      *
139      * @param reactorProjects The projects in the reactor. May be <code>null</code> in which case <code>null</code> is
140      *            returned.
141      * @return The execution root project in the reactor, or <code>null</code> if none can be found
142      */
143     private static MavenProject getExecutionRootProject(List<MavenProject> reactorProjects) {
144         if (reactorProjects == null) {
145             return null;
146         }
147 
148         // todo Lambda Java 1.8
149         for (MavenProject reactorProject : reactorProjects) {
150             if (reactorProject.isExecutionRoot()) {
151                 return reactorProject;
152             }
153         }
154         return null;
155     }
156 }