maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vsive...@apache.org
Subject svn commit: r803437 - in /maven/plugins/trunk/maven-pdf-plugin: pom.xml src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
Date Wed, 12 Aug 2009 10:39:28 GMT
Author: vsiveton
Date: Wed Aug 12 10:39:28 2009
New Revision: 803437

URL: http://svn.apache.org/viewvc?rev=803437&view=rev
Log:
MPDF-26: Add report generation in PDF

o be backward compatible with 2.0.x, hard to find the right way ;)
o revert prerequisites to 2.0.6 and mavenVersion to 2.0.9 (before r803108)
o added commons-logging dep for commons-configuration with fo module

Modified:
    maven/plugins/trunk/maven-pdf-plugin/pom.xml
    maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java

Modified: maven/plugins/trunk/maven-pdf-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/pom.xml?rev=803437&r1=803436&r2=803437&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-pdf-plugin/pom.xml Wed Aug 12 10:39:28 2009
@@ -59,7 +59,7 @@
   </developers>
 
   <prerequisites>
-    <maven>2.1.0</maven>
+    <maven>2.0.6</maven>
   </prerequisites>
 
   <scm>
@@ -74,7 +74,7 @@
 
   <properties>
     <doxiaVersion>1.1.2-SNAPSHOT</doxiaVersion>
-    <mavenVersion>2.1.0</mavenVersion>
+    <mavenVersion>2.0.9</mavenVersion>
   </properties>
 
   <dependencies>
@@ -240,6 +240,13 @@
       <version>1.0-alpha-9</version>
     </dependency>
 
+    <!-- misc -->
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1.1</version>
+    </dependency>
+
     <!-- test dependencies -->
     <dependency>
       <groupId>junit</groupId>
@@ -295,37 +302,40 @@
         </executions>
       </plugin>
 <!-- END SNIPPET: configuration -->
-      <!-- Backward compatibility with Maven 2.0.x, see MNG-3402
-           TODO to be removed. Leaving there only to try to be backward compatible with 2.0.x
MPDF-26
+      <!-- Backward compatibility with Maven 2.0.x, see MNG-3402 -->
       <plugin>
-          <artifactId>maven-shade-plugin</artifactId>
-          <executions>
-              <execution>
-                  <phase>package</phase>
-                  <goals>
-                      <goal>shade</goal>
-                    </goals>
-                  <configuration>
-                      <finalName>${project.build.finalName}</finalName>
-                      <createDependencyReducedPom>
-                          false
-                        </createDependencyReducedPom>
-                      <keepDependenciesWithProvidedScope>
-                          true
-                        </keepDependenciesWithProvidedScope>
-                      <transformers>
-                          <transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"
/>
-                        </transformers>
-                      <artifactSet>
-                          <includes>
-                              <include>org.apache.maven.doxia:doxia-sink-api</include>
-                              <include>org.apache.maven.doxia:doxia-logging-api</include>
-                            </includes>
-                        </artifactSet>
-                    </configuration>
-                </execution>
-            </executions>
-        </plugin> -->
+        <artifactId>maven-shade-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <finalName>${project.build.finalName}</finalName>
+              <createDependencyReducedPom>false</createDependencyReducedPom>
+              <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
+              <transformers>
+                <transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"
/>
+              </transformers>
+              <artifactSet>
+                <includes>
+                  <include>org.apache.maven.doxia:doxia-sink-api</include>
+                  <include>org.apache.maven.doxia:doxia-logging-api</include>
+                </includes>
+              </artifactSet>
+              <filters>
+                <filter>
+                  <artifact>org.apache.maven.doxia:doxia-sink-api</artifact>
+                  <excludes>
+                    <exclude>org/codehaus/doxia/sink/Sink.class</exclude>
+                  </excludes>
+                </filter>
+              </filters>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
     </plugins>
   </build>
 

Modified: maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java?rev=803437&r1=803436&r2=803437&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
(original)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
Wed Aug 12 10:39:28 2009
@@ -21,6 +21,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -30,6 +31,9 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -46,12 +50,14 @@
 import org.apache.maven.doxia.document.DocumentModel;
 import org.apache.maven.doxia.document.DocumentTOCItem;
 import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Writer;
+import org.apache.maven.doxia.logging.Log;
 import org.apache.maven.doxia.markup.HtmlMarkup;
 import org.apache.maven.doxia.module.xdoc.XdocSink;
 import org.apache.maven.doxia.parser.ParseException;
 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.sink.SinkAdapter;
+import org.apache.maven.doxia.sink.SinkEventAttributes;
 import org.apache.maven.doxia.site.decoration.DecorationModel;
 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
 import org.apache.maven.doxia.siterenderer.Renderer;
@@ -84,7 +90,13 @@
 import org.apache.maven.reporting.MavenReportException;
 import org.apache.maven.settings.Settings;
 import org.codehaus.classworlds.ClassRealm;
+import org.codehaus.classworlds.NoSuchRealmException;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.context.Context;
+import org.codehaus.plexus.context.ContextException;
 import org.codehaus.plexus.i18n.I18N;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.PathTool;
@@ -103,7 +115,11 @@
  */
 public class PdfMojo
     extends AbstractMojo
+    implements Contextualizable
 {
+    /** The Maven minor version, i.e. 0 if 2.0.10, 1 if 2.1.0 etc. */
+    private static final int MAVEN_MINOR_VERSION = getMavenMinorVersion();
+
     // ----------------------------------------------------------------------
     // Mojo components
     // ----------------------------------------------------------------------
@@ -353,6 +369,15 @@
      */
     private List generatedMavenReports;
 
+
+    /**
+     * The current Plexus container.
+     * <b>Note</b>: its realm id should be <code>maven-pdf-plugin</code>
+     *
+     * @since 1.1
+     */
+    private PlexusContainer container;
+
     // ----------------------------------------------------------------------
     // Public methods
     // ----------------------------------------------------------------------
@@ -411,6 +436,13 @@
         }
     }
 
+    /** {@inheritDoc} */
+    public void contextualize( Context context )
+        throws ContextException
+    {
+        container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
+    }
+
     // ----------------------------------------------------------------------
     // Private methods
     // ----------------------------------------------------------------------
@@ -1036,12 +1068,16 @@
     private MavenReport getMavenReport( MojoDescriptor mojoDescriptor )
         throws MojoExecutionException
     {
+        /*
+         * container.getContainerRealm().getId() == maven-pdf-plugin
+         * session.getContainer().getContainerRealm().getId() == plexus.core
+         * mojoDescriptor.getPluginDescriptor().getClassRealm().getId() == a report plugin
+         */
         ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
         try
         {
-            Thread.currentThread().setContextClassLoader(
-                                                          mojoDescriptor.getPluginDescriptor().getClassRealm()
-                                                                        .getClassLoader()
);
+            Thread.currentThread()
+                  .setContextClassLoader( getClassRealm( mojoDescriptor.getPluginDescriptor()
).getClassLoader() );
 
             MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
 
@@ -1070,6 +1106,50 @@
     }
 
     /**
+     * Get the Classworlds ClassRealm instance from the given plugin descriptor.
+     * <br/>
+     * <b>Note</b>: For backward compatibility with Maven 2.0.x, some Doxia 1.1
interfaces will be imported from
+     * this Plexus container.
+     *
+     * @param pluginDescriptor not null
+     * @return the plugin descriptor ClassRealm with Doxia 1.1 interfaces in the Maven 2.0.x
case.
+     * @since 1.1
+     * @see #MAVEN_MINOR_VERSION
+     */
+    private ClassRealm getClassRealm( PluginDescriptor pluginDescriptor)
+    {
+        if ( MAVEN_MINOR_VERSION == 0 )
+        {
+            String[] doxiaInterfaces =
+                new String[] { Sink.class.getName(), SinkEventAttributes.class.getName(),
Log.class.getName() };
+
+            if ( getLog().isDebugEnabled() )
+            {
+                getLog().debug(
+                                "To be backward compatible with mvn 2.0.x, added Doxia 1.1.x
interfaces from the PDF realm ("
+                                    + container.getContainerRealm().getId() + ") to the report
plugin realm ("
+                                    + pluginDescriptor.getClassRealm().getId() + "): "
+                                    + StringUtils.join( doxiaInterfaces, ", " ) );
+            }
+
+            try
+            {
+                for ( int i = 0; i < doxiaInterfaces.length; i++ )
+                {
+                    pluginDescriptor.getClassRealm().importFrom( container.getContainerRealm().getId(),
+                                                                 doxiaInterfaces[i] );
+                }
+            }
+            catch ( NoSuchRealmException e )
+            {
+                getLog().error( "NoSuchRealmException: " + e.getMessage(), e );
+            }
+        }
+
+        return pluginDescriptor.getClassRealm();
+    }
+
+    /**
      * Generate the given Maven report only if it is not an external report and the report
could be generated.
      *
      * @param mojoDescriptor not null, to catch linkage error
@@ -1585,6 +1665,66 @@
         return excludesLocales;
     }
 
+     /**
+    * @return the current Maven version from <code>META-INF/maven/org.apache.maven/maven-core/pom.properties</code>
+    * or <code>null</code> if not found.
+    */
+    private static String getMavenVersion()
+    {
+        InputStream is = null;
+        try
+        {
+            is =
+                PdfMojo.class.getClassLoader()
+                             .getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties"
);
+
+            if ( is != null )
+            {
+                final Properties properties = new Properties();
+                properties.load( is );
+
+                return properties.getProperty( "version" );
+            }
+        }
+        catch ( IOException e )
+        {
+            // nop
+        }
+        finally
+        {
+            IOUtil.close( is );
+        }
+
+        return null;
+    }
+
+    /**
+     * All Maven versions respect this convention:
+     * <pre>
+     * &lt;major&gt;.&lt;minor&gt;.&lt;revision&gt;
+     * </pre>
+     *
+     * @return the Maven minor version as int or 0.
+     * @see #getMavenVersion()
+     */
+    private static int getMavenMinorVersion()
+    {
+        final String mvnVersion = getMavenVersion();
+        if ( mvnVersion == null )
+        {
+            return 0;
+        }
+
+        final Pattern pattern = Pattern.compile( "(\\d+)\\.{1}(\\d+)(\\.{1}(\\d+))?" );
+        final Matcher matcher = pattern.matcher( mvnVersion );
+        if ( matcher.find() )
+        {
+            return Integer.valueOf( matcher.group( 2 ) ).intValue();
+        }
+
+        return 0;
+    }
+
     // ----------------------------------------------------------------------
     // Inner class
     // ----------------------------------------------------------------------



Mime
View raw message