From 3e72b84b0c7ef9537b4d59422f16d4cbe0cfe16e Mon Sep 17 00:00:00 2001 From: barreiro Date: Tue, 7 Jul 2015 17:52:52 +0100 Subject: [PATCH] HHH-9584 - maven enahcer plugin - add parameter to control behavior in case of error --- .../orm/tooling/maven/MavenEnhancePlugin.java | 48 ++++++++++++++++--- .../plugin-help.xml | 8 ++++ .../main/resources/META-INF/maven/plugin.xml | 8 ++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java index f89cb0a9cf..8413bee617 100644 --- a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java +++ b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java @@ -55,6 +55,9 @@ public class MavenEnhancePlugin extends AbstractMojo { @Parameter(property = "dir", defaultValue = "${project.build.outputDirectory}") private String dir = null; + @Parameter(property = "failOnError", defaultValue = "true") + private boolean failOnError = true; + @Parameter(property = "enableLazyInitialization", defaultValue = "true") private boolean enableLazyInitialization = true; @@ -113,6 +116,9 @@ public class MavenEnhancePlugin extends AbstractMojo { for ( File file : sourceSet ) { final CtClass ctClass = toCtClass( file, classPool ); + if ( ctClass == null ) { + continue; + } if ( !ctClass.hasAnnotation( Entity.class ) && !ctClass.hasAnnotation( Embedded.class ) ) { 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() ); } 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 ); } 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 { try { @@ -159,7 +174,12 @@ public class MavenEnhancePlugin extends AbstractMojo { } catch (FileNotFoundException e) { // 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() ); } 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{ + if ( enhancedBytecode == null ) { + return; + } try { if ( file.delete() ) { if ( !file.createNewFile() ) { @@ -224,7 +252,11 @@ public class MavenEnhancePlugin extends AbstractMojo { outputStream.flush(); } 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 { try { @@ -236,7 +268,11 @@ public class MavenEnhancePlugin extends AbstractMojo { } } 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 ); } } } diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml index 8e8a9dc7c1..3b79845433 100644 --- a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml +++ b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml @@ -38,6 +38,13 @@ true Base directory where to search for .class files + + failOnError + java.lang.Boolean + false + true + Indicates whether the build will continue even if there are enhancement errors + enableLazyInitialization java.lang.Boolean @@ -62,6 +69,7 @@ ${project.build.outputDirectory} + true true true true diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml index 4cbe0c713c..172bbb70c2 100644 --- a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml +++ b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml @@ -40,6 +40,13 @@ true Base directory where to search for .class files + + failOnError + java.lang.Boolean + false + true + Indicates whether the build will continue even if there are enhancement errors + enableLazyInitialization java.lang.Boolean @@ -64,6 +71,7 @@ ${project.build.outputDirectory} + true true true true