maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vsive...@apache.org
Subject svn commit: r688412 - in /maven/plugins/trunk/maven-help-plugin: ./ src/main/java/org/apache/maven/plugins/help/
Date Sat, 23 Aug 2008 20:53:35 GMT
Author: vsiveton
Date: Sat Aug 23 13:53:35 2008
New Revision: 688412

URL: http://svn.apache.org/viewvc?rev=688412&view=rev
Log:
MPH-40: help:effective-pom emits invalid XML to output file

o output of effective pom/settings are now full xml

Added:
    maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
  (with props)
Modified:
    maven/plugins/trunk/maven-help-plugin/pom.xml
    maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
    maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java

Modified: maven/plugins/trunk/maven-help-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/pom.xml?rev=688412&r1=688411&r2=688412&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-help-plugin/pom.xml Sat Aug 23 13:53:35 2008
@@ -110,6 +110,11 @@
       <artifactId>maven-plugin-tools-api</artifactId>
       <version>2.4.3</version>
     </dependency>
+    <dependency>
+      <groupId>jdom</groupId>
+      <artifactId>jdom</artifactId>
+      <version>1.0</version>
+    </dependency>
   </dependencies>
   <reporting>
     <plugins>

Added: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java?rev=688412&view=auto
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
(added)
+++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
Sat Aug 23 13:53:35 2008
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.help;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Locale;
+
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.WriterFactory;
+import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.XmlWriterUtil;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.Namespace;
+import org.jdom.filter.ElementFilter;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
+
+/**
+ * Base class with common utilities to write effective Pom/settings.
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ * @since 2.1
+ */
+public abstract class AbstractEffectiveMojo
+    extends AbstractHelpMojo
+{
+    /** The POM XSD URL */
+    private static final String POM_XSD_URL ="http://maven.apache.org/maven-v4_0_0.xsd";
+
+    /** The Settings XSD URL */
+    private static final String SETTINGS_XSD_URL ="http://maven.apache.org/xsd/settings-1.0.0.xsd";
+
+    /**
+     * Utility method to write an XML content in a given file.
+     *
+     * @param output is the wanted output file.
+     * @param content contains the XML content to be written to the file.
+     * @param encoding is the wanted encoding to use when writing file.
+     * @throws IOException if any
+     * @see AbstractHelpMojo#writeFile(File, String) if encoding is null.
+     */
+    protected static void writeXmlFile( File output, String content, String encoding )
+        throws IOException
+    {
+        if ( output == null )
+        {
+            return;
+        }
+
+        if ( StringUtils.isEmpty( encoding ) )
+        {
+            writeFile( output, content );
+            return;
+        }
+
+        Writer out = null;
+        try
+        {
+            output.getParentFile().mkdirs();
+
+            out = WriterFactory.newXmlWriter( output );
+
+            out.write( content );
+
+            out.flush();
+        }
+        finally
+        {
+            IOUtil.close( out );
+        }
+    }
+
+    /**
+     * Write comments in the Effective POM/settings header.
+     *
+     * @param writer not null
+     */
+    protected static void writeHeader( XMLWriter writer )
+    {
+        XmlWriterUtil.writeCommentLineBreak( writer );
+        XmlWriterUtil.writeComment( writer, " " );
+        DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT,
Locale.US );
+        XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on "
+            + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
+        XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/"
);
+        XmlWriterUtil.writeComment( writer, " " );
+        XmlWriterUtil.writeCommentLineBreak( writer );
+
+        XmlWriterUtil.writeLineBreak( writer );
+    }
+
+    /**
+     * Write comments in a normalize way.
+     *
+     * @param writer not null
+     * @param comment not null
+     */
+    protected static void writeComment( XMLWriter writer, String comment )
+    {
+        XmlWriterUtil.writeCommentLineBreak( writer );
+        XmlWriterUtil.writeComment( writer, " " );
+        XmlWriterUtil.writeComment( writer, comment );
+        XmlWriterUtil.writeComment( writer, " " );
+        XmlWriterUtil.writeCommentLineBreak( writer );
+
+        XmlWriterUtil.writeLineBreak( writer );
+    }
+
+    /**
+     * Add a Pom/Settings namespaces to the effective XML content.
+     *
+     * @param effectiveXml not null the effective POM or Settings
+     * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the
settings xsd url.
+     * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt;
with the Maven namespace
+     * or the original <code>effective</code> if an error occurred.
+     * @see #POM_XSD_URL
+     * @see #SETTINGS_XSD_URL
+     */
+    protected static String addMavenNamespace( String effectiveXml, boolean isPom )
+    {
+        SAXBuilder builder = new SAXBuilder();
+
+        try
+        {
+            Document document = builder.build( new StringReader( effectiveXml ) );
+            Element rootElement = document.getRootElement();
+
+            // added namespaces
+            Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/4.0.0"
);
+            rootElement.setNamespace( pomNamespace );
+
+            Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance"
);
+            rootElement.addNamespaceDeclaration( xsiNamespace );
+            if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null )
+            {
+                rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/4.0.0
"
+                    + ( isPom ? POM_XSD_URL : SETTINGS_XSD_URL ), xsiNamespace );
+            }
+
+            ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace( "" )
);
+            for ( Iterator i = rootElement.getDescendants( elementFilter ); i.hasNext();
)
+            {
+                Element e = (Element) i.next();
+                e.setNamespace( pomNamespace );
+            }
+
+            StringWriter w = new StringWriter();
+            Format format = Format.getPrettyFormat();
+            XMLOutputter out = new XMLOutputter( format );
+            out.output( document.getRootElement(), w );
+
+            return w.toString();
+        }
+        catch ( JDOMException e )
+        {
+            return effectiveXml;
+        }
+        catch ( IOException e )
+        {
+            return effectiveXml;
+        }
+    }
+}

Propchange: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java?rev=688412&r1=688411&r2=688412&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
(original)
+++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
Sat Aug 23 13:53:35 2008
@@ -19,20 +19,28 @@
  * under the License.
  */
 
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.List;
+
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.util.StringUtils;
-
-import java.io.IOException;
-import java.io.StringWriter;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
+import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
+import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.XmlWriterUtil;
+import org.jdom.Document;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
 
 /**
- * Displays the effective POM for this build, with the active profiles factored in.
+ * Displays the effective POM as an XML for this build, with the active profiles factored
in.
  *
  * @version $Id$
  * @since 2.0
@@ -40,7 +48,7 @@
  * @aggregator
  */
 public class EffectivePomMojo
-    extends AbstractHelpMojo
+    extends AbstractEffectiveMojo
 {
     // ----------------------------------------------------------------------
     // Mojo parameters
@@ -74,37 +82,41 @@
     public void execute()
         throws MojoExecutionException
     {
-        StringBuffer message = new StringBuffer();
+        StringWriter w = new StringWriter();
+        XMLWriter writer =
+            new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE
),
+                                      project.getModel().getModelEncoding(), null );
 
-        if ( projects.get( 0 ).equals( project ) )
-        {
-            // this is normal in aggregation mode.
+        writeHeader( writer );
 
+        String effectivePom;
+        if ( projects.get( 0 ).equals( project ) && projects.size() > 1 )
+        {
+            // outer root element
+            writer.startElement( "projects" );
             for ( Iterator it = projects.iterator(); it.hasNext(); )
             {
-                MavenProject project = (MavenProject) it.next();
-
-                getEffectivePom( project, message );
+                MavenProject subProject = (MavenProject) it.next();
 
-                message.append( "\n" );
+                writeEffectivePom( subProject, writer );
             }
+            writer.endElement();
+
+            effectivePom = w.toString();
+            effectivePom = prettyFormat( effectivePom );
         }
         else
         {
-            getEffectivePom( project, message );
-            message.append( "\n" );
+            writeEffectivePom( project, writer );
+
+            effectivePom = w.toString();
         }
 
         if ( output != null )
         {
-            StringBuffer sb = new StringBuffer();
-            sb.append( "Created by: " + getClass().getName() ).append( "\n" );
-            sb.append( "Created on: " + new Date() ).append( "\n" ).append( "\n" );
-            sb.append( message.toString() );
-
             try
             {
-                writeFile( output, sb );
+                writeXmlFile( output, effectivePom, project.getModel().getModelEncoding()
);
             }
             catch ( IOException e )
             {
@@ -118,15 +130,15 @@
         }
         else
         {
-            StringBuffer formatted = new StringBuffer();
+            StringBuffer message = new StringBuffer();
 
-            formatted.append( "\nEffective POMs, after inheritance, interpolation, and profiles
are applied:\n\n" );
-            formatted.append( message.toString() );
-            formatted.append( "\n" );
+            message.append( "\nEffective POMs, after inheritance, interpolation, and profiles
are applied:\n\n" );
+            message.append( effectivePom );
+            message.append( "\n" );
 
             if ( getLog().isInfoEnabled() )
             {
-                getLog().info( message );
+                getLog().info( message.toString() );
             }
         }
     }
@@ -136,35 +148,62 @@
     // ----------------------------------------------------------------------
 
     /**
-     * Method for displaying the effective pom information of the current build
+     * Method for writing the effective pom informations of the current build.
      *
-     * @param project   the project of the current build
-     * @param message   the information to be displayed
-     * @throws MojoExecutionException
+     * @param project the project of the current build, not null.
+     * @param writer the XML writer , not null, not null.
+     * @throws MojoExecutionException if any
      */
-    private void getEffectivePom( MavenProject project, StringBuffer message )
+    private static void writeEffectivePom( MavenProject project, XMLWriter writer )
         throws MojoExecutionException
     {
         Model pom = project.getModel();
+        String effectivePom;
 
         StringWriter sWriter = new StringWriter();
-
         MavenXpp3Writer pomWriter = new MavenXpp3Writer();
-
         try
         {
             pomWriter.write( sWriter, pom );
-
-            message.append( "\n" ).append( StringUtils.repeat( "=", LINE_LENGTH ) );
-            message.append( "\nEffective POM for project \'" + project.getId() + "\'" );
-            message.append( "\n" ).append( StringUtils.repeat( "=", LINE_LENGTH ) );
-            message.append( "\n" );
-            message.append( sWriter.toString() );
-            message.append( "\n" ).append( StringUtils.repeat( "=", LINE_LENGTH ) );
         }
         catch ( IOException e )
         {
             throw new MojoExecutionException( "Cannot serialize POM to XML.", e );
         }
+
+        effectivePom = addMavenNamespace( sWriter.toString(), true );
+
+        writeComment( writer, "Effective POM for project \'" + project.getId() + "\'" );
+
+        writer.writeMarkup( effectivePom );
+    }
+
+    /**
+     * @param effectivePom not null
+     * @return pretty format of the xml  or the original <code>effectivePom</code>
if an error occurred.
+     */
+    private static String prettyFormat( String effectivePom )
+    {
+        SAXBuilder builder = new SAXBuilder();
+
+        try
+        {
+            Document effectiveDocument = builder.build( new StringReader( effectivePom )
);
+
+            StringWriter w = new StringWriter();
+            Format format = Format.getPrettyFormat();
+            XMLOutputter out = new XMLOutputter( format );
+            out.output( effectiveDocument, w );
+
+            return w.toString();
+        }
+        catch ( JDOMException e )
+        {
+            return effectivePom;
+        }
+        catch ( IOException e )
+        {
+            return effectivePom;
+        }
     }
 }

Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java?rev=688412&r1=688411&r2=688412&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
(original)
+++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
Sat Aug 23 13:53:35 2008
@@ -21,6 +21,8 @@
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Iterator;
 
 import org.apache.maven.plugin.MojoExecutionException;
@@ -29,9 +31,12 @@
 import org.apache.maven.settings.Settings;
 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
+import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.XmlWriterUtil;
 
 /**
- * Displays the calculated settings for this project, given any profile enhancement and the
inheritance
+ * Displays the calculated settings as XML for this project, given any profile enhancement
and the inheritance
  * of the global settings into the user-level settings.
  *
  * @version $Id$
@@ -40,7 +45,7 @@
  * @requiresProject false
  */
 public class EffectiveSettingsMojo
-    extends AbstractHelpMojo
+    extends AbstractEffectiveMojo
 {
     // ----------------------------------------------------------------------
     // Mojo parameters
@@ -48,7 +53,7 @@
 
     /**
      * The system settings for Maven. This is the instance resulting from
-     * merging global- and user-level settings files.
+     * merging global and user-level settings files.
      *
      * @parameter expression="${settings}"
      * @readonly
@@ -57,7 +62,7 @@
     private Settings settings;
 
     /**
-     * For security reasons, all passwords are hidden by default. Set this to 'true' to show
all passwords.
+     * For security reasons, all passwords are hidden by default. Set this to <code>true</code>
to show all passwords.
      *
      * @since 2.1
      * @parameter expression="${showPasswords}" default-value="false"
@@ -83,24 +88,22 @@
             hidePasswords( copySettings );
         }
 
-        StringWriter sWriter = new StringWriter();
+        StringWriter w = new StringWriter();
+        XMLWriter writer =
+            new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE
),
+                                      copySettings.getModelEncoding(), null );
 
-        SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
+        writeHeader( writer );
 
-        try
-        {
-            settingsWriter.write( sWriter, copySettings );
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( "Cannot serialize Settings to XML.", e );
-        }
+        writeEffectiveSettings( copySettings, writer );
+
+        String effectiveSettings = w.toString();
 
         if ( output != null )
         {
             try
             {
-                writeFile( output, sWriter.toString() );
+                writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding()
);
             }
             catch ( IOException e )
             {
@@ -116,11 +119,14 @@
         {
             StringBuffer message = new StringBuffer();
 
-            message.append( "\nEffective settings:\n\n" );
-            message.append( sWriter.toString() );
-            message.append( "\n\n" );
+            message.append( "\nEffective user-specific configuration settings:\n\n" );
+            message.append( effectiveSettings );
+            message.append( "\n" );
 
-            getLog().info( message );
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( message.toString() );
+            }
         }
     }
 
@@ -133,7 +139,7 @@
      *
      * @param aSettings not null
      */
-    private void hidePasswords( Settings aSettings )
+    private static void hidePasswords( Settings aSettings )
     {
         for ( Iterator it = aSettings.getProxies().iterator(); it.hasNext(); )
         {
@@ -157,7 +163,7 @@
     }
 
     /**
-     * TODO: should be replaced by SettingsUtils#copySettings()
+     * TODO: should be replaced by SettingsUtils#copySettings() in 2.0.10+.
      *
      * @param settings could be null
      * @return a new instance of settings or null if settings was null.
@@ -185,4 +191,64 @@
 
         return clone;
     }
+
+    /**
+     * Method for writing the effective settings informations.
+     *
+     * @param settings the settings, not null.
+     * @param writer the XML writer used, not null.
+     * @throws MojoExecutionException if any
+     */
+    private static void writeEffectiveSettings( Settings settings, XMLWriter writer )
+        throws MojoExecutionException
+    {
+        String effectiveSettings;
+
+        StringWriter sWriter = new StringWriter();
+        SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
+        try
+        {
+            settingsWriter.write( sWriter, settings );
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Cannot serialize Settings to XML.", e );
+        }
+
+        effectiveSettings = addMavenNamespace( sWriter.toString(), false );
+
+        writeComment( writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName()
+ "'" );
+
+        writer.writeMarkup( effectiveSettings );
+    }
+
+    /**
+     * @return the current host name or <code>unknown</code> if error
+     * @see InetAddress#getLocalHost()
+     */
+    private static String getHostName()
+    {
+        try
+        {
+            return InetAddress.getLocalHost().getHostName();
+        }
+        catch ( UnknownHostException e )
+        {
+            return "unknown";
+        }
+    }
+
+    /**
+     * @return the user name or <code>unknown</code> if <code>user.name</code>
is not a system property.
+     */
+    private static String getUserName()
+    {
+        String userName = System.getProperty( "user.name" );
+        if ( StringUtils.isEmpty( userName ))
+        {
+            return "unknown";
+        }
+
+        return userName;
+    }
 }



Mime
View raw message