[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
git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1405260 13f79535-47bb-0310-9956-ffa450edef68master
parent
a757bee927
commit
92df3b3987
|
|
@ -204,4 +204,9 @@ assert parameter.description.text() == 'Parameter description.'
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -42,9 +42,13 @@ import org.objectweb.asm.commons.SimpleRemapper;
|
|||
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() )
|
||||
|
|
|
|||
Loading…
Reference in New Issue