[MPLUGIN-227]

o avoid NPE but get (intended-)explicit warning when unexpected situation happens (probably caused by using different plugin version between source generation and XML descriptor generation phases)
o try guessing the directory in such situation, using default location
o but don't fail in any case: 

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1406480 13f79535-47bb-0310-9956-ffa450edef68
master
Herve Boutemy 2012-11-07 07:20:12 +00:00
parent 5d366fc36f
commit 805e0f84a7
2 changed files with 53 additions and 29 deletions

View File

@ -28,6 +28,7 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
import org.apache.maven.tools.plugin.PluginToolsRequest;
import org.apache.maven.tools.plugin.util.PluginUtils;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
@ -56,6 +57,7 @@ import java.util.Set;
* get validation directives to help users in IDEs.
*/
public class PluginDescriptorGenerator
extends AbstractLogEnabled
implements Generator
{
@ -66,7 +68,7 @@ public class PluginDescriptorGenerator
throws GeneratorException
{
// eventually rewrite help mojo class to match actual package name
PluginHelpGenerator.rewriteHelpMojo( request );
PluginHelpGenerator.rewriteHelpMojo( request, getLogger() );
try
{

View File

@ -285,7 +285,7 @@ public class PluginHelpGenerator
* @param request
* @throws GeneratorException
*/
static void rewriteHelpMojo( PluginToolsRequest request )
static void rewriteHelpMojo( PluginToolsRequest request, Logger log )
throws GeneratorException
{
File tmpPropertiesFile =
@ -303,8 +303,22 @@ 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 ) )
{
File destinationDirectory = new File( properties.getProperty( "destinationDirectory" ) );
String helpMojoImplementation = rewriteHelpClassToMojoPackage( request, destinationDirectory );
String destDir = properties.getProperty( "destinationDirectory" );
File destinationDirectory;
if ( StringUtils.isEmpty( destDir ) )
{
// writeHelpPropertiesFile() creates 2 properties: find one without the other should not be possible
log.warn( "\n\nUnexpected situation: destinationDirectory not defined in " + HELP_PROPERTIES_FILENAME
+ " during help mojo source generation but expected during XML descriptor generation." );
log.warn( "Please check helpmojo goal version used in previous build phase." );
destinationDirectory = new File( "target/generated-sources/plugin" );
log.warn( "Trying default location: " + destinationDirectory );
}
else
{
destinationDirectory = new File( destDir );
}
String helpMojoImplementation = rewriteHelpClassToMojoPackage( request, destinationDirectory, log );
if ( helpMojoImplementation != null )
{
@ -314,7 +328,7 @@ public class PluginHelpGenerator
}
}
private static String rewriteHelpClassToMojoPackage( PluginToolsRequest request, File destinationDirectory )
private static String rewriteHelpClassToMojoPackage( PluginToolsRequest request, File destinationDirectory, Logger log )
throws GeneratorException
{
String destinationPackage = GeneratorUtils.discoverPackageName( request.getPluginDescriptor() );
@ -333,34 +347,42 @@ public class PluginHelpGenerator
// rewrite help mojo source
File helpSourceFile = new File( destinationDirectory, HELP_MOJO_CLASS_NAME + ".java" );
File helpSourceFileNew = new File( destinationDirectory, packageAsDirectory + '/' + HELP_MOJO_CLASS_NAME + ".java" );
if ( !helpSourceFileNew.getParentFile().exists() )
if ( !helpSourceFile.exists() )
{
helpSourceFileNew.getParentFile().mkdirs();
log.warn( "HelpMojo.java not found in default location: " + helpSourceFile.getAbsolutePath() );
log.warn( "Help goal source won't be moved to package: " + destinationPackage );
}
Reader sourceReader = null;
PrintWriter sourceWriter = null;
try
else
{
sourceReader = new InputStreamReader( new FileInputStream( helpSourceFile ), request.getEncoding() );
sourceWriter =
new PrintWriter( new OutputStreamWriter( new FileOutputStream( helpSourceFileNew ),
request.getEncoding() ) );
sourceWriter.println( "package " + destinationPackage + ";" );
IOUtil.copy( sourceReader, sourceWriter );
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 InputStreamReader( new FileInputStream( helpSourceFile ), request.getEncoding() );
sourceWriter =
new PrintWriter( new OutputStreamWriter( new FileOutputStream( helpSourceFileNew ),
request.getEncoding() ) );
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();
}
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 =