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.descriptor;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Locale;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.project.MavenProjectHelper;
31  import org.apache.maven.shared.utils.PathTool;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * Adds the site descriptor (<code>site.xml</code>) to the list of files to be installed/deployed.
36   * <p>For Maven-2.x this is enabled by default only when the project has <code>pom</code> packaging since it will be
37   * used by modules inheriting, but this can be enabled for other projects packaging if needed.</p>
38   * <p>This default execution has been removed from the built-in lifecycle of Maven 3.x for <code>pom</code>-projects.
39   * Users that actually use those projects to provide a common site descriptor for sub modules will need to explicitly
40   * define this goal execution to restore the intended behavior.</p>
41   *
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   *
44   * @since 2.0
45   */
46  @Mojo(name = "attach-descriptor", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
47  public class SiteDescriptorAttachMojo extends AbstractSiteDescriptorMojo {
48      /**
49       */
50      @Parameter(property = "basedir", required = true, readonly = true)
51      private File basedir;
52  
53      /**
54       * Maven ProjectHelper.
55       *
56       * @since 2.1.1
57       */
58      @Component
59      private MavenProjectHelper projectHelper;
60  
61      /**
62       * Attach site descriptor only if packaging is pom.
63       * @since 3.0
64       */
65      @Parameter(defaultValue = "true")
66      private boolean pomPackagingOnly;
67  
68      public void execute() throws MojoExecutionException {
69          if (pomPackagingOnly && !"pom".equals(project.getPackaging())) {
70              // https://issues.apache.org/jira/browse/MSITE-597
71              getLog().info("Skipping because packaging '" + project.getPackaging() + "' is not pom.");
72              return;
73          }
74  
75          boolean attachedSiteDescriptor = false;
76          for (Locale locale : getLocales()) {
77              File descriptorFile = siteTool.getSiteDescriptor(siteDirectory, locale);
78  
79              if (descriptorFile.exists()) {
80                  attachedSiteDescriptor = true;
81  
82                  // Calculate the classifier to use
83                  String classifier = getClassifier(descriptorFile);
84                  // Prepare a file for the interpolated site descriptor
85                  String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
86                  File targetDescriptorFile = new File(project.getBuild().getDirectory(), filename);
87  
88                  try {
89                      // Copy the site descriptor to a file
90                      FileUtils.copyFile(descriptorFile, targetDescriptorFile);
91                      // Attach the site descriptor
92                      getLog().info("Attaching '"
93                              + PathTool.getRelativeFilePath(basedir.getAbsolutePath(), descriptorFile.getAbsolutePath())
94                              + "' site descriptor with classifier '" + classifier + "'.");
95                      projectHelper.attachArtifact(project, "xml", classifier, targetDescriptorFile);
96                  } catch (IOException e) {
97                      throw new MojoExecutionException("Unable to copy site descriptor", e);
98                  }
99              }
100         }
101 
102         if (!attachedSiteDescriptor) {
103             getLog().info("No site descriptor found: nothing to attach.");
104         }
105     }
106 
107     private static String getClassifier(final File descriptorFile) throws MojoExecutionException {
108         final int index = descriptorFile.getName().lastIndexOf('.');
109 
110         if (index > 0) {
111             return descriptorFile.getName().substring(0, index);
112         } else {
113             throw new MojoExecutionException("Unable to determine the classifier to use");
114         }
115     }
116 }