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