Coverage Report - org.apache.maven.plugins.site.SiteDescriptorAttachMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteDescriptorAttachMojo
0%
0/47
0%
0/12
0
 
 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: org.apache.maven.plugins.site.SiteDescriptorAttachMojo.html 816556 2012-05-08 11:58:34Z hboutemy $
 50  
  * @goal attach-descriptor
 51  
  * @phase package
 52  
  */
 53  0
 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  0
         List<Locale> localesList = siteTool.getAvailableLocales( locales );
 77  
 
 78  0
         for ( Locale locale : localesList )
 79  
         {
 80  0
             File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(),
 81  
                                                                                      siteDirectory.getAbsolutePath() ),
 82  
                                                                          basedir, locale );
 83  
 
 84  0
             if ( descriptorFile.exists() )
 85  
             {
 86  0
                 Map<String, String> props = new HashMap<String, String>();
 87  0
                 props.put( "reports", "<menu ref=\"reports\"/>" );
 88  0
                 props.put( "modules", "<menu ref=\"modules\"/>" );
 89  
 
 90  
                 DecorationModel decoration;
 91  0
                 XmlStreamReader reader = null;
 92  
                 try
 93  
                 {
 94  0
                     reader = ReaderFactory.newXmlReader( descriptorFile );
 95  0
                     String siteDescriptorContent = IOUtil.toString( reader );
 96  
 
 97  0
                     siteDescriptorContent =
 98  
                         siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
 99  
                                                                        getInputEncoding(), getOutputEncoding() );
 100  
 
 101  0
                     decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
 102  
                 }
 103  0
                 catch ( XmlPullParserException e )
 104  
                 {
 105  0
                     throw new MojoExecutionException( "Error parsing site descriptor", e );
 106  
                 }
 107  0
                 catch ( IOException e )
 108  
                 {
 109  0
                     throw new MojoExecutionException( "Error reading site descriptor", e );
 110  
                 }
 111  0
                 catch ( SiteToolException e )
 112  
                 {
 113  0
                     throw new MojoExecutionException( "Error when interpolating site descriptor", e );
 114  
                 }
 115  
                 finally
 116  
                 {
 117  0
                     IOUtils.closeQuietly( reader );
 118  0
                 }
 119  
 
 120  0
                 MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository );
 121  0
                 if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null )
 122  
                 {
 123  0
                     siteTool.populateParentMenu( decoration, locale, project, parentProject, true );
 124  
                 }
 125  
                 try
 126  
                 {
 127  0
                     siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true );
 128  
                 }
 129  0
                 catch ( SiteToolException e )
 130  
                 {
 131  0
                     throw new MojoExecutionException( "Error when populating modules", e );
 132  0
                 }
 133  
 
 134  
                 // Calculate the classifier to use
 135  0
                 String classifier = null;
 136  0
                 int index = descriptorFile.getName().lastIndexOf( '.' );
 137  0
                 if ( index > 0 )
 138  
                 {
 139  0
                     classifier = descriptorFile.getName().substring( 0, index );
 140  
                 }
 141  
                 else
 142  
                 {
 143  0
                     throw new MojoExecutionException( "Unable to determine the classifier to use" );
 144  
                 }
 145  
 
 146  
                 // Prepare a file for the interpolated site descriptor
 147  0
                 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
 148  0
                 File interpolatedDescriptorFile = new File( project.getBuild().getDirectory(), filename );
 149  0
                 interpolatedDescriptorFile.getParentFile().mkdirs();
 150  
 
 151  0
                 Writer writer = null;
 152  
                 try
 153  
                 {
 154  
                     // Write the interpolated site descriptor to a file
 155  0
                     writer = WriterFactory.newXmlWriter( interpolatedDescriptorFile );
 156  0
                     new DecorationXpp3Writer().write( writer, decoration );
 157  
                     // Attach the interpolated site descriptor
 158  0
                     getLog().debug( "Attaching the site descriptor '" + interpolatedDescriptorFile.getAbsolutePath()
 159  
                         + "' with classifier '" + classifier + "' to the project." );
 160  0
                     projectHelper.attachArtifact( project, "xml", classifier, interpolatedDescriptorFile );
 161  
                 }
 162  0
                 catch ( IOException e )
 163  
                 {
 164  0
                     throw new MojoExecutionException( "Unable to store interpolated site descriptor", e );
 165  
                 }
 166  
                 finally
 167  
                 {
 168  0
                     IOUtils.closeQuietly( writer );
 169  0
                 }
 170  
             }
 171  0
         }
 172  0
     }
 173  
 }