close the plugin classloader after finishing the job

This commit is contained in:
Sergey Tselovalnikov 2018-07-02 15:44:42 +10:00
parent 419eedff5e
commit d1c6163cb6
No known key found for this signature in database
GPG Key ID: 5A557D9D6A68C9B1

View File

@ -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<String> classpathElements = project.getRuntimeClasspathElements();
URL urls[] = new URL[classpathElements.size()];