I have extended the JavaGenerator of jOOQs codegen classes and would like the gradle plugin use this Generator, used in IntelliJ. Of course, this class is in a package outside the jooq dependency.
In spite of the fact that the compiled class is on the classpath I get a class not found exception.
In the forum I found the recommendation to use the syntax name= MyCustomClass.class.canonicalName, which doesnt work.
Does somebody have experience with that issue?
Thank you very much in advance, best greetings, Elke
Please note that the all of Gradle project classloaders are instantiated before a single class is compiled. So if you want to execute one of your compiled classes during the build, you’ll need to use another classloader. Eg:
configurations {
jooq
}
dependencies {
jooq project(':my-jooq-javagenerator')
}
task jooq {
dependsOn ':my-jooq-javagenerator:classes'
doLast {
List<URL> urls = configurations.jooq.collect { it.toURI().toURL() }
def cl = new UrlClassLoader(urls as URL[])
Class myClass = cl.loadClass('foo.bar.MyJavaGenerator')
def myGenerator = myClass.newInstance(...)
myGenerator.doStuff(...)
}
}
Thank you very much, dear Lance.
Two questions:
In my build.gradle UrlClassLoader can not be resolved (gradle 4.10.3 and Java 11.0.3 in IntelliJ) at the same time the .newInstance() Method is marked as deprecated.How to fix this?
I tried newer Versions of gradle but they do not work with Java11 and Intellij (invalid Type Code 85, according to this https://intellij-support.jetbrains.com/hc/en-us/community/posts/360002683740-Gradle-failure-Java-11-with-Gradle-5-and-Cause-invalid-type-code-CD).
- Once the preload of the class is succeeded, how can I integrate it in my code?
Thank you very much in advance.
Best Elke
jooq {
version = '3.11.11'
edition = 'OSS'
sample(sourceSets.main) {
jdbc {
driver='org.postgresql.Driver'
url='jdbc:postgresql://myServer/myDataBase'
user='user'
password='secret'
}
generator {
//original
//name = 'org.jooq.codegen.JavaGenerator'
// custom
name = 'foo.bar.MyJavaGenerator'
strategy {
// name = 'nu.studer.sample.SampleGeneratorStrategy' // use the custom generator strategy
}
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
}
target {
packageName='dbgenerated'
directory='src/main/java'
}
}
}
}
``
Gesendet von [Mailspring](https://getmailspring.com/), der besten kostenlosen E-Mail-App für die Arbeit
Dear Lance
I solved the first question:
it was a Typo (URL instead of Url, and so I found also the new Code for the deprecated Method). Code below.
But Intellij tells my that this task cannot be applied to nu.studer.jooq.extension.
What can I do?
Thank you in advance, best greetings, Elke
configurations {
jooq
}
dependencies {
jooq project(':my-jooq-javagenerator')
}
task jooq {
dependsOn ':my-jooq-javagenerator:classes'
doLast {
List<URL> urls = configurations.jooq.collect { it.toURI().toURL() }
def cl = new URLClassLoader(urls as URL[])
Class myClass = cl.loadClass('foo.bar.MyJavaGenerator')
def myGenerator = myClass.getDeclaredConstructor().newInstance()
}
}
``
Gesendet von [Mailspring](https://getmailspring.com/), der besten kostenlosen E-Mail-App für die Arbeit
Ah, sorry… I’ve led you on a bit of a wild goose chase there. If you want the jooq plugin to have your generator on its classpath you are best to put the java source in buildSrc/src/main/java
.
You’ll need to add jooq to the compile time dependencies in buildSrc/build.gradle
Dear Lance
To be sure: buildSrc means my project folder?
Jooq is of course in my dependencies, the Codegenerator works fine with the standard JavaGenerator.
compile 'org.jooq:jooq'
compile 'org.jooq:jooq:3.11.11'
compile'org.jooq:jooq-scala_2.12:3.11.11'
compile 'org.jooq:jooq-meta-extensions:3.11.11'
compile 'org.jooq:jooq-meta:3.11.11'
compile 'org.jooq:jooq-codegen:3.11.11'
runtime 'org.postgresql:postgresql:42.2.5'
testCompile 'junit:junit:4.11'
jooqRuntime 'org.postgresql:postgresql:42.2.5'
``
I want to adopt my Records to JavaFX thats why I want to have an own generator.
My MyJavaGenerator.java is in src/main/java/foo/bar, consequently the compiled class in buildScr/build/classes/java/main/foo/bar. (this order in Intellij)
I try to access with
generator {
name = 'foo.bar.JavaFXGenerator'
//name = 'org.jooq.codegen.JavaGenerator'
}
``
What is wrong?
Perhaps I have to add something here?
dependencies {
classpath 'org.jooq:jooq-codegen:3.11.11'
classpath 'com.h2database:h2:1.4.177'
classpath 'org.postgresql:postgresql:42.2.5'
}
``
Best and thank you in advance, Elke
Gesendet von [Mailspring](https://getmailspring.com/), der besten kostenlosen E-Mail-App für die Arbeit
To be sure: buildSrc means my project folder?
Incorrect, buildSrc
is a special project which is implicit to every gradle build. It’s where you put custom build utilities that end up on the buildscript classpath.
Ok, thank you once more… This is new to me (I developed my project from a sample project without buildSrc), I will try and answer you.
Best, Elke
Gesendet von Mailspring, der besten kostenlosen E-Mail-App für die Arbeit
Dear Lance
I included a buildSrc-folder in my project.
My buildSrc/build.gradle looks like:
repositories {
mavenCentral()
}
dependencies {
compile 'org.jooq:jooq'
compile 'org.jooq:jooq:3.11.11'
compile'org.jooq:jooq-scala_2.12:3.11.11'
compile 'org.jooq:jooq-meta-extensions:3.11.11'
compile 'org.jooq:jooq-meta:3.11.11'
compile 'org.jooq:jooq-codegen:3.11.11'
runtime 'org.postgresql:postgresql:42.2.5'
testCompile 'junit:junit:4.11'
compile 'com.github.javaparser:javaparser-core:3.14.5'
}
``
The error I get is always the same:
14:34:03: Executing task 'build'...
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:processResources NO-SOURCE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:sourcesJar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
> Task :wrapper
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:processResources NO-SOURCE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:sourcesJar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
> Task :generateSampleJooqSchemaSource FAILED
Juni 25, 2019 2:34:04 NACHM. org.jooq.tools.JooqLogger info
INFORMATION: Initialising properties : /home/elkemint/switchdrive/HLS/jpro/studentischeprojekte/build/tmp/generateSampleJooqSchemaSource/config.xml
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/home/elkemint/.gradle/caches/modules-2/files-2.1/com.sun.xml.bind/jaxb-impl/2.3.0.1/2e979dabb3e5e74a0686115075956391a14dece8/jaxb-impl-2.3.0.1.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Juni 25, 2019 2:34:05 NACHM. org.jooq.tools.JooqLogger error
SCHWERWIEGEND: Cannot read /home/elkemint/switchdrive/HLS/jpro/studentischeprojekte/build/tmp/generateSampleJooqSchemaSource/config.xml. Error : JavaFXGenerator
java.lang.ClassNotFoundException: JavaFXGenerator
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at org.jooq.codegen.GenerationTool.loadClass(GenerationTool.java:821)
at org.jooq.codegen.GenerationTool.run(GenerationTool.java:331)
at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:222)
at org.jooq.codegen.GenerationTool.main(GenerationTool.java:194)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':generateSampleJooqSchemaSource'.
> Process 'command '/usr/lib/jvm/java-11-openjdk-amd64/bin/java'' finished with non-zero exit value 255
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
1 actionable task: 1 executed
Process 'command '/usr/lib/jvm/java-11-openjdk-amd64/bin/java'' finished with non-zero exit value 255
14:34:05: Task execution finished 'build'.
What is wrong?
The class IS included in the jar in the buildSrc/build-folder.
I also tried several package structures in buildSrc/main/java and got warnings that the class would not be found by the main script using the old package name, so it seems to be connected anyhow.
I would be really thankful for your advice.
Best greetings, Elke
Is JavaFXGenerator in a package? Is it a public class?
Dear Lance
I now tried to generate my jooq classes without the plugin from Etienne Studer: generating an xml String for configuration and calling the GenerationTool.generate() Method with this xml String as argument.
When I put my custom generator as name tag in this xml, theclass (in buildSrc) ist found. So I have at least a workaround.
Best, Elke
Gesendet von Mailspring, der besten kostenlosen E-Mail-App für die Arbeit