From 2f6eee2513d43ccbf334d69c3b48bc341f96dddb Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 11 May 2012 16:46:34 +0000 Subject: [PATCH] [MPLUGIN-189] if helpPackageName is not configured change the package of the generic class name to have a similar package as before annotations git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/branches/MPLUGIN-189@1337279 13f79535-47bb-0310-9956-ffa450edef68 --- .../verify.groovy | 6 + .../annotation-with-inheritance/verify.groovy | 6 + maven-plugin-tools-annotations/pom.xml | 2 - maven-plugin-tools-api/pom.xml | 9 ++ .../generator/PluginDescriptorGenerator.java | 121 ++++++++++++++++-- .../plugin/generator/PluginHelpGenerator.java | 6 +- .../src/main/resources/help-class-source.vm | 3 +- pom.xml | 11 ++ 8 files changed, 149 insertions(+), 15 deletions(-) diff --git a/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy b/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy index 9e047fa..213081a 100644 --- a/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy +++ b/maven-plugin-plugin/src/it/annotation-with-inheritance-from-deps/verify.groovy @@ -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] diff --git a/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy b/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy index 56fb57b..2052a1f 100644 --- a/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy +++ b/maven-plugin-plugin/src/it/annotation-with-inheritance/verify.groovy @@ -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] diff --git a/maven-plugin-tools-annotations/pom.xml b/maven-plugin-tools-annotations/pom.xml index c2fdcf8..bef9e8a 100644 --- a/maven-plugin-tools-annotations/pom.xml +++ b/maven-plugin-tools-annotations/pom.xml @@ -64,12 +64,10 @@ asm asm - 3.3.1 asm asm-commons - 3.3.1 diff --git a/maven-plugin-tools-api/pom.xml b/maven-plugin-tools-api/pom.xml index 28803f5..6de6aa0 100644 --- a/maven-plugin-tools-api/pom.xml +++ b/maven-plugin-tools-api/pom.xml @@ -88,6 +88,15 @@ velocity + + asm + asm + + + asm + asm-commons + + net.sf.jtidy diff --git a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java index 136db43..76e48f8 100644 --- a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java +++ b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java @@ -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 ); + } + } } diff --git a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java index 13d37a0..6f54f51 100644 --- a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java +++ b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java @@ -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( '.' ) ); diff --git a/maven-plugin-tools-api/src/main/resources/help-class-source.vm b/maven-plugin-tools-api/src/main/resources/help-class-source.vm index 0df0df7..6db6464 100644 --- a/maven-plugin-tools-api/src/main/resources/help-class-source.vm +++ b/maven-plugin-tools-api/src/main/resources/help-class-source.vm @@ -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 { diff --git a/pom.xml b/pom.xml index 1feec30..f562ff9 100644 --- a/pom.xml +++ b/pom.xml @@ -258,6 +258,17 @@ 1.11 + + asm + asm + 3.3.1 + + + asm + asm-commons + 3.3.1 + + org.apache.maven.plugin-testing maven-plugin-testing-harness