Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOOQ plugin for latest JOOQ library in gradle project

Tags:

gradle

jooq

I have a problem with finding a working JOOQ plugin or its configuring for latest JOOQ library in my java gradle project.

I have found the following plugins:

  • https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-codegen-gradle

This doesn't work for me due to error message:

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/
  • https://github.com/ben-manes/gradle-jooq-plugin

That is working only for JOOQ 3.2.2(latest 3.4.2 is not working). If I set it up to 3.4.2, jooqGenerate executes successfully but data fetching invokes error message:

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

What i do wrong?

That is my 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 where I do data fetch:

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

EDIT:

Updated build.gradle file for https://github.com/etiennestuder/gradle-jooq-plugin:

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

Working gradle script:

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

Thanks to Lukas Eder and Etienne Studer, plugin is really working now!

like image 952
Viktor M. Avatar asked Aug 16 '14 10:08

Viktor M.


2 Answers

In the build.gradle file that you provide for the Gradle plugin 'nu.studer.jooq', you must use the equal sign for assigning configuration values:

Instead of:

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

It should be:

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

If you use the latest version of the plugin (1.0.5), you must give your configuration a name (since you can define multiple configurations) and define to which source set the generated sources are added:

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

See also https://github.com/etiennestuder/gradle-jooq-plugin#configuration

If you want to enforce the plugin to use a specific version of jOOQ, you can do it like this:

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

See also https://github.com/etiennestuder/gradle-jooq-plugin#custom-jooq-version

like image 168
Etienne Studer Avatar answered Nov 03 '22 01:11

Etienne Studer


The error message you're seeing:

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

Is really due to an @Override annotation being put on a method that is implemented from an interface, not from an abstract class. This usage of the @Override annotation is available only from Java 6 onwards. You should probably switch to using

sourceCompatibility = 1.6

An alternative jOOQ/Gradle plugin, which might be a bit easier to use and more up to date can also be found her:

  • https://github.com/etiennestuder/gradle-jooq-plugin

Standalone code generator

Note that you do not need to use any Gradle plugin to make use of jOOQ's standalone code generator, which can be provided with programmatic configuration, as can be seen in the examples:

  • https://github.com/jOOQ/jOOQ/tree/master/jOOQ-examples/jOOQ-codegen-gradle

For instance:

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

And then run the above with

gradle generate
like image 38
Lukas Eder Avatar answered Nov 03 '22 00:11

Lukas Eder