diff --git a/jOOQ-codegen-maven/src/main/java/org/jooq/codegen/maven/Plugin.java b/jOOQ-codegen-maven/src/main/java/org/jooq/codegen/maven/Plugin.java index d1807a2ab5..5beb9d27b3 100644 --- a/jOOQ-codegen-maven/src/main/java/org/jooq/codegen/maven/Plugin.java +++ b/jOOQ-codegen-maven/src/main/java/org/jooq/codegen/maven/Plugin.java @@ -169,9 +169,10 @@ public class Plugin extends AbstractMojo { ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); + // [#2886] Add the surrounding project's dependencies to the current classloader + URLClassLoader pluginClassLoader = getClassLoader(); try { - // [#2886] Add the surrounding project's dependencies to the current classloader - Thread.currentThread().setContextClassLoader(getClassLoader()); + Thread.currentThread().setContextClassLoader(pluginClassLoader); // [#5881] Target is allowed to be null if (generator.getTarget() == null) @@ -201,12 +202,18 @@ public class Plugin extends AbstractMojo { // [#2886] Restore old class loader finally { Thread.currentThread().setContextClassLoader(oldCL); + try { + pluginClassLoader.close(); + } + catch (Throwable e) { // catch all possible errors to avoid suppressing the original exception + getLog().error("Couldn't close the classloader.", e); + } } project.addCompileSourceRoot(generator.getTarget().getDirectory()); } - private ClassLoader getClassLoader() throws MojoExecutionException { + private URLClassLoader getClassLoader() throws MojoExecutionException { try { List classpathElements = project.getRuntimeClasspathElements(); URL urls[] = new URL[classpathElements.size()];