moved help mojo rewriting code to PluginHelpGenerator
git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1354239 13f79535-47bb-0310-9956-ffa450edef68master
parent
297b152d05
commit
fdcb229aa4
|
|
@ -29,18 +29,11 @@ import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
|
||||||
import org.apache.maven.tools.plugin.PluginToolsRequest;
|
import org.apache.maven.tools.plugin.PluginToolsRequest;
|
||||||
import org.apache.maven.tools.plugin.util.PluginUtils;
|
import org.apache.maven.tools.plugin.util.PluginUtils;
|
||||||
import org.codehaus.plexus.util.IOUtil;
|
import org.codehaus.plexus.util.IOUtil;
|
||||||
import org.codehaus.plexus.util.PropertyUtils;
|
|
||||||
import org.codehaus.plexus.util.StringUtils;
|
import org.codehaus.plexus.util.StringUtils;
|
||||||
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
|
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
|
||||||
import org.codehaus.plexus.util.xml.XMLWriter;
|
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.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
|
@ -51,7 +44,6 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -73,28 +65,8 @@ public class PluginDescriptorGenerator
|
||||||
public void execute( File destinationDirectory, PluginToolsRequest request )
|
public void execute( File destinationDirectory, PluginToolsRequest request )
|
||||||
throws GeneratorException
|
throws GeneratorException
|
||||||
{
|
{
|
||||||
|
// eventually rewrite help mojo class to match actual package name
|
||||||
File tmpPropertiesFile =
|
PluginHelpGenerator.rewriteHelpMojo( request );
|
||||||
new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" );
|
|
||||||
|
|
||||||
if ( tmpPropertiesFile.exists() )
|
|
||||||
{
|
|
||||||
Properties properties = PropertyUtils.loadProperties( tmpPropertiesFile );
|
|
||||||
|
|
||||||
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
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -106,6 +78,7 @@ public class PluginDescriptorGenerator
|
||||||
MavenProject mavenProject = request.getProject();
|
MavenProject mavenProject = request.getProject();
|
||||||
String pluginHelpFilePath =
|
String pluginHelpFilePath =
|
||||||
"META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
|
"META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
|
||||||
|
|
||||||
f = new File( request.getProject().getBuild().getOutputDirectory(), pluginHelpFilePath );
|
f = new File( request.getProject().getBuild().getOutputDirectory(), pluginHelpFilePath );
|
||||||
writeDescriptor( f, request, true );
|
writeDescriptor( f, request, true );
|
||||||
}
|
}
|
||||||
|
|
@ -643,87 +616,4 @@ public class PluginDescriptorGenerator
|
||||||
}
|
}
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String rewriteHelpClassToMojoPackage( PluginToolsRequest request )
|
|
||||||
throws GeneratorException
|
|
||||||
{
|
|
||||||
String destinationPackage = GeneratorUtils.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();
|
|
||||||
}
|
|
||||||
|
|
||||||
FileInputStream fileInputStream = null;
|
|
||||||
|
|
||||||
ClassReader cr = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
fileInputStream = new FileInputStream( helpClassFile );
|
|
||||||
cr = new ClassReader( fileInputStream );
|
|
||||||
}
|
|
||||||
catch ( IOException e )
|
|
||||||
{
|
|
||||||
throw new GeneratorException( e.getMessage(), e );
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
IOUtil.close( fileInputStream );
|
|
||||||
}
|
|
||||||
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,18 @@ import org.codehaus.plexus.logging.Logger;
|
||||||
import org.codehaus.plexus.logging.console.ConsoleLogger;
|
import org.codehaus.plexus.logging.console.ConsoleLogger;
|
||||||
import org.codehaus.plexus.util.FileUtils;
|
import org.codehaus.plexus.util.FileUtils;
|
||||||
import org.codehaus.plexus.util.IOUtil;
|
import org.codehaus.plexus.util.IOUtil;
|
||||||
|
import org.codehaus.plexus.util.PropertyUtils;
|
||||||
import org.codehaus.plexus.util.StringUtils;
|
import org.codehaus.plexus.util.StringUtils;
|
||||||
import org.codehaus.plexus.velocity.VelocityComponent;
|
import org.codehaus.plexus.velocity.VelocityComponent;
|
||||||
|
import org.objectweb.asm.ClassReader;
|
||||||
|
import org.objectweb.asm.ClassVisitor;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.commons.Remapper;
|
||||||
|
import org.objectweb.asm.commons.RemappingClassAdapter;
|
||||||
|
import org.objectweb.asm.commons.SimpleRemapper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
@ -57,6 +65,11 @@ public class PluginHelpGenerator
|
||||||
*/
|
*/
|
||||||
private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
|
private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Help properties file, to store data about generated source.
|
||||||
|
*/
|
||||||
|
private static final String HELP_PROPERTIES_FILENAME = "maven-plugin-help.properties";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default goal
|
* Default goal
|
||||||
*/
|
*/
|
||||||
|
|
@ -197,6 +210,13 @@ public class PluginHelpGenerator
|
||||||
return StringUtils.isEmpty( packageName ) ? HELP_MOJO_CLASS_NAME : packageName + '.' + HELP_MOJO_CLASS_NAME;
|
return StringUtils.isEmpty( packageName ) ? HELP_MOJO_CLASS_NAME : packageName + '.' + HELP_MOJO_CLASS_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write help properties files for later use to eventually rewrite Help Mojo.
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @throws GeneratorException
|
||||||
|
* @see {@link #rewriteHelpMojo(PluginToolsRequest)}
|
||||||
|
*/
|
||||||
private void writeHelpPropertiesFile( PluginToolsRequest request )
|
private void writeHelpPropertiesFile( PluginToolsRequest request )
|
||||||
throws GeneratorException
|
throws GeneratorException
|
||||||
{
|
{
|
||||||
|
|
@ -204,7 +224,7 @@ public class PluginHelpGenerator
|
||||||
properties.put( "helpPackageName", helpPackageName == null ? "" : helpPackageName );
|
properties.put( "helpPackageName", helpPackageName == null ? "" : helpPackageName );
|
||||||
|
|
||||||
File tmpPropertiesFile =
|
File tmpPropertiesFile =
|
||||||
new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" );
|
new File( request.getProject().getBuild().getDirectory(), HELP_PROPERTIES_FILENAME );
|
||||||
|
|
||||||
if ( tmpPropertiesFile.exists() )
|
if ( tmpPropertiesFile.exists() )
|
||||||
{
|
{
|
||||||
|
|
@ -230,4 +250,125 @@ public class PluginHelpGenerator
|
||||||
IOUtil.close( fos );
|
IOUtil.close( fos );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrite Help Mojo to match actual Mojos package name if it was not available at source generation
|
||||||
|
* time. This is used at descriptor generation time.
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @throws GeneratorException
|
||||||
|
*/
|
||||||
|
static void rewriteHelpMojo( PluginToolsRequest request )
|
||||||
|
throws GeneratorException
|
||||||
|
{
|
||||||
|
File tmpPropertiesFile =
|
||||||
|
new File( request.getProject().getBuild().getDirectory(), HELP_PROPERTIES_FILENAME );
|
||||||
|
|
||||||
|
if ( !tmpPropertiesFile.exists() )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Properties properties = PropertyUtils.loadProperties( tmpPropertiesFile );
|
||||||
|
|
||||||
|
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
|
||||||
|
updateHelpMojoDescriptor( request.getPluginDescriptor(), helpMojoImplementation );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String rewriteHelpClassToMojoPackage( PluginToolsRequest request )
|
||||||
|
throws GeneratorException
|
||||||
|
{
|
||||||
|
String destinationPackage = GeneratorUtils.discoverPackageName( request.getPluginDescriptor() );
|
||||||
|
if ( StringUtils.isEmpty( destinationPackage ) )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String packageAsDirectory = StringUtils.replace( destinationPackage, '.', '/' );
|
||||||
|
|
||||||
|
String outputDirectory = request.getProject().getBuild().getOutputDirectory();
|
||||||
|
File helpClassFile = new File( outputDirectory, HELP_MOJO_CLASS_NAME + ".class" );
|
||||||
|
if ( !helpClassFile.exists() )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File rewriteHelpClassFile =
|
||||||
|
new File( outputDirectory + '/' + packageAsDirectory, HELP_MOJO_CLASS_NAME + ".class" );
|
||||||
|
if ( !rewriteHelpClassFile.getParentFile().exists() )
|
||||||
|
{
|
||||||
|
rewriteHelpClassFile.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileInputStream fileInputStream = null;
|
||||||
|
ClassReader cr = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fileInputStream = new FileInputStream( helpClassFile );
|
||||||
|
cr = new ClassReader( fileInputStream );
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
throw new GeneratorException( e.getMessage(), e );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IOUtil.close( fileInputStream );
|
||||||
|
}
|
||||||
|
|
||||||
|
ClassWriter cw = new ClassWriter( 0 );
|
||||||
|
|
||||||
|
Remapper packageRemapper =
|
||||||
|
new SimpleRemapper( HELP_MOJO_CLASS_NAME, packageAsDirectory + '/' + HELP_MOJO_CLASS_NAME );
|
||||||
|
ClassVisitor cv = new RemappingClassAdapter( cw, packageRemapper );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cr.accept( cv, ClassReader.EXPAND_FRAMES );
|
||||||
|
}
|
||||||
|
catch ( Throwable e )
|
||||||
|
{
|
||||||
|
throw new GeneratorException( "ASM issue processing class-file " + 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 static void updateHelpMojoDescriptor( PluginDescriptor pluginDescriptor, String helpMojoImplementation )
|
||||||
|
{
|
||||||
|
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( "help" );
|
||||||
|
|
||||||
|
if ( mojoDescriptor != null )
|
||||||
|
{
|
||||||
|
mojoDescriptor.setImplementation( helpMojoImplementation );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue