[Solved] jooq Customize GeneratorStrategy Error: ClassNotFoundException

Problem description

tip: describe the problems encountered in the project here:

Gradle (kotlin project)

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*

plugins {
    id("org.springframework.boot") version "2.7.1"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"

}

group = "com.nova"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.jooq:jooq-codegen:3.14.7")
        classpath("mysql:mysql-connector-java:8.0.29")
    }
}


dependencies {

    implementation("org.springframework.boot:spring-boot-starter-jooq")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    runtimeOnly("mysql:mysql-connector-java:8.0.29")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.jooq:jooq:3.14.7")
    implementation("org.jooq:jooq-meta:3.14.7")
    implementation("org.jooq:jooq-codegen:3.14.7")

}

tasks.register("JOOQ_Code_Generate") {
    doLast{
        val config: org.jooq.meta.jaxb.Configuration = Configuration()
            .withJdbc(Jdbc()
                .withDriver("com.mysql.cj.jdbc.Driver")
                .withUrl("jdbc:mysql://127.0.0.1:3306/learn-jooq?characterEncoding=UTF-8&serverTimezone=GMT")
                .withUsername("root")
                .withPassword("root"))
            .withGenerator(Generator()
                    //default is java
                .withName("org.jooq.codegen.KotlinGenerator")
                .withGenerate(Generate()
                    .withComments(true) 
                    .withCommentsOnCatalogs(true)
                    .withRelations(true)
                    .withImmutablePojos(false) // if true, cannot use 'into()' method
                    .withInterfaces(true)
                    .withDaos(true))
                .withDatabase(Database()
                    .withName("org.jooq.meta.mysql.MySQLDatabase")
                    .withIncludes("s.*")
                    .withInputSchema("learn-jooq")
                )
                .withTarget(org.jooq.meta.jaxb.Target()
                    .withPackageName("com.nova.novademo.codegen")
                    .withDirectory("src/main/kotlin"))
                .withStrategy(Strategy() //Customized generation policy, error reporting
                    .withName("com.nova.novademo.CustomGeneratorStrategy")
//                    .withName("org.jooq.codegen.DefaultGeneratorStrategy")
                )
            )
        GenerationTool.generate(config)
    }
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Maven (Java project)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lzy</groupId>
    <artifactId>JOOQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JOOQ</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
            <version>3.14.15</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen</artifactId>
            <version>3.14.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>


            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>


            <!-- jooq code generate plugin-->
            <plugin>

                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>${jooq.version}</version>
                
                <configuration>
                    <jdbc>
                        <driver>com.mysql.cj.jdbc.Driver</driver>
                        <url>jdbc:mysql://127.0.0.1:3306/learn-jooq?serverTimezone=GMT%2B8</url>
                        <user>root</user>
                        <password>root</password>
                    </jdbc>
                    <generator>
                        <strategy>
                            <name>com.lzy.jooq.CustomGeneratorStrategy</name>
                        </strategy>
                        <generate>
                            <pojos>true</pojos>
                            <daos>true</daos>
                        </generate>
                        <database>
                            <includes>s.*</includes>
                            <inputSchema>learn-jooq</inputSchema>
                        </database>
                        <target>
                            <packageName>com.lzy.jooq.codegen</packageName>
                            <directory>/src/main/java</directory>
                        </target>
                    </generator>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>



Cause analysis:

After specifying the GeneratorStrategy as a custom CustomGeneratorStrategy, an error ClassNotFoundException is reported.


Solution:

You can compile or run it first, as long as you generate the corresponding .class file under the target file.

Read More: