Skip to content

Commit 3e72b84

Browse files
committed
HHH-9584 - maven enahcer plugin - add parameter to control behavior in case of error
1 parent 02026de commit 3e72b84

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class MavenEnhancePlugin extends AbstractMojo {
5555
@Parameter(property = "dir", defaultValue = "${project.build.outputDirectory}")
5656
private String dir = null;
5757

58+
@Parameter(property = "failOnError", defaultValue = "true")
59+
private boolean failOnError = true;
60+
5861
@Parameter(property = "enableLazyInitialization", defaultValue = "true")
5962
private boolean enableLazyInitialization = true;
6063

@@ -113,6 +116,9 @@ public boolean isLazyLoadable(CtField field) {
113116

114117
for ( File file : sourceSet ) {
115118
final CtClass ctClass = toCtClass( file, classPool );
119+
if ( ctClass == null ) {
120+
continue;
121+
}
116122

117123
if ( !ctClass.hasAnnotation( Entity.class ) && !ctClass.hasAnnotation( Embedded.class ) ) {
118124
getLog().debug( "Skipping class file [" + file.getAbsolutePath() + "], not an entity nor embedded" );
@@ -131,7 +137,11 @@ private ClassLoader toClassLoader(List<File> runtimeClasspath) throws MojoExecut
131137
urls.add( file.toURI().toURL() );
132138
}
133139
catch (MalformedURLException e) {
134-
throw new MojoExecutionException( "Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e );
140+
String msg = "Unable to resolve classpath entry to URL: " + file.getAbsolutePath();
141+
if ( failOnError ) {
142+
throw new MojoExecutionException( msg, e );
143+
}
144+
getLog().warn( msg );
135145
}
136146
}
137147

@@ -146,7 +156,12 @@ private CtClass toCtClass(File file, ClassPool classPool) throws MojoExecutionEx
146156
return classPool.makeClass( is );
147157
}
148158
catch (IOException e) {
149-
throw new MojoExecutionException( "Javassist unable to load class in preparation for enhancing : " + file.getAbsolutePath(), e );
159+
String msg = "Javassist unable to load class in preparation for enhancing: " + file.getAbsolutePath();
160+
if ( failOnError ) {
161+
throw new MojoExecutionException( msg, e );
162+
}
163+
getLog().warn( msg );
164+
return null;
150165
}
151166
finally {
152167
try {
@@ -159,7 +174,12 @@ private CtClass toCtClass(File file, ClassPool classPool) throws MojoExecutionEx
159174
}
160175
catch (FileNotFoundException e) {
161176
// should never happen, but...
162-
throw new MojoExecutionException( "Unable to locate class file for InputStream: " + file.getAbsolutePath(), e );
177+
String msg = "Unable to locate class file for InputStream: " + file.getAbsolutePath();
178+
if ( failOnError ) {
179+
throw new MojoExecutionException( msg, e );
180+
}
181+
getLog().warn( msg );
182+
return null;
163183
}
164184
}
165185

@@ -168,7 +188,12 @@ private byte[] doEnhancement(CtClass ctClass, Enhancer enhancer) throws MojoExec
168188
return enhancer.enhance( ctClass.getName(), ctClass.toBytecode() );
169189
}
170190
catch (Exception e) {
171-
throw new MojoExecutionException( "Unable to enhance class : " + ctClass.getName(), e );
191+
String msg = "Unable to enhance class: " + ctClass.getName();
192+
if ( failOnError ) {
193+
throw new MojoExecutionException( msg, e );
194+
}
195+
getLog().warn( msg );
196+
return null;
172197
}
173198
}
174199

@@ -203,6 +228,9 @@ private void walkDir(File dir, FileFilter classesFilter, FileFilter dirFilter) {
203228
}
204229

205230
private void writeOutEnhancedClass(byte[] enhancedBytecode, CtClass ctClass, File file) throws MojoExecutionException{
231+
if ( enhancedBytecode == null ) {
232+
return;
233+
}
206234
try {
207235
if ( file.delete() ) {
208236
if ( !file.createNewFile() ) {
@@ -224,7 +252,11 @@ private void writeOutEnhancedClass(byte[] enhancedBytecode, CtClass ctClass, Fil
224252
outputStream.flush();
225253
}
226254
catch (IOException e) {
227-
throw new MojoExecutionException( "Error writing to enhanced class [" + ctClass.getName() + "] to file [" + file.getAbsolutePath() + "]", e );
255+
String msg = String.format( "Error writing to enhanced class [%s] to file [%s]", ctClass.getName(), file.getAbsolutePath() );
256+
if ( failOnError ) {
257+
throw new MojoExecutionException( msg, e );
258+
}
259+
getLog().warn( msg );
228260
}
229261
finally {
230262
try {
@@ -236,7 +268,11 @@ private void writeOutEnhancedClass(byte[] enhancedBytecode, CtClass ctClass, Fil
236268
}
237269
}
238270
catch (FileNotFoundException e) {
239-
throw new MojoExecutionException( "Error opening class file for writing : " + file.getAbsolutePath(), e );
271+
String msg = "Error opening class file for writing: " + file.getAbsolutePath();
272+
if ( failOnError ) {
273+
throw new MojoExecutionException( msg, e );
274+
}
275+
getLog().warn( msg );
240276
}
241277
}
242278
}

tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
<editable>true</editable>
3939
<description>Base directory where to search for .class files</description>
4040
</parameter>
41+
<parameter>
42+
<name>failOnError</name>
43+
<type>java.lang.Boolean</type>
44+
<required>false</required>
45+
<editable>true</editable>
46+
<description>Indicates whether the build will continue even if there are enhancement errors</description>
47+
</parameter>
4148
<parameter>
4249
<name>enableLazyInitialization</name>
4350
<type>java.lang.Boolean</type>
@@ -62,6 +69,7 @@
6269
</parameters>
6370
<configuration>
6471
<dir>${project.build.outputDirectory}</dir>
72+
<failOnError>true</failOnError>
6573
<enableLazyInitialization>true</enableLazyInitialization>
6674
<enableDirtyTracking>true</enableDirtyTracking>
6775
<enableAssociationManagement>true</enableAssociationManagement>

tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<editable>true</editable>
4141
<description>Base directory where to search for .class files</description>
4242
</parameter>
43+
<parameter>
44+
<name>failOnError</name>
45+
<type>java.lang.Boolean</type>
46+
<required>false</required>
47+
<editable>true</editable>
48+
<description>Indicates whether the build will continue even if there are enhancement errors</description>
49+
</parameter>
4350
<parameter>
4451
<name>enableLazyInitialization</name>
4552
<type>java.lang.Boolean</type>
@@ -64,6 +71,7 @@
6471
</parameters>
6572
<configuration>
6673
<dir>${project.build.outputDirectory}</dir>
74+
<failOnError>true</failOnError>
6775
<enableLazyInitialization>true</enableLazyInitialization>
6876
<enableDirtyTracking>true</enableDirtyTracking>
6977
<enableAssociationManagement>true</enableAssociationManagement>

0 commit comments

Comments
 (0)