2

guys.

I'm building a spring boot service. Right now I'm setting it up to query a local MySQL instance with jooq.

However, ./gradlew build gives error Unable to load class 'com.mysql.jdbc.Driver'.

Am I missing anything?

More Info

I'm able to see the com.mysql.jdbc.Driver class in Intellij. enter image description here

Here is my gradle script.

import nu.studer.gradle.jooq.JooqEdition

plugins {
    id 'org.springframework.boot' version '2.6.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'nu.studer.jooq' version '3.0.2'
    id 'java'
}

if(JavaVersion.current() != JavaVersion.VERSION_11){
    throw new GradleException("This build must be run with java 11")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

repositories {
    mavenCentral()
}

group = 'snorlax'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

//create a fat Jar with all dependencies
jar {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
    from {
        configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes "Main-Class": "com.snorlax.userservice.MainApplication"
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

dependencies {
    // Spring boot
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    // Swagger
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

    // Lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // RDS Connection
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java:8.0.27'
    implementation 'com.amazonaws.secretsmanager:aws-secretsmanager-jdbc:1.0.6'

    // JOOQ
    implementation 'org.springframework.boot:spring-boot-starter-jooq:2.6.2'
    implementation 'org.jooq:jooq-meta:3.15.5'
    implementation 'org.jooq:jooq-codegen:3.15.5'

}

test {
    useJUnitPlatform()
}

/************************
    jooq code generation
 *************************/
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*
GenerationTool.generate(new Configuration()
        .withJdbc(new Jdbc()
                .withDriver('com.mysql.jdbc.Driver')
                .withUrl('jdbc:mysql://127.0.0.1:3306/SnorlaxRds')
                .withUser('root')
                .withPassword('123456'))
        .withGenerator(new Generator()
                .withDatabase(new Database())
                .withGenerate(new Generate()
                        .withPojos(true)
                        .withDaos(true))
                .withTarget(new Target()
                        .withPackageName('com.snorlax.userservice')
                        .withDirectory('src/main/java/jooq'))))

Shadow
  • 33,525
  • 10
  • 51
  • 64
Yang Liu
  • 541
  • 9
  • 26
  • I followed https://www.jooq.org/doc/latest/manual/code-generation/codegen-gradle/ to set up the jooq codegen – Yang Liu Jan 15 '22 at 20:15
  • 1
    You are using the wrong class name for the MySQL Driver. It changed in version 8. Check the MySQL Connector/J 8.x documentation. – Stephen C Jan 17 '22 at 03:01
  • 1
    Probably not strictly related, but good to know anyway: https://stackoverflow.com/q/52344453/521799 – Lukas Eder Jan 17 '22 at 07:24

1 Answers1

2

My bad.

I missed the buildscript { } block mentioned here: https://www.jooq.org/doc/latest/manual/code-generation/codegen-gradle.

After adding below section, now my gradle build works.

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'org.jooq:jooq-codegen:3.16.2'
        classpath 'mysql:mysql-connector-java:8.0.27'
    }
}

Full version


buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'org.jooq:jooq-codegen:3.16.2'
        classpath 'mysql:mysql-connector-java:8.0.27'
    }
}

plugins {
    id 'org.springframework.boot' version '2.6.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

if(JavaVersion.current() != JavaVersion.VERSION_11){
    throw new GradleException("This build must be run with java 11")
}

repositories {
    mavenCentral()
}

group = 'snorlax'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

//create a fat Jar with all dependencies
jar {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
    from {
        configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes "Main-Class": "com.snorlax.userservice.MainApplication"
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

dependencies {
    // Spring boot
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    // Swagger
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

    // Lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // RDS Connection
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java:8.0.27'

    // AWS secretes manager
    implementation 'com.amazonaws.secretsmanager:aws-secretsmanager-jdbc:1.0.6'

    // JOOQ
    implementation 'org.springframework.boot:spring-boot-starter-jooq'
    implementation 'org.jooq:jooq-meta:3.16.2'
    compileOnly 'org.jooq:jooq-codegen:3.16.2'

}

test {
    useJUnitPlatform()
}

/************************
    jooq code generation
 *************************/
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.*;

task generate {
    def outputDirectory = projectDir.toString() + '/src/main/java'
    println outputDirectory
    def configuration = new Configuration()
            .withJdbc(new Jdbc()
            .withDriver('com.mysql.cj.jdbc.Driver')
            .withUrl('jdbc:mysql://127.0.0.1:3306/snorlaxRds')
            .withUser('root')
            .withPassword('123456'))
            .withGenerator(new Generator()
                    .withDatabase(new Database().withInputSchema("snorlaxRds"))
                    .withGenerate(new Generate()
                            .withPojos(true)
                            .withDaos(true))
                    .withTarget(new Target()
                            .withPackageName('snorlax.userservice.database')
                            .withDirectory(outputDirectory)));

    doLast {
        GenerationTool.generate(configuration)
    }
}


Yang Liu
  • 541
  • 9
  • 26