Coverage Report - org.apache.maven.plugins.help.EffectivePomMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EffectivePomMojo
0%
0/56
0%
0/12
4.25
 
 1  
 package org.apache.maven.plugins.help;
 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.IOException;
 23  
 import java.io.StringReader;
 24  
 import java.io.StringWriter;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.apache.maven.model.Model;
 30  
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 31  
 import org.apache.maven.plugin.MojoExecutionException;
 32  
 import org.apache.maven.project.MavenProject;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 35  
 import org.codehaus.plexus.util.xml.XMLWriter;
 36  
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
 37  
 import org.jdom.Document;
 38  
 import org.jdom.JDOMException;
 39  
 import org.jdom.input.SAXBuilder;
 40  
 import org.jdom.output.Format;
 41  
 import org.jdom.output.XMLOutputter;
 42  
 
 43  
 /**
 44  
  * Displays the effective POM as an XML for this build, with the active profiles factored in.
 45  
  *
 46  
  * @version $Id: EffectivePomMojo.java 688495 2008-08-24 11:11:01Z vsiveton $
 47  
  * @since 2.0
 48  
  * @goal effective-pom
 49  
  * @aggregator
 50  
  */
 51  0
 public class EffectivePomMojo
 52  
     extends AbstractEffectiveMojo
 53  
 {
 54  
     // ----------------------------------------------------------------------
 55  
     // Mojo parameters
 56  
     // ----------------------------------------------------------------------
 57  
 
 58  
     /**
 59  
      * The Maven project.
 60  
      *
 61  
      * @since 2.0.2
 62  
      * @parameter expression="${project}"
 63  
      * @required
 64  
      * @readonly
 65  
      */
 66  
     private MavenProject project;
 67  
 
 68  
     /**
 69  
      * The projects in the current build. The effective-POM for
 70  
      * each of these projects will written.
 71  
      *
 72  
      * @parameter expression="${reactorProjects}"
 73  
      * @required
 74  
      * @readonly
 75  
      */
 76  
     private List projects;
 77  
 
 78  
     // ----------------------------------------------------------------------
 79  
     // Public methods
 80  
     // ----------------------------------------------------------------------
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     public void execute()
 84  
         throws MojoExecutionException
 85  
     {
 86  0
         StringWriter w = new StringWriter();
 87  0
         XMLWriter writer =
 88  
             new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
 89  
                                       project.getModel().getModelEncoding(), null );
 90  
 
 91  0
         writeHeader( writer );
 92  
 
 93  
         String effectivePom;
 94  0
         if ( projects.get( 0 ).equals( project ) && projects.size() > 1 )
 95  
         {
 96  
             // outer root element
 97  0
             writer.startElement( "projects" );
 98  0
             for ( Iterator it = projects.iterator(); it.hasNext(); )
 99  
             {
 100  0
                 MavenProject subProject = (MavenProject) it.next();
 101  
 
 102  0
                 writeEffectivePom( subProject, writer );
 103  0
             }
 104  0
             writer.endElement();
 105  
 
 106  0
             effectivePom = w.toString();
 107  0
             effectivePom = prettyFormat( effectivePom );
 108  
         }
 109  
         else
 110  
         {
 111  0
             writeEffectivePom( project, writer );
 112  
 
 113  0
             effectivePom = w.toString();
 114  
         }
 115  
 
 116  0
         if ( output != null )
 117  
         {
 118  
             try
 119  
             {
 120  0
                 writeXmlFile( output, effectivePom, project.getModel().getModelEncoding() );
 121  
             }
 122  0
             catch ( IOException e )
 123  
             {
 124  0
                 throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
 125  0
             }
 126  
 
 127  0
             if ( getLog().isInfoEnabled() )
 128  
             {
 129  0
                 getLog().info( "Effective-POM written to: " + output );
 130  
             }
 131  
         }
 132  
         else
 133  
         {
 134  0
             StringBuffer message = new StringBuffer();
 135  
 
 136  0
             message.append( "\nEffective POMs, after inheritance, interpolation, and profiles are applied:\n\n" );
 137  0
             message.append( effectivePom );
 138  0
             message.append( "\n" );
 139  
 
 140  0
             if ( getLog().isInfoEnabled() )
 141  
             {
 142  0
                 getLog().info( message.toString() );
 143  
             }
 144  
         }
 145  0
     }
 146  
 
 147  
     // ----------------------------------------------------------------------
 148  
     // Private methods
 149  
     // ----------------------------------------------------------------------
 150  
 
 151  
     /**
 152  
      * Method for writing the effective pom informations of the current build.
 153  
      *
 154  
      * @param project the project of the current build, not null.
 155  
      * @param writer the XML writer , not null, not null.
 156  
      * @throws MojoExecutionException if any
 157  
      */
 158  
     private static void writeEffectivePom( MavenProject project, XMLWriter writer )
 159  
         throws MojoExecutionException
 160  
     {
 161  0
         Model pom = project.getModel();
 162  0
         cleanModel( pom );
 163  
 
 164  
         String effectivePom;
 165  
 
 166  0
         StringWriter sWriter = new StringWriter();
 167  0
         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
 168  
         try
 169  
         {
 170  0
             pomWriter.write( sWriter, pom );
 171  
         }
 172  0
         catch ( IOException e )
 173  
         {
 174  0
             throw new MojoExecutionException( "Cannot serialize POM to XML.", e );
 175  0
         }
 176  
 
 177  0
         effectivePom = addMavenNamespace( sWriter.toString(), true );
 178  
 
 179  0
         writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" );
 180  
 
 181  0
         writer.writeMarkup( effectivePom );
 182  0
     }
 183  
 
 184  
     /**
 185  
      * Apply some logic to clean the model before writing it.
 186  
      *
 187  
      * @param pom not null
 188  
      */
 189  
     private static void cleanModel( Model pom )
 190  
     {
 191  0
         Properties properties = new SortedProperties();
 192  0
         properties.putAll( pom.getProperties() );
 193  0
         pom.setProperties( properties );
 194  0
     }
 195  
 
 196  
     /**
 197  
      * @param effectivePom not null
 198  
      * @return pretty format of the xml  or the original <code>effectivePom</code> if an error occurred.
 199  
      */
 200  
     private static String prettyFormat( String effectivePom )
 201  
     {
 202  0
         SAXBuilder builder = new SAXBuilder();
 203  
 
 204  
         try
 205  
         {
 206  0
             Document effectiveDocument = builder.build( new StringReader( effectivePom ) );
 207  
 
 208  0
             StringWriter w = new StringWriter();
 209  0
             Format format = Format.getPrettyFormat();
 210  0
             XMLOutputter out = new XMLOutputter( format );
 211  0
             out.output( effectiveDocument, w );
 212  
 
 213  0
             return w.toString();
 214  
         }
 215  0
         catch ( JDOMException e )
 216  
         {
 217  0
             return effectivePom;
 218  
         }
 219  0
         catch ( IOException e )
 220  
         {
 221  0
             return effectivePom;
 222  
         }
 223  
     }
 224  
 }