java - gradle 构建失败,jooq 配置 : java. lang.ClassNotFoundException : com. mysql.jdbc.Driver

标签 java spring-boot gradle jooq mysql-connector

伙计们。

我正在构建一个 Spring Boot 服务。现在我正在将其设置为使用 jooq 查询本地 MySQL 实例。

但是,./gradlew build给出错误无法加载类'com.mysql.jdbc.Driver'

我错过了什么吗?

更多信息

我能够在 Intellij 中看到 com.mysql.jdbc.Driver 类。 enter image description here

这是我的 gradle 脚本。

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

最佳答案

我的错。

我错过了这里提到的 buildscript { } block :https://www.jooq.org/doc/latest/manual/code-generation/codegen-gradle

添加以下部分后,现在我的 gradle 构建可以工作了。

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

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

完整版


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


关于java - gradle 构建失败,jooq 配置 : java. lang.ClassNotFoundException : com. mysql.jdbc.Driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70724802/

相关文章:

java - 在gradle的应用程序插件生成的启动脚本中,如何将程序名称传递给应用程序?

java - Gradle 的 Maven Publish 插件未将 POM 或正确版本发布到 Maven Local

spring-boot - 如何将JBPM设置为springboot微服务?

java - Gradle:使用 spring-boot 进行多项目构建

java - 如何使用Spring boot jpa 1.11在mysql中实现第一个最大记录

java - 使用值列表搜索 HashMap 的优化

java - Spring 无法在多个 TransactionManager bean 之间进行选择

java - Spring - 设计实用模块

java - 子资源在 Jersey REST API 框架中不起作用

在 Tomcat 中使用 waffle 的 Java SSO