From 82117d0e99aa89516e1e07679325c41e877ed628 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 24 Jun 2014 15:02:47 +0200 Subject: [PATCH] [#2886] jooq-codegen-maven should look into project dependencies for jdbc driver --- .../main/java/org/jooq/util/maven/Plugin.java | 38 +++++++++++++++++-- .../java/org/jooq/util/GenerationTool.java | 5 ++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/jOOQ-codegen-maven/src/main/java/org/jooq/util/maven/Plugin.java b/jOOQ-codegen-maven/src/main/java/org/jooq/util/maven/Plugin.java index ad037385d5..44401c3784 100644 --- a/jOOQ-codegen-maven/src/main/java/org/jooq/util/maven/Plugin.java +++ b/jOOQ-codegen-maven/src/main/java/org/jooq/util/maven/Plugin.java @@ -41,9 +41,13 @@ package org.jooq.util.maven; import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES; +import static org.apache.maven.plugins.annotations.ResolutionScope.TEST; import java.io.File; import java.io.StringWriter; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; import javax.xml.bind.JAXB; @@ -64,7 +68,8 @@ import org.apache.maven.project.MavenProject; */ @Mojo( name = "generate", - defaultPhase = GENERATE_SOURCES + defaultPhase = GENERATE_SOURCES, + requiresDependencyResolution = TEST ) public class Plugin extends AbstractMojo { @@ -92,9 +97,13 @@ public class Plugin extends AbstractMojo { @Override public void execute() throws MojoExecutionException { - try { + ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); - // [#2887] Patch relative paths to take plugin execution basedir into accountcd joo + try { + // [#2886] Add the surrounding project's dependencies to the current classloader + Thread.currentThread().setContextClassLoader(getClassLoader()); + + // [#2887] Patch relative paths to take plugin execution basedir into account String dir = generator.getTarget().getDirectory(); if (!new File(dir).isAbsolute()) { generator.getTarget().setDirectory(project.getBasedir() + File.separator + dir); @@ -113,6 +122,29 @@ public class Plugin extends AbstractMojo { catch (Exception ex) { throw new MojoExecutionException("Error running jOOQ code generation tool", ex); } + + // [#2886] Restore old class loader + finally { + Thread.currentThread().setContextClassLoader(oldCL); + } + project.addCompileSourceRoot(generator.getTarget().getDirectory()); } + + @SuppressWarnings("unchecked") + private ClassLoader getClassLoader() throws MojoExecutionException { + try { + List classpathElements = project.getRuntimeClasspathElements(); + URL urls[] = new URL[classpathElements.size()]; + + for (int i = 0; i < urls.length; i++) { + urls[i] = new File(classpathElements.get(i)).toURI().toURL(); + } + + return new URLClassLoader(urls, getClass().getClassLoader()); + } + catch (Exception e) { + throw new MojoExecutionException("Couldn't create a classloader.", e); + } + } } diff --git a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java index 01943104ca..56168684cf 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java @@ -51,6 +51,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.util.List; import java.util.Properties; @@ -191,7 +192,7 @@ public class GenerationTool { // --------------------- if (connection == null) { errorIfNull(j, "The tag is mandatory."); - loadClass(driverClass(j)); + Class driver = (Class) loadClass(driverClass(j)); Properties properties = new Properties(); for (Property p : j.getProperties()) { @@ -203,7 +204,7 @@ public class GenerationTool { if (!properties.containsKey("password")) properties.put("password", defaultString(j.getPassword())); - connection = DriverManager.getConnection(defaultString(j.getUrl()), properties); + connection = driver.newInstance().connect(defaultString(j.getUrl()), properties); close = true; } else {