简体   繁体   中英

gradle build failed with jooq configuration: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

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. 在此处输入图像描述

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'))))

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'
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM