HHH-9584 - maven enahcer plugin - add parameter to control behavior in case of error

This commit is contained in:
barreiro 2015-07-07 17:52:52 +01:00
parent 02026dec78
commit 3e72b84b0c
3 changed files with 58 additions and 6 deletions

View File

@ -55,6 +55,9 @@ public class MavenEnhancePlugin extends AbstractMojo {
@Parameter(property = "dir", defaultValue = "${project.build.outputDirectory}") @Parameter(property = "dir", defaultValue = "${project.build.outputDirectory}")
private String dir = null; private String dir = null;
@Parameter(property = "failOnError", defaultValue = "true")
private boolean failOnError = true;
@Parameter(property = "enableLazyInitialization", defaultValue = "true") @Parameter(property = "enableLazyInitialization", defaultValue = "true")
private boolean enableLazyInitialization = true; private boolean enableLazyInitialization = true;
@ -113,6 +116,9 @@ public class MavenEnhancePlugin extends AbstractMojo {
for ( File file : sourceSet ) { for ( File file : sourceSet ) {
final CtClass ctClass = toCtClass( file, classPool ); final CtClass ctClass = toCtClass( file, classPool );
if ( ctClass == null ) {
continue;
}
if ( !ctClass.hasAnnotation( Entity.class ) && !ctClass.hasAnnotation( Embedded.class ) ) { if ( !ctClass.hasAnnotation( Entity.class ) && !ctClass.hasAnnotation( Embedded.class ) ) {
getLog().debug( "Skipping class file [" + file.getAbsolutePath() + "], not an entity nor embedded" ); getLog().debug( "Skipping class file [" + file.getAbsolutePath() + "], not an entity nor embedded" );
@ -131,7 +137,11 @@ public class MavenEnhancePlugin extends AbstractMojo {
urls.add( file.toURI().toURL() ); urls.add( file.toURI().toURL() );
} }
catch (MalformedURLException e) { catch (MalformedURLException e) {
throw new MojoExecutionException( "Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e ); String msg = "Unable to resolve classpath entry to URL: " + file.getAbsolutePath();
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
} }
} }
@ -146,7 +156,12 @@ public class MavenEnhancePlugin extends AbstractMojo {
return classPool.makeClass( is ); return classPool.makeClass( is );
} }
catch (IOException e) { catch (IOException e) {
throw new MojoExecutionException( "Javassist unable to load class in preparation for enhancing : " + file.getAbsolutePath(), e ); String msg = "Javassist unable to load class in preparation for enhancing: " + file.getAbsolutePath();
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
return null;
} }
finally { finally {
try { try {
@ -159,7 +174,12 @@ public class MavenEnhancePlugin extends AbstractMojo {
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
// should never happen, but... // should never happen, but...
throw new MojoExecutionException( "Unable to locate class file for InputStream: " + file.getAbsolutePath(), e ); String msg = "Unable to locate class file for InputStream: " + file.getAbsolutePath();
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
return null;
} }
} }
@ -168,7 +188,12 @@ public class MavenEnhancePlugin extends AbstractMojo {
return enhancer.enhance( ctClass.getName(), ctClass.toBytecode() ); return enhancer.enhance( ctClass.getName(), ctClass.toBytecode() );
} }
catch (Exception e) { catch (Exception e) {
throw new MojoExecutionException( "Unable to enhance class : " + ctClass.getName(), e ); String msg = "Unable to enhance class: " + ctClass.getName();
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
return null;
} }
} }
@ -203,6 +228,9 @@ public class MavenEnhancePlugin extends AbstractMojo {
} }
private void writeOutEnhancedClass(byte[] enhancedBytecode, CtClass ctClass, File file) throws MojoExecutionException{ private void writeOutEnhancedClass(byte[] enhancedBytecode, CtClass ctClass, File file) throws MojoExecutionException{
if ( enhancedBytecode == null ) {
return;
}
try { try {
if ( file.delete() ) { if ( file.delete() ) {
if ( !file.createNewFile() ) { if ( !file.createNewFile() ) {
@ -224,7 +252,11 @@ public class MavenEnhancePlugin extends AbstractMojo {
outputStream.flush(); outputStream.flush();
} }
catch (IOException e) { catch (IOException e) {
throw new MojoExecutionException( "Error writing to enhanced class [" + ctClass.getName() + "] to file [" + file.getAbsolutePath() + "]", e ); String msg = String.format( "Error writing to enhanced class [%s] to file [%s]", ctClass.getName(), file.getAbsolutePath() );
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
} }
finally { finally {
try { try {
@ -236,7 +268,11 @@ public class MavenEnhancePlugin extends AbstractMojo {
} }
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
throw new MojoExecutionException( "Error opening class file for writing : " + file.getAbsolutePath(), e ); String msg = "Error opening class file for writing: " + file.getAbsolutePath();
if ( failOnError ) {
throw new MojoExecutionException( msg, e );
}
getLog().warn( msg );
} }
} }
} }

View File

@ -38,6 +38,13 @@
<editable>true</editable> <editable>true</editable>
<description>Base directory where to search for .class files</description> <description>Base directory where to search for .class files</description>
</parameter> </parameter>
<parameter>
<name>failOnError</name>
<type>java.lang.Boolean</type>
<required>false</required>
<editable>true</editable>
<description>Indicates whether the build will continue even if there are enhancement errors</description>
</parameter>
<parameter> <parameter>
<name>enableLazyInitialization</name> <name>enableLazyInitialization</name>
<type>java.lang.Boolean</type> <type>java.lang.Boolean</type>
@ -62,6 +69,7 @@
</parameters> </parameters>
<configuration> <configuration>
<dir>${project.build.outputDirectory}</dir> <dir>${project.build.outputDirectory}</dir>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization> <enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>true</enableDirtyTracking> <enableDirtyTracking>true</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement> <enableAssociationManagement>true</enableAssociationManagement>

View File

@ -40,6 +40,13 @@
<editable>true</editable> <editable>true</editable>
<description>Base directory where to search for .class files</description> <description>Base directory where to search for .class files</description>
</parameter> </parameter>
<parameter>
<name>failOnError</name>
<type>java.lang.Boolean</type>
<required>false</required>
<editable>true</editable>
<description>Indicates whether the build will continue even if there are enhancement errors</description>
</parameter>
<parameter> <parameter>
<name>enableLazyInitialization</name> <name>enableLazyInitialization</name>
<type>java.lang.Boolean</type> <type>java.lang.Boolean</type>
@ -64,6 +71,7 @@
</parameters> </parameters>
<configuration> <configuration>
<dir>${project.build.outputDirectory}</dir> <dir>${project.build.outputDirectory}</dir>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization> <enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>true</enableDirtyTracking> <enableDirtyTracking>true</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement> <enableAssociationManagement>true</enableAssociationManagement>