View Javadoc

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  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          StringWriter w = new StringWriter();
87          XMLWriter writer =
88              new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
89                                        project.getModel().getModelEncoding(), null );
90  
91          writeHeader( writer );
92  
93          String effectivePom;
94          if ( projects.get( 0 ).equals( project ) && projects.size() > 1 )
95          {
96              // outer root element
97              writer.startElement( "projects" );
98              for ( Iterator it = projects.iterator(); it.hasNext(); )
99              {
100                 MavenProject subProject = (MavenProject) it.next();
101 
102                 writeEffectivePom( subProject, writer );
103             }
104             writer.endElement();
105 
106             effectivePom = w.toString();
107             effectivePom = prettyFormat( effectivePom );
108         }
109         else
110         {
111             writeEffectivePom( project, writer );
112 
113             effectivePom = w.toString();
114         }
115 
116         if ( output != null )
117         {
118             try
119             {
120                 writeXmlFile( output, effectivePom, project.getModel().getModelEncoding() );
121             }
122             catch ( IOException e )
123             {
124                 throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
125             }
126 
127             if ( getLog().isInfoEnabled() )
128             {
129                 getLog().info( "Effective-POM written to: " + output );
130             }
131         }
132         else
133         {
134             StringBuffer message = new StringBuffer();
135 
136             message.append( "\nEffective POMs, after inheritance, interpolation, and profiles are applied:\n\n" );
137             message.append( effectivePom );
138             message.append( "\n" );
139 
140             if ( getLog().isInfoEnabled() )
141             {
142                 getLog().info( message.toString() );
143             }
144         }
145     }
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         Model pom = project.getModel();
162         cleanModel( pom );
163 
164         String effectivePom;
165 
166         StringWriter sWriter = new StringWriter();
167         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
168         try
169         {
170             pomWriter.write( sWriter, pom );
171         }
172         catch ( IOException e )
173         {
174             throw new MojoExecutionException( "Cannot serialize POM to XML.", e );
175         }
176 
177         effectivePom = addMavenNamespace( sWriter.toString(), true );
178 
179         writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" );
180 
181         writer.writeMarkup( effectivePom );
182     }
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         Properties properties = new SortedProperties();
192         properties.putAll( pom.getProperties() );
193         pom.setProperties( properties );
194     }
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         SAXBuilder builder = new SAXBuilder();
203 
204         try
205         {
206             Document effectiveDocument = builder.build( new StringReader( effectivePom ) );
207 
208             StringWriter w = new StringWriter();
209             Format format = Format.getPrettyFormat();
210             XMLOutputter out = new XMLOutputter( format );
211             out.output( effectiveDocument, w );
212 
213             return w.toString();
214         }
215         catch ( JDOMException e )
216         {
217             return effectivePom;
218         }
219         catch ( IOException e )
220         {
221             return effectivePom;
222         }
223     }
224 }