2

Is it possible to read values from properties file(say a database property file) in my code generator xml configuration file?

For Example, I have all my database properties stored in a separate file like:-

db.properties

local.datasource.url=jdbc:mysql://localhost:3307
local.datasource.username=testuser
local.datasource.password=testpassword
local.datasource.driver-class-name=com.mysql.jdbc.Driver

Above connection information is being used by my application to access database. I want my jooqCodeGen.xml to look somewhat like this:

jooqCodeGen.xml

   <jdbc>
        <driver>${local.datasource.driver-class-name}</driver>
        <url>${local.datasource.url}</url>
        <user>${local.datasource.username}</user>
        <password>${local.datasource.password}</password>
    </jdbc>
...

So that I don't have to duplicate the properties. I am triggering JOOQ code generation Tool from build.gradle

Build.gradle

task generateJooqDatabaseSource(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'org.jooq.util.GenerationTool'
    args = ['/jooqCodeGen.xml']
    standardOutput = System.out
    errorOutput = System.err
}

Is there a way we can achieve this?

Community
  • 1
  • 1
Aiden
  • 355
  • 5
  • 17

1 Answers1

1

You could generate the XML during the gradle build like on in the manual

http://www.jooq.org/doc/3.7/manual/code-generation/codegen-gradle/

(It's quite long so I won't duplicate it here)

Then all you need to do is load in db.properties so that the properties are available to the gradle build, e.g. (from https://discuss.gradle.org/t/how-to-include-an-external-properties-file/4263/2)

ext.dbProps = new Properties()
dbProps.load(file("db.properties"))

Then you can access your properties:

println dbProps['local.datasource.url']
artbristol
  • 32,010
  • 5
  • 70
  • 103