1

I have the following gradle task in my build.gradle.kts. It's supposed to generate files from my JPA entities. However when running this task, upon succeeding, no file or directory is generated.

task(name = "generateJooq") {
    doLast {
        val configuration = Configuration().apply {

            generator = Generator().apply {
                database = Database().apply {
                    name = "org.jooq.util.jpa.JPADatabase"
                    properties = listOf(Property().apply {
                        key = "packages"
                        value = "com.example.sample"
                    })
                }

                target = Target().apply {
                    packageName = "com.example.jooq"
                    directory = "src/generated/java"
                }
            }
        }

        GenerationTool.generate(configuration)
    }
}

This is my entity living under package com.example.sample. Note that I wrote this entity in Java.

@Entity
public class Book {

    @Id
    private String id;

    @Column
    private String title;
}

Had the following logs

./gradlew clean generateJooq      

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
Chad
  • 2,041
  • 6
  • 25
  • 39

1 Answers1

3

I'm guessing that your Book class is in src/main/java correct?

And now you want that class to be available to JOOQ right? There's a bit of a chicken or egg problem where the Gradle buildscript classloader (which loads gradle tasks) is defined before src/main/java is compiled.

You'll need to pass a custom classloader to JOOQ (eg URLClassloader). JOOQ can then use the classloader to load the classes (hopefully the JOOQ API's support this). You can use sourceSets.main.runtimeClasspath to create an array of URL.

I see that GenerationTool has a setClassLoader(..) method here. I'm guessing you'll call that

See here for a similar solution (using Reflections rather than JOOQ but fundamentally the same).

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • That's probably it. [Here's a similar question where Maven is used](https://stackoverflow.com/a/43008349/521799) – Lukas Eder Sep 01 '17 at 11:53