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  import java.io.StringReader;
25  import java.io.Writer;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.apache.maven.doxia.site.decoration.DecorationModel;
33  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
34  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
35  import org.apache.maven.doxia.tools.SiteToolException;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.MavenProjectHelper;
39  import org.codehaus.plexus.util.IOUtil;
40  import org.codehaus.plexus.util.ReaderFactory;
41  import org.codehaus.plexus.util.WriterFactory;
42  import org.codehaus.plexus.util.xml.XmlStreamReader;
43  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44  
45  /**
46   * Adds the site descriptor (<code>site.xml</code>) to the list of files to be installed/deployed.
47   *
48   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
49   * @version $Id: SiteDescriptorAttachMojo.html 816556 2012-05-08 11:58:34Z hboutemy $
50   * @goal attach-descriptor
51   * @phase package
52   */
53  public class SiteDescriptorAttachMojo
54      extends AbstractSiteMojo
55  {
56  
57      /**
58       * @parameter expression="${basedir}"
59       * @required
60       * @readonly
61       */
62      private File basedir;
63  
64      /**
65       * Maven ProjectHelper.
66       *
67       * @component
68       * @readonly
69       * @since 2.1.1
70       */
71      private MavenProjectHelper projectHelper;
72  
73      public void execute()
74          throws MojoExecutionException
75      {
76          List<Locale> localesList = siteTool.getAvailableLocales( locales );
77  
78          for ( Locale locale : localesList )
79          {
80              File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(),
81                                                                                       siteDirectory.getAbsolutePath() ),
82                                                                           basedir, locale );
83  
84              if ( descriptorFile.exists() )
85              {
86                  Map<String, String> props = new HashMap<String, String>();
87                  props.put( "reports", "<menu ref=\"reports\"/>" );
88                  props.put( "modules", "<menu ref=\"modules\"/>" );
89  
90                  DecorationModel decoration;
91                  XmlStreamReader reader = null;
92                  try
93                  {
94                      reader = ReaderFactory.newXmlReader( descriptorFile );
95                      String siteDescriptorContent = IOUtil.toString( reader );
96  
97                      siteDescriptorContent =
98                          siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
99                                                                         getInputEncoding(), getOutputEncoding() );
100 
101                     decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
102                 }
103                 catch ( XmlPullParserException e )
104                 {
105                     throw new MojoExecutionException( "Error parsing site descriptor", e );
106                 }
107                 catch ( IOException e )
108                 {
109                     throw new MojoExecutionException( "Error reading site descriptor", e );
110                 }
111                 catch ( SiteToolException e )
112                 {
113                     throw new MojoExecutionException( "Error when interpolating site descriptor", e );
114                 }
115                 finally
116                 {
117                     IOUtils.closeQuietly( reader );
118                 }
119 
120                 MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository );
121                 if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null )
122                 {
123                     siteTool.populateParentMenu( decoration, locale, project, parentProject, true );
124                 }
125                 try
126                 {
127                     siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true );
128                 }
129                 catch ( SiteToolException e )
130                 {
131                     throw new MojoExecutionException( "Error when populating modules", e );
132                 }
133 
134                 // Calculate the classifier to use
135                 String classifier = null;
136                 int index = descriptorFile.getName().lastIndexOf( '.' );
137                 if ( index > 0 )
138                 {
139                     classifier = descriptorFile.getName().substring( 0, index );
140                 }
141                 else
142                 {
143                     throw new MojoExecutionException( "Unable to determine the classifier to use" );
144                 }
145 
146                 // Prepare a file for the interpolated site descriptor
147                 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
148                 File interpolatedDescriptorFile = new File( project.getBuild().getDirectory(), filename );
149                 interpolatedDescriptorFile.getParentFile().mkdirs();
150 
151                 Writer writer = null;
152                 try
153                 {
154                     // Write the interpolated site descriptor to a file
155                     writer = WriterFactory.newXmlWriter( interpolatedDescriptorFile );
156                     new DecorationXpp3Writer().write( writer, decoration );
157                     // Attach the interpolated site descriptor
158                     getLog().debug( "Attaching the site descriptor '" + interpolatedDescriptorFile.getAbsolutePath()
159                         + "' with classifier '" + classifier + "' to the project." );
160                     projectHelper.attachArtifact( project, "xml", classifier, interpolatedDescriptorFile );
161                 }
162                 catch ( IOException e )
163                 {
164                     throw new MojoExecutionException( "Unable to store interpolated site descriptor", e );
165                 }
166                 finally
167                 {
168                     IOUtils.closeQuietly( writer );
169                 }
170             }
171         }
172     }
173 }