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  
23  import org.apache.maven.model.Site;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * Deploys the generated site to a local staging or mock directory based on the site URL
32   * specified in the <code>&lt;distributionManagement&gt;</code> section of the
33   * POM.
34   * <p>It can be used to test that links between module sites in a multi-module
35   * build work.
36   * </p>
37   * <p>This goal requires the site to already have been generated using the site goal,
38   * such as by calling <code>mvn site</code>.</p>
39   *
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   *
42   * @since 2.0
43   */
44  @Mojo(name = "stage", requiresDependencyResolution = ResolutionScope.TEST)
45  public class SiteStageMojo extends AbstractStagingMojo {
46      /**
47       * Staging directory location. This needs to be an absolute path, like
48       * <code>C:\stagingArea\myProject\</code> on Windows or
49       * <code>/stagingArea/myProject/</code> on Unix.
50       * If this is not specified, the site will be staged in ${project.build.directory}/staging.
51       *
52       * @since 2.3
53       */
54      @Parameter(property = "stagingDirectory")
55      private File stagingDirectory;
56  
57      @Override
58      public void execute() throws MojoExecutionException {
59          if (skip) {
60              getLog().info("maven.site.skip = true: Skipping site staging");
61              return;
62          }
63  
64          super.execute();
65      }
66  
67      @Override
68      protected boolean isDeploy() {
69          return false;
70      }
71  
72      @Override
73      protected Site determineDeploySite() throws MojoExecutionException {
74          Site staging = new Site();
75          staging.setId("stagingLocal");
76  
77          final File outputDirectory = determineStagingDirectory();
78          getLog().info("Using this base directory for staging: " + outputDirectory);
79  
80          // Safety
81          if (!outputDirectory.exists()) {
82              outputDirectory.mkdirs();
83          }
84  
85          staging.setUrl("file://" + outputDirectory.getAbsolutePath());
86  
87          return staging;
88      }
89  
90      /**
91       * Find the directory where staging will take place.
92       *
93       * @return the directory for staging
94       */
95      private File determineStagingDirectory() {
96          if (stagingDirectory != null) {
97              // the user has specified a stagingDirectory - use it
98              getLog().debug("stagingDirectory specified by the user: " + stagingDirectory);
99              return stagingDirectory;
100         }
101 
102         // The user didn't specify a URL: calculate default in the execution root target dir
103         File defaultStagingDirectory = new File(getExecutionRootBuildDirectory(), DEFAULT_STAGING_DIRECTORY);
104         getLog().debug("stagingDirectory NOT specified, using the execution root project: " + defaultStagingDirectory);
105         return defaultStagingDirectory;
106     }
107 
108     /**
109      * Find the build directory of the execution root project in the reactor.
110      * If no execution root project is found, the build directory of the current project is returned.
111      *
112      * @return the build directory of the execution root project.
113      */
114     protected File getExecutionRootBuildDirectory() {
115         // Find the top level project in the reactor
116         final MavenProject executionRootProject = mavenSession.getTopLevelProject();
117 
118         // Use the top level project's build directory if there is one, otherwise use this project's build directory
119         final File buildDirectory;
120 
121         if (executionRootProject == null) {
122             getLog().debug("No execution root project found in the reactor, using the current project.");
123 
124             buildDirectory = new File(project.getBuild().getDirectory());
125         } else {
126             getLog().debug("Using the execution root project found in the reactor: "
127                     + executionRootProject.getArtifactId());
128 
129             buildDirectory = new File(executionRootProject.getBuild().getDirectory());
130         }
131 
132         return buildDirectory;
133     }
134 }