Location of generated source files for maven directory structure

后端 未结 4 1165
执笔经年
执笔经年 2020-12-29 20:20

Besides src/main/java folder, we have one folder that contains some generated java sources that are required for the main sources. Code generation is invoked

4条回答
  •  长情又很酷
    2020-12-29 21:00

    I totally agree with the accepted answer and only want to offer a slightly different suggestion for naming the directories:

    src-gen/main/java
    

    Background: In the Eclipse/Maven Tycho world (where code/resource generation often plays a large role) there is the src-gen directory for generated code, which has been established as some kind of standard convention. (the default project layout is a bit different compared to Maven, as all source files are directly in src and src-gen).

    In a Maven project that could be translated for example to src-gen/main/java, src-gen/test/java, src-gen/test/resources. I like that more than moving everything into a "generated" directory, because

    • Sources in src/main/javaand src-gen/main/java are on the same depth in the directory tree
    • It's more clear that src-gen contains both
      a) sources/resources that contribute to the build and
      b) generated stuff A folder named "generated" on the other hand could be anything.
    • All the mentioned advantages of generated/src/main/java still apply (e.g. easy cleanup)
    • After a quick google search it looks like there are already projects on Github that use this pattern

    Some thoughts/opinions about the other suggestions from the question:

    • Instead of /src/main/generated-java I would probably rather go with something like /src/main/java-gen which, when sorting directories alphabetically, keeps generated and regular code next to each other (-gen is also another pattern already used in Eclipse projects)
    • In my opinion gen fits in with the brief official names like src, it etc. more than generated. I've already seen src/gen/java a few times in the wild and have the feeling it is a more common than /src/generated/java. On the other hand some Maven plugins use the quite verbose target/generated-sources/ directory, so generated-sources/main/java could also be an option if your not into short names...

    Ultimately I think the naming doesn't matter that much and it is up to your preference since none of this is official convention.

提交回复
热议问题