maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hbout...@apache.org
Subject svn commit: r1405260 - in /maven/plugin-tools/trunk: maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
Date Sat, 03 Nov 2012 02:15:18 GMT
Author: hboutemy
Date: Sat Nov  3 02:15:17 2012
New Revision: 1405260

URL: http://svn.apache.org/viewvc?rev=1405260&view=rev
Log:
[MPLUGIN-227] rewrite HelpMojo source file when rewriting class file to keep package name
info in sync and not disturb javadoc with an empty package name

Modified:
    maven/plugin-tools/trunk/maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy
    maven/plugin-tools/trunk/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java

Modified: maven/plugin-tools/trunk/maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy
URL: http://svn.apache.org/viewvc/maven/plugin-tools/trunk/maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy?rev=1405260&r1=1405259&r2=1405260&view=diff
==============================================================================
--- maven/plugin-tools/trunk/maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy
(original)
+++ maven/plugin-tools/trunk/maven-plugin-plugin/src/it/java-basic-annotations/verify.groovy
Sat Nov  3 02:15:17 2012
@@ -204,4 +204,9 @@ assert parameter.description.text() == '
 requirement = mojo.requirements.requirement.findAll{ it.'field-name'.text() == "projectHelper"
}[0]
 assert requirement.role.text() == 'org.apache.maven.project.MavenProjectHelper'
 
+// check help mojo source and class
+assert new File( basedir, "target/classes/org/apache/maven/plugin/coreit/HelpMojo.class"
).isFile()
+assert new File( basedir, "target/generated-sources/plugin/org/apache/maven/plugin/coreit/HelpMojo.java"
).isFile()
+assert !new File( basedir, "target/generated-sources/plugin/HelpMojo.java" ).isFile()
+
 return true;

Modified: maven/plugin-tools/trunk/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
URL: http://svn.apache.org/viewvc/maven/plugin-tools/trunk/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java?rev=1405260&r1=1405259&r2=1405260&view=diff
==============================================================================
--- maven/plugin-tools/trunk/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
(original)
+++ maven/plugin-tools/trunk/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java
Sat Nov  3 02:15:17 2012
@@ -42,9 +42,13 @@ import org.objectweb.asm.commons.SimpleR
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
 import java.io.StringWriter;
 import java.util.List;
 import java.util.Properties;
@@ -55,9 +59,9 @@ import java.util.Properties;
  * which is generated by this {@link PluginDescriptorGenerator}.
  * <p>Notice that the help mojo source needs to be generated before compilation, but
when Java 5 annotations are used,
  * plugin descriptor content is available only after compilation (detecting annotations in
.class files):
- * help mojo source can be generated with empty package (and no plugin descriptor available),
then updated
- * after compilation through {@link #rewriteHelpMojo(PluginToolsRequest)} which is called
from plugin descriptor XML
- * generation.</p>
+ * help mojo source can be generated with empty package only (and no plugin descriptor available
yet), then needs
+ * to be updated after compilation - through {@link #rewriteHelpMojo(PluginToolsRequest)}
which is called from plugin
+ * descriptor XML generation.</p>
  *
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  * @version $Id$
@@ -129,7 +133,7 @@ public class PluginHelpGenerator
             }
         }
 
-        writeHelpPropertiesFile( request );
+        writeHelpPropertiesFile( request, destinationDirectory );
 
         try
         {
@@ -190,8 +194,8 @@ public class PluginHelpGenerator
         StringWriter stringWriter = new StringWriter();
 
         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(
"help-class-source.vm" );
-        InputStreamReader isReader = new InputStreamReader( is );
-        velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader );
+        InputStreamReader isReader = new InputStreamReader( is ); // FIXME platform encoding
+        velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader ); //
FIXME close reader
 
         return stringWriter.toString();
     }
@@ -219,11 +223,12 @@ public class PluginHelpGenerator
      * @throws GeneratorException
      * @see {@link #rewriteHelpMojo(PluginToolsRequest)}
      */
-    private void writeHelpPropertiesFile( PluginToolsRequest request )
+    private void writeHelpPropertiesFile( PluginToolsRequest request, File destinationDirectory
)
         throws GeneratorException
     {
         Properties properties = new Properties();
         properties.put( "helpPackageName", helpPackageName == null ? "" : helpPackageName
);
+        properties.put( "destinationDirectory", destinationDirectory.getAbsolutePath() );
 
         File tmpPropertiesFile =
             new File( request.getProject().getBuild().getDirectory(), HELP_PROPERTIES_FILENAME
);
@@ -283,7 +288,8 @@ public class PluginHelpGenerator
         // 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 );
+            File destinationDirectory = new File( properties.getProperty( "destinationDirectory"
) );
+            String helpMojoImplementation = rewriteHelpClassToMojoPackage( request, destinationDirectory
);
 
             if ( helpMojoImplementation != null )
             {
@@ -293,7 +299,7 @@ public class PluginHelpGenerator
         }
     }
 
-    private static String rewriteHelpClassToMojoPackage( PluginToolsRequest request )
+    private static String rewriteHelpClassToMojoPackage( PluginToolsRequest request, File
destinationDirectory )
         throws GeneratorException
     {
         String destinationPackage = GeneratorUtils.discoverPackageName( request.getPluginDescriptor()
);
@@ -310,6 +316,39 @@ public class PluginHelpGenerator
             return null;
         }
 
+        // rewrite help mojo source
+        File helpSourceFile = new File( destinationDirectory, HELP_MOJO_CLASS_NAME + ".java"
);
+        if ( helpSourceFile.exists() )
+        {
+            File helpSourceFileNew = new File( destinationDirectory, packageAsDirectory +
'/' + HELP_MOJO_CLASS_NAME + ".java" );
+            if ( !helpSourceFileNew.getParentFile().exists() )
+            {
+                helpSourceFileNew.getParentFile().mkdirs();
+            }
+            Reader sourceReader = null;
+            PrintWriter sourceWriter = null;
+            try
+            {
+                sourceReader = new FileReader( helpSourceFile ); // FIXME platform encoding
+                sourceWriter = new PrintWriter( new FileWriter( helpSourceFileNew ) ); //
FIXME platform encoding
+    
+                sourceWriter.println( "package " + destinationPackage + ";" );
+                IOUtil.copy( sourceReader, sourceWriter );
+            }
+            catch ( IOException e )
+            {
+                throw new GeneratorException( e.getMessage(), e );
+            }
+            finally
+            {
+                IOUtil.close( sourceReader );
+                IOUtil.close( sourceWriter );
+            }
+            helpSourceFileNew.setLastModified( helpSourceFile.lastModified() );
+            helpSourceFile.delete();
+        }
+
+        // rewrite help mojo .class
         File rewriteHelpClassFile =
             new File( outputDirectory + '/' + packageAsDirectory, HELP_MOJO_CLASS_NAME +
".class" );
         if ( !rewriteHelpClassFile.getParentFile().exists() )



Mime
View raw message