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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.doxia.site.decoration.DecorationModel;
24  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
25  import org.apache.maven.doxia.tools.SiteToolException;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.ReaderFactory;
30  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.io.StringReader;
35  import java.util.HashMap;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  /**
42   * Adds the site descriptor (<code>site.xml</code>) to the list of files to be installed/deployed.
43   *
44   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45   * @version $Id: SiteDescriptorAttachMojo.html 816547 2012-05-08 11:51:09Z hboutemy $
46   * @goal attach-descriptor
47   * @phase package
48   */
49  public class SiteDescriptorAttachMojo
50      extends AbstractSiteMojo
51  {
52      /**
53       * @parameter expression="${project.artifact}"
54       * @required
55       * @readonly
56       */
57      private Artifact artifact;
58  
59      /**
60       * @parameter expression="${basedir}"
61       * @required
62       * @readonly
63       */
64      private File basedir;
65  
66      public void execute()
67          throws MojoExecutionException
68      {
69          List localesList = siteTool.getAvailableLocales( locales );
70  
71          for ( Iterator iterator = localesList.iterator(); iterator.hasNext(); )
72          {
73              Locale locale = (Locale) iterator.next();
74  
75              File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(),
76                                                                                       siteDirectory.getAbsolutePath() ),
77                                                                           basedir, locale );
78  
79              if ( descriptorFile.exists() )
80              {
81                  Map props = new HashMap();
82                  props.put( "reports", "<menu ref=\"reports\"/>" );
83                  props.put( "modules", "<menu ref=\"modules\"/>" );
84  
85                  DecorationModel decoration;
86                  try
87                  {
88                      String siteDescriptorContent = IOUtil.toString( ReaderFactory.newXmlReader( descriptorFile ) );
89  
90                      siteDescriptorContent =
91                          siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
92                                                                         getInputEncoding(), getOutputEncoding() );
93  
94                      decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
95                  }
96                  catch ( XmlPullParserException e )
97                  {
98                      throw new MojoExecutionException( "Error parsing site descriptor", e );
99                  }
100                 catch ( IOException e )
101                 {
102                     throw new MojoExecutionException( "Error reading site descriptor", e );
103                 }
104                 catch ( SiteToolException e )
105                 {
106                     throw new MojoExecutionException( "Error when interpoling site descriptor", e );
107                 }
108 
109                 MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository );
110                 if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null )
111                 {
112                     siteTool.populateParentMenu( decoration, locale, project, parentProject, true );
113                 }
114                 try
115                 {
116                     siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true );
117                 }
118                 catch ( SiteToolException e )
119                 {
120                     throw new MojoExecutionException( "Error when populating modules", e );
121                 }
122 
123                 artifact.addMetadata( new SiteDescriptorArtifactMetadata( artifact, decoration, descriptorFile ) );
124             }
125         }
126     }
127 }