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.io.IOException;
24  
25  import java.util.List;
26  import java.util.Locale;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.project.MavenProjectHelper;
30  
31  import org.codehaus.plexus.util.FileUtils;
32  
33  /**
34   * Adds the site descriptor (<code>site.xml</code>) to the list of files to be installed/deployed.
35   * By default, this is enabled only when the project has pom packaging since it will be used by modules inheriting,
36   * but this can be enabled for other projects packaging if needed.
37   *
38   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39   * @version $Id: SiteDescriptorAttachMojo.html 816568 2012-05-08 12:09:38Z hboutemy $
40   * @goal attach-descriptor
41   * @phase package
42   */
43  public class SiteDescriptorAttachMojo
44      extends AbstractSiteMojo
45  {
46      /**
47       * @parameter expression="${basedir}"
48       * @required
49       * @readonly
50       */
51      private File basedir;
52  
53      /**
54       * Maven ProjectHelper.
55       *
56       * @component
57       * @readonly
58       * @since 2.1.1
59       */
60      private MavenProjectHelper projectHelper;
61  
62      /**
63       * @parameter default-value="true"
64       */
65      private boolean pomPackagingOnly;
66  
67      public void execute()
68          throws MojoExecutionException
69      {
70          if ( pomPackagingOnly && !"pom".equals( project.getPackaging() ) )
71          {
72              // http://jira.codehaus.org/browse/MSITE-597
73              return;
74          }
75  
76          List<Locale> localesList = siteTool.getAvailableLocales( locales );
77  
78          for ( Locale locale : localesList )
79          {
80              File descriptorFile = siteTool.getSiteDescriptorFromBasedir(
81                  siteTool.getRelativePath( siteDirectory.getAbsolutePath(), project.getBasedir().getAbsolutePath() ),
82                                                                           basedir, locale );
83  
84              if ( descriptorFile.exists() )
85              {
86                  // Calculate the classifier to use
87                  String classifier = getClassifier( descriptorFile );
88                  // Prepare a file for the interpolated site descriptor
89                  String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
90                  File targetDescriptorFile = new File( project.getBuild().getDirectory(), filename );
91  
92                  try
93                  {
94                      // Copy the site descriptor to a file
95                      FileUtils.copyFile( descriptorFile, targetDescriptorFile );
96                      // Attach the site descriptor
97                      getLog().debug( "Attaching the site descriptor '" + targetDescriptorFile.getAbsolutePath()
98                          + "' with classifier '" + classifier + "' to the project." );
99                      projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile );
100                 }
101                 catch ( IOException e )
102                 {
103                     throw new MojoExecutionException( "Unable to copy site descriptor", e );
104                 }
105             }
106         }
107     }
108 
109     private static String getClassifier( final File descriptorFile )
110         throws MojoExecutionException
111     {
112         final int index = descriptorFile.getName().lastIndexOf( '.' );
113 
114         if ( index > 0 )
115         {
116             return descriptorFile.getName().substring( 0, index );
117         }
118         else
119         {
120             throw new MojoExecutionException( "Unable to determine the classifier to use" );
121         }
122     }
123 }