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