Kotlin and theJOOQ Plugin

79 views
Skip to first unread message

Kevin Jones

unread,
Jan 16, 2024, 5:39:55 AM1/16/24
to jOOQ User Group
Hey Lukas,

thanks for the fixes in JOOQ 3.19.2 for Kotlin, that is all working now.

One issue I'm having though is with the JOOQ plugin. I've just switched to this after previously manually creating a generator task in my Gradle file, and I have a couple of issues (both of which maybe my mistakes)

The first issue (and the biggest) is that the generator task is not "depended upon" by the Kotlin compile task. So when I created my own generator task (called "generateJOOQ") that task was run as part of the Kotlin compilation.

However, when I use the plugin I have to manually a dependency on the "jooqCodegen" from the Kotlin compile task, so I need to add this to my buildfile:

tasks.compileKotlin {
    dependsOn(tasks["jooqCodegen"])
}

The other issue is around sourceSets. I'm configuring the generator to create its sources in a directory called build/generated, I do this by setting the 'basedir' property of the task to the Gradle build directory and then the 'target' directory of the generator to 'generated/java. I then have a sourceSet declaration in the Gradle file that points at that directory.

So something like this:

jooq {
    val output: Provider<Directory> = layout.buildDirectory.dir(".")
    configuration {
        basedir = "${output.get()}"
        jdbc {
           ...
        }
        generator {
            name = "org.jooq.codegen.KotlinGenerator"
            database {
                ...
            }
 
            target {
                packageName = "com.knowledgespike.db"
                directory = "generated/java"
                isClean = true
            }
        }
    }
}

This works, the code is generated into the correct directory and everything complies but the plugin also creates it's own sourceSet at "./generated/java" is there anyway to prevent that?

Thanks,

Kevin Jones

Lukas Eder

unread,
Jan 16, 2024, 7:09:09 AM1/16/24
to jooq...@googlegroups.com
Hi Kevin,

On Tue, Jan 16, 2024 at 11:39 AM Kevin Jones <ke...@knowledgespike.com> wrote:
Hey Lukas,

thanks for the fixes in JOOQ 3.19.2 for Kotlin, that is all working now.

One issue I'm having though is with the JOOQ plugin. I've just switched to this after previously manually creating a generator task in my Gradle file, and I have a couple of issues (both of which maybe my mistakes)

The first issue (and the biggest) is that the generator task is not "depended upon" by the Kotlin compile task. So when I created my own generator task (called "generateJOOQ") that task was run as part of the Kotlin compilation.

However, when I use the plugin I have to manually a dependency on the "jooqCodegen" from the Kotlin compile task, so I need to add this to my buildfile:

tasks.compileKotlin {
    dependsOn(tasks["jooqCodegen"])
}

This is what people wanted explicitly in this thread:

After a lot of discussion and comparison with other code generators, I agreed, this is how things should work with gradle. The jOOQ-codegen-gradle plugin shouldn't create such dependencies on your behalf.

However, it's probably worth documenting this in the manual. I've created an issue to collect documentation ideas like this:
 
The other issue is around sourceSets. I'm configuring the generator to create its sources in a directory called build/generated, I do this by setting the 'basedir' property of the task to the Gradle build directory and then the 'target' directory of the generator to 'generated/java. I then have a sourceSet declaration in the Gradle file that points at that directory.

So something like this:

jooq {
    val output: Provider<Directory> = layout.buildDirectory.dir(".")
    configuration {
        basedir = "${output.get()}"
        jdbc {
           ...
        }
        generator {
            name = "org.jooq.codegen.KotlinGenerator"
            database {
                ...
            }
 
            target {
                packageName = "com.knowledgespike.db"
                directory = "generated/java"
                isClean = true
            }
        }
    }
}

This works, the code is generated into the correct directory and everything complies but the plugin also creates it's own sourceSet at "./generated/java" is there anyway to prevent that?

The plugin currently generates all executions into the "main" source set (which may contain multiple directories, one per execution). There might be future versions of the plugin that are source set aware, or generate multiple source sets, but I don't have a clear picture of potential requirements yet. I've created an issue to track this topic:

Kevin Jones

unread,
Jan 16, 2024, 11:53:11 AM1/16/24
to jooq...@googlegroups.com
Thanks Lukas,

I read the discussion about the plugin's dependencies and I think what you've done is correct, adding it to the docs would be good :)

As to the sourceset, there are two things happening independently that I feel should be dependent: code is generated to a location and the sourceset is created.

In my case I want my code to be generated to 'build/generated/src/java' and I want this to be my sourceset.

Now, I can configure the plugin to put the generated code in 'build/generated/src/java' by setting the basedir and the target.directory properties. However it's not setting this to be the sourceset, it uses some default value.  If I want a non-default directory to be the sourceset then I have to do that myself in the Gradle file (which I was doing previously)

It seems that at the moment you create the sourceset location independently of the output location of the code generation. You always create the sourceSet at ',/generated/java', you seem to ignore the basedir property. Maybe it could be changed so that the sourceset location is the same as the location of the generated source?
I haven't used multiple executions so I'm don't know your sourceset creation plays into that, 

(BTW, the docs show an 'executions' element but I can't see that in the XSD, not sure if it should be in there)

Hope this all makes sense and thanks again for replying so quickly,

Kevin

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAB4ELO4ys0aAZo1WJOWaJ0Zs9OEjQrTvVhewdhtghKMnZwiTMQ%40mail.gmail.com.


--
Kevin Jones
KnowledgeSpike

Lukas Eder

unread,
Jan 16, 2024, 12:17:13 PM1/16/24
to jooq...@googlegroups.com
On Tue, Jan 16, 2024 at 5:53 PM Kevin Jones <ke...@knowledgespike.com> wrote:
Thanks Lukas,

I read the discussion about the plugin's dependencies and I think what you've done is correct, adding it to the docs would be good :)

As to the sourceset, there are two things happening independently that I feel should be dependent: code is generated to a location and the sourceset is created.

In my case I want my code to be generated to 'build/generated/src/java' and I want this to be my sourceset.

Now, I can configure the plugin to put the generated code in 'build/generated/src/java' by setting the basedir and the target.directory properties. However it's not setting this to be the sourceset, it uses some default value.  If I want a non-default directory to be the sourceset then I have to do that myself in the Gradle file (which I was doing previously)

It seems that at the moment you create the sourceset location independently of the output location of the code generation. You always create the sourceSet at ',/generated/java', you seem to ignore the basedir property. Maybe it could be changed so that the sourceset location is the same as the location of the generated source?
I haven't used multiple executions so I'm don't know your sourceset creation plays into that, 

Well, I fixed 2 bugs just today (keeping in mind that this is a very new feature):


So there might be more bugs. I'll double check on the basedir feedback tomorrow.

Note, you can always just use ${projectDir} in your directory definition:

directory "${projectDir}/generated/java"

That's probably easier anyway.

(BTW, the docs show an 'executions' element but I can't see that in the XSD, not sure if it should be in there)

The XSD starts at configuration. The name "executions" was simply chosen because I'm a Maven guy, and Maven plugins have executions, and I found this too useful a feature of plugins to just not have in gradle as well (I was surprised this doesn't come out of the box in gradle, but I'm still discovering the idiomatic way to use gradle). But it's a feature of the plugin, not of the code generator, which doesn't have a concept of executions (or build system, etc.), so indeed, it shouldn't be in the XSD. I'm not sure how to convey this in the docs. This stuff is all very standardised in Maven, so no additional docs are needed, but in "wild west gradle," I guess every plugin invents their own wheels and needs to come up with intuitive documentation...

Lukas Eder

unread,
Jan 23, 2024, 3:06:48 AM1/23/24
to jOOQ User Group
For the record, I've created this issue regarding the basedir, which currently doesn't seem to work correctly in the jOOQ-codegen-gradle plugin:
Reply all
Reply to author
Forward
0 new messages