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