13

Looking at the jOOQ example of the Maven plugin configuration for the code generator (near the end of the configuration), I see that the target directory for the generated files is target/generated-sources/jooq.

Since the generator produces Java code, do you know why the suggestion here isn't a subfolder of /src, but instead a completely separated one? Is there any reason for this? Is it a best practice or something?

1 Answer 1

12

This is a good question and should probably also be covered in the jOOQ manual!

The question is not strictly related to jOOQ but to source code generation in general (e.g. also when using XJC to generate JAXB-annotated Java code from XSD). Some people prefer making generated source code part of the "main" source code, others prefer to keep it apart. There are essentially these ideas behind each approach:

Keeping things separate (e.g. target/generated-sources)

  • The only "source of truth" is the database schema. No copies thereof are ever stored in source form, only in binary form.
  • Source code generation becomes an integral part of your build. Everyone building your application (developers, continuous integration) will need to re-generate the sources. This will ensure that sources are always up to date.

Keeping things together (e.g. src/main/java)

  • The same "truth" is duplicated between the database schema and the generated copies thereof.
  • Source code generation is an "external" process, that isn't part of your build.
  • Generated sources are put under version control, where they are expected to be up-to-date.
  • Such version-controlled sources can be monitored for database changes, in the case of jOOQ.

There isn't really a generally preferred way. Both have their advantages.

7
  • 5
    Interesting, especially the "truth" part. Actually (this is for a webapp), I try to keep two collateral aims: a) to make everything work out-of-the-box for a new developer after checkout (no extra steps needed); b) to make life the easiest the possible for a web designer that has zero knowledge about Java, Maven, etc so that it needs almost no assistance (so a missing / out-of-date file shouldn't break his workflow). So far, duplication of truth seems reasonable, the database doesn't change unexpectedly, but the dev that makes the changes is responsible to regenerate the supporting classes.
    – watery
    Commented Aug 30, 2014 at 9:52
  • 1
    @watery: Fair enough. If you're opting for that solution, there had been a very interesting discussion on the jOOQ User Group about how to best do it
    – Lukas Eder
    Commented Aug 31, 2014 at 16:53
  • In Java (using Maven) I would strongly suggest the generally preferred way is keeping generated sources out of the repository whereas in other languages e.g. JavaScript (bower/npm) build artifacts are versioned
    – ooxi
    Commented Sep 1, 2014 at 5:36
  • @ooxi: There are compelling reasons for both approaches. I wouldn't go as far as saying that one way is "generally preferred"... It really depends on your build architecture. jOOQ-meta, for instance, includes generated classes for all 17 supported RDBMS's dictionary views / information_schemas. I wouldn't want to expect every developer and more so: contributor to set up all those 17 RDBMS just for the sake of applying a "generally preferred" way to deal with generated source code :-) so, clearly, jOOQ-meta generated source code is part of the repository.
    – Lukas Eder
    Commented Sep 1, 2014 at 7:00
  • 1
    @ooxi Is there any way to have the IDE (Eclipse in my case) automatically add the generated sources to the build path, like it does for each source and resource declared in the POM, thus requiring no manual intervention?
    – watery
    Commented Sep 2, 2014 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.