You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2012/05/11 18:46:35 UTC

svn commit: r1337279 - in /maven/plugin-tools/branches/MPLUGIN-189: ./ maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/ maven-plugin-plugin/src/it/annotation-with-inheritance/ maven-plugin-tools-annotations/ maven-plugin-tools-api/ mav...

Author: olamy
Date: Fri May 11 16:46:34 2012
New Revision: 1337279

URL: http://svn.apache.org/viewvc?rev=1337279&view=rev
Log:
[MPLUGIN-189] if helpPackageName is not configured change the package of the generic class name to have a similar package as before annotations

Modified:
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-annotations/pom.xml
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/pom.xml
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
    maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/resources/help-class-source.vm
    maven/plugin-tools/branches/MPLUGIN-189/pom.xml

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy Fri May 11 16:46:34 2012
@@ -2,6 +2,12 @@
 File descriptorFile = new File( basedir, "target/classes/META-INF/maven/plugin.xml" );
 assert descriptorFile.isFile()
 
+File oldHelpClass = new File( basedir, "target/classes/HelpMojo.class" );
+assert !oldHelpClass.exists()
+
+File newHelpClass = new File( basedir, "target/classes/org/apache/maven/plugin/coreit/HelpMojo.class" );
+assert newHelpClass.exists()
+
 def pluginDescriptor = new XmlParser().parse( descriptorFile );
 
 def mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "first"}[0]

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy Fri May 11 16:46:34 2012
@@ -4,6 +4,12 @@ assert touchFile.isFile()
 File descriptorFile = new File( basedir, "target/classes/META-INF/maven/plugin.xml" );
 assert descriptorFile.isFile()
 
+File oldHelpClass = new File( basedir, "target/classes/HelpMojo.class" );
+assert !oldHelpClass.exists()
+
+File newHelpClass = new File( basedir, "target/classes/org/apache/maven/plugin/coreit/HelpMojo.class" );
+assert newHelpClass.exists()
+
 def pluginDescriptor = new XmlParser().parse( descriptorFile );
 
 def mojo = pluginDescriptor.mojos.mojo.findAll{ it.goal.text() == "first"}[0]

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-annotations/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-annotations/pom.xml?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-annotations/pom.xml (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-annotations/pom.xml Fri May 11 16:46:34 2012
@@ -64,12 +64,10 @@
     <dependency>
       <groupId>asm</groupId>
       <artifactId>asm</artifactId>
-      <version>3.3.1</version>
     </dependency>
     <dependency>
       <groupId>asm</groupId>
       <artifactId>asm-commons</artifactId>
-      <version>3.3.1</version>
     </dependency>
 
     <dependency>

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/pom.xml?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/pom.xml (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/pom.xml Fri May 11 16:46:34 2012
@@ -88,6 +88,15 @@
       <artifactId>velocity</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>asm</groupId>
+      <artifactId>asm</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>asm</groupId>
+      <artifactId>asm-commons</artifactId>
+    </dependency>
+
     <!-- misc -->
     <dependency>
       <groupId>net.sf.jtidy</groupId>

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java Fri May 11 16:46:34 2012
@@ -32,6 +32,11 @@ import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 import org.codehaus.plexus.util.xml.XMLWriter;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.commons.RemappingClassAdapter;
+import org.objectweb.asm.commons.SimpleRemapper;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -64,6 +69,35 @@ public class PluginDescriptorGenerator
     public void execute( File destinationDirectory, PluginToolsRequest request )
         throws GeneratorException
     {
+
+        File tmpPropertiesFile =
+            new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" );
+
+        if ( tmpPropertiesFile.exists() )
+        {
+            Properties properties = new Properties();
+            try
+            {
+                properties.load( new FileInputStream( tmpPropertiesFile ) );
+            }
+            catch ( IOException e )
+            {
+                throw new GeneratorException( e.getMessage(), e );
+            }
+            String helpPackageName = properties.getProperty( "helpPackageName" );
+            // if helpPackageName property is empty we have to rewrite the class with a better package name than empty
+            if ( StringUtils.isEmpty( helpPackageName ) )
+            {
+                String helpMojoImplementation = rewriteHelpClassToMojoPackage( request );
+                if ( helpMojoImplementation != null )
+                {
+                    // rewrite plugin descriptor with new HelpMojo implementation class
+                    rewriteDescriptor( request.getPluginDescriptor(), helpMojoImplementation );
+                }
+
+            }
+        }
+
         try
         {
             File f = new File( destinationDirectory, "plugin.xml" );
@@ -90,17 +124,6 @@ public class PluginDescriptorGenerator
     {
         PluginDescriptor pluginDescriptor = request.getPluginDescriptor();
 
-        File tmpPropertiesFile =
-            new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" );
-
-        if ( tmpPropertiesFile.exists() )
-        {
-            Properties properties = new Properties();
-            properties.load( new FileInputStream( tmpPropertiesFile ) );
-            //MojoDescriptor mojoDescriptor =
-            //    makeHelpDescriptor( pluginDescriptor, properties.getProperty( "helpPackageName" ) );
-            //pluginDescriptor.addMojo( mojoDescriptor );
-        }
         if ( destinationFile.exists() )
         {
             destinationFile.delete();
@@ -664,4 +687,80 @@ public class PluginDescriptorGenerator
 
         w.endElement();
     }
+
+    protected String rewriteHelpClassToMojoPackage( PluginToolsRequest request )
+        throws GeneratorException
+    {
+        String destinationPackage = PluginHelpGenerator.discoverPackageName( request.getPluginDescriptor() );
+        if ( StringUtils.isEmpty( destinationPackage ) )
+        {
+            return null;
+        }
+        File helpClassFile = new File( request.getProject().getBuild().getOutputDirectory(), "HelpMojo.class" );
+        if ( !helpClassFile.exists() )
+        {
+            return null;
+        }
+        File rewriteHelpClassFile = new File(
+            request.getProject().getBuild().getOutputDirectory() + "/" + StringUtils.replace( destinationPackage, ".",
+                                                                                              "/" ), "HelpMojo.class" );
+        if ( !rewriteHelpClassFile.getParentFile().exists() )
+        {
+            rewriteHelpClassFile.getParentFile().mkdirs();
+        }
+
+        ClassReader cr = null;
+        try
+        {
+            cr = new ClassReader( new FileInputStream( helpClassFile ) );
+        }
+        catch ( IOException e )
+        {
+            throw new GeneratorException( e.getMessage(), e );
+        }
+
+        ClassWriter cw = new ClassWriter( 0 );
+
+        ClassVisitor cv = new RemappingClassAdapter( cw, new SimpleRemapper( "HelpMojo",
+                                                                             StringUtils.replace( destinationPackage,
+                                                                                                  ".", "/" )
+                                                                                 + "/HelpMojo" ) );
+
+        try
+        {
+            cr.accept( cv, ClassReader.EXPAND_FRAMES );
+        }
+        catch ( Throwable e )
+        {
+            throw new GeneratorException( "ASM issue processing classFile " + helpClassFile.getPath(), e );
+        }
+
+        byte[] renamedClass = cw.toByteArray();
+        FileOutputStream fos = null;
+        try
+        {
+            fos = new FileOutputStream( rewriteHelpClassFile );
+            fos.write( renamedClass );
+        }
+        catch ( IOException e )
+        {
+            throw new GeneratorException( "Error rewriting help class: " + e.getMessage(), e );
+        }
+        finally
+        {
+            IOUtil.close( fos );
+        }
+        helpClassFile.delete();
+        return destinationPackage + ".HelpMojo";
+    }
+
+
+    private void rewriteDescriptor( PluginDescriptor pluginDescriptor, String helpMojoImplementation )
+    {
+        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( "help" );
+        if ( mojoDescriptor != null )
+        {
+            mojoDescriptor.setImplementation( helpMojoImplementation );
+        }
+    }
 }

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java Fri May 11 16:46:34 2012
@@ -313,7 +313,7 @@ public class PluginHelpGenerator
      * @param pluginDescriptor not null
      * @return the best name of the package for the generated mojo
      */
-    private static String discoverPackageName( PluginDescriptor pluginDescriptor )
+    protected static String discoverPackageName( PluginDescriptor pluginDescriptor )
     {
         Map packageNames = new HashMap();
         for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); )
@@ -321,6 +321,10 @@ public class PluginHelpGenerator
             MojoDescriptor descriptor = (MojoDescriptor) it.next();
 
             String impl = descriptor.getImplementation();
+            if ( StringUtils.equals( descriptor.getGoal(), "help" ) && StringUtils.equals( "HelpMojo", impl ) )
+            {
+                continue;
+            }
             if ( impl.lastIndexOf( '.' ) != -1 )
             {
                 String name = impl.substring( 0, impl.lastIndexOf( '.' ) );

Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/resources/help-class-source.vm
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/resources/help-class-source.vm?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/resources/help-class-source.vm (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/resources/help-class-source.vm Fri May 11 16:46:34 2012
@@ -81,12 +81,13 @@ private int lineLength;
 private int indentSize;
 
     // groupId/artifactId/version
-    private String pluginDescriptorPath = "${propertiesFilePath}";
+    private String pluginDescriptorPath = "/${propertiesFilePath}";
 
     private Xpp3Dom build()
         throws MojoExecutionException
     {
         // olamy more than one pluginDescriptor in the classloader possible ?
+        getLog().debug("load pluginDescriptorPath: " + pluginDescriptorPath);
         InputStream is = getClass().getResourceAsStream( pluginDescriptorPath );
         try
         {

Modified: maven/plugin-tools/branches/MPLUGIN-189/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/pom.xml?rev=1337279&r1=1337278&r2=1337279&view=diff
==============================================================================
--- maven/plugin-tools/branches/MPLUGIN-189/pom.xml (original)
+++ maven/plugin-tools/branches/MPLUGIN-189/pom.xml Fri May 11 16:46:34 2012
@@ -259,6 +259,17 @@
       </dependency>
 
       <dependency>
+        <groupId>asm</groupId>
+        <artifactId>asm</artifactId>
+        <version>3.3.1</version>
+      </dependency>
+      <dependency>
+        <groupId>asm</groupId>
+        <artifactId>asm-commons</artifactId>
+        <version>3.3.1</version>
+      </dependency>
+
+      <dependency>
         <groupId>org.apache.maven.plugin-testing</groupId>
         <artifactId>maven-plugin-testing-harness</artifactId>
         <version>${pluginTestingHarnessVersion}</version>