用于gradle项目中最新JOOQ库的JOOQ插件

时间:2014-08-16 10:09:38

标签: gradle jooq

我在java gradle项目中找到一个正在运行的JOOQ插件或配置最新的JOOQ库时遇到了问题。

我找到了以下插件:

由于错误消息,这对我不起作用:

Could not find any version that matches org.jooq:jooq-codegen-gradle:latest.integration.
Searched in the following locations:
    http://repo1.maven.org/maven2/org/jooq/jooq-codegen-gradle/maven-metadata.xml
    http://repo1.maven.org/maven2/org/jooq/jooq-codegen-gradle/

这仅适用于JOOQ 3.2.2(最新的3.4.2不起作用)。如果我将其设置为3.4.2,则jooqGenerate成功执行但数据提取会调用错误消息:

Error:(12, 8) java: org.jooq.generated.tables.records.PersonsRecord is not abstract and does not override abstract method values(java.lang.Integer,java.lang.String,java.lang.String) in org.jooq.Record3

我做错了什么?

那是我的gradle.build:

apply plugin: 'java'
apply plugin: 'jooq'

sourceCompatibility = 1.5
version = '1.0'

repositories {
    jcenter()
}

dependencies {
    compile 'org.jooq:jooq:3.4.2'
    compile 'org.jooq:jooq-meta:3.4.2'
    compile 'org.jooq:jooq-codegen:3.4.2'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.github.ben-manes:gradle-jooq-plugin:+'
        classpath 'mysql:mysql-connector-java:+'
    }
}

jooq {
    jdbc {
        url 'jdbc:mysql://localhost:3306'
        driver 'com.mysql.jdbc.Driver'
        user 'admin'
        passwoed 'xxx'
    }
    generator {
        database {
            name 'org.jooq.util.mysql.MySQLDatabase'
            inputSchema 'test_db'
            includes '.*'
        }
    }
}

我在做数据提取的App.class:

import static org.jooq.generated.Tables.PERSONS;

public class App {

    public static void main(String[] args) {
        try(Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db","admin","xxx")){
            DSLContext create = DSL.using(c, SQLDialect.MYSQL);

            for(PersonsRecord personsRecord : create.selectFrom(PERSONS).fetch()) {
                System.out.println(personsRecord
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

修改

更新了https://github.com/etiennestuder/gradle-jooq-plugin的build.gradle文件:

apply plugin: 'nu.studer.jooq'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'nu.studer:gradle-jooq-plugin:+'
        classpath 'mysql:mysql-connector-java:+'
    }
}

repositories {
    jcenter()
}

dependencies {
    compile 'org.jooq:jooq:+'
    compile 'org.jooq:jooq-meta:+'
    compile 'org.jooq:jooq-codegen:+'
    compile 'mysql:mysql-connector-java:+'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

jooq {
    jdbc {
        url 'jdbc:mysql://localhost:3306'
        driver 'com.mysql.jdbc.Driver'
        user 'admin'
        password 'xxx'
    }
    generator {
        database {
            name 'org.jooq.util.mysql.MySQLDatabase'
            inputSchema 'test_db'
            includes '.*'
        }
        generate {
            daos true
            classes false
        }
    }
}

使用gradle脚本:

apply plugin: 'nu.studer.jooq'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'nu.studer:gradle-jooq-plugin:1.0.5'
        classpath 'mysql:mysql-connector-java:+'
    }
}

repositories {
    jcenter()
}

dependencies {
    compile 'org.jooq:jooq:+'
    compile 'mysql:mysql-connector-java:+'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}


jooq {
    sample(sourceSets.main) {
        jdbc {
            driver = 'com.mysql.jdbc.Driver'
            url = 'jdbc:mysql://localhost:3306/test_db'
            user = 'admin'
            password = 'qwerty123'
            schema = 'test_db'
        }
        generator {
            name = 'org.jooq.util.DefaultGenerator'
            strategy {
                name = 'org.jooq.util.DefaultGeneratorStrategy'
            }
            database {
                name = 'org.jooq.util.mysql.MySQLDatabase'
                inputSchema = 'test_db'
            }
            generate {
                daos = true
            }
            target {
                packageName = 'org.homemade.warehouse.db'
                directory = 'src/main/java'
            }
        }
    }
}

感谢Lukas Eder和Etienne Studer,插件现在正在运行!

2 个答案:

答案 0 :(得分:7)

在为Gradle插件'nu.studer.jooq'提供的build.gradle文件中,必须使用等号分配配置值:

而不是:

jdbc {
    url 'jdbc:mysql://localhost:3306'
    ....
}

应该是:

jdbc {
    url = 'jdbc:mysql://localhost:3306'
    ....
}

如果使用最新版本的插件(1.0.5),则必须为配置命名(因为您可以定义多个配置)并定义添加生成的源的源集:

jooq {
    sample(sourceSets.main) {
        jdbc {
            ...
        }
    }
}

另见https://github.com/etiennestuder/gradle-jooq-plugin#configuration

如果你想强制插件使用特定版本的jOOQ,你可以这样做:

buildscript {
    ...

    configurations.classpath {
        resolutionStrategy {
            forcedModules = [
                'org.jooq:jooq:3.4.1',
                'org.jooq:jooq-meta:3.4.1',
                'org.jooq:jooq-codegen:3.4.1'
            ]     
        }
    }
}

另见https://github.com/etiennestuder/gradle-jooq-plugin#custom-jooq-version

答案 1 :(得分:2)

您看到的错误消息:

  

错误:(12,8)java:org.jooq.generated.tables.records.PersonsRecord不是抽象的,也不会覆盖抽象方法值(java.lang.Integer,java.lang.String,java.lang。字符串)在org.jooq.Record3

实际上是由于@Override注释放在从接口而不是抽象类实现的方法上。 @Override注释的这种用法仅在Java 6之后可用。您应该切换到使用

sourceCompatibility = 1.6

另一个jOOQ / Gradle插件,可能更容易使用,也可以找到更新的更新:

独立代码生成器

请注意,您不需要使用任何Gradle插件来使用jOOQ的独立代码生成器,它可以通过编程配置提供,如示例中所示:

例如:

// Configure the Java plugin and the dependencies
// ----------------------------------------------
apply plugin: 'java'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.jooq:jooq:3.5.0-SNAPSHOT'

    runtime 'com.h2database:h2:1.4.177'
    testCompile 'junit:junit:4.11'
}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'org.jooq:jooq-codegen:3.5.0-SNAPSHOT'
        classpath 'com.h2database:h2:1.4.177'
    }
}


task generate << {

    // Use your favourite XML builder to construct the code generation 
    // configuration file
    // ---------------------------------------------------------------
    def writer = new StringWriter()
    def xml = new groovy.xml.MarkupBuilder(writer)
    .configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.4.0.xsd') {
        jdbc() {
            driver('org.h2.Driver')
            url('jdbc:h2:~/test-gradle')
            user('sa')
            password('')
        }
        generator() {
            database() {
            }
            generate() {
            }
            target() {
                packageName('org.jooq.example.gradle.db')
                directory('src/main/java')
            }
        }
    }

    // Run the code generator
    // ----------------------
    org.jooq.util.GenerationTool.main(
        javax.xml.bind.JAXB.unmarshal(
            new StringReader(writer.toString()), 
            org.jooq.util.jaxb.Configuration.class
        )
    )
}

然后用

运行上面的内容
gradle generate