[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-ffa450edef68master
parent
643c8de61c
commit
2f6eee2513
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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( '.' ) );
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
11
pom.xml
11
pom.xml
|
|
@ -258,6 +258,17 @@
|
|||
<version>1.11</version>
|
||||
</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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue