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
In Maven project source file store inside src/main/java
, src/main/resources
and test class store inside src/test/java
.
In Maven generated code (Compile code) stored into target/
folder.
When you build your Maven project, all generated code to be updated in target folder.
As much as i know there is no standard folder structure for generated sources. In my projects, i prefer src/gen/java
kind of notation.
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
src/main/java
and src-gen/main/java
are on the same depth in the directory treesrc-gen
contains bothgenerated/src/main/java
still apply (e.g. easy cleanup)Some thoughts/opinions about the other suggestions from the question:
/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 (<lang>-gen
is also another pattern already used in Eclipse projects)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/<lang>
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.
I think the location depends on how the source is generated and handled.
The source code is generated automatically during the build process: Then i would use target/main/java/
, target/test/java/
and so on. This code is not checked in into CVS since you can rebuild it fairly easy. In case you clean your project the target
directory will be removed and the source will be rebuild.
The source code is generated manually by an external tool or similar: I would use generated/src/main/java/
, generated/src/test/java/
, generated/src/main/resources/
and so on. This code should be checked in. A benefit is, as soon you see that the top-level directory name is generated
you know that all files/directories below are also generated. Also you have the standard maven directory structure under the top-level directory. Another point is that clean-up is easy, just delete generated
and recreate it, without looking through many other directories (like in your example: src/main/generated-java
and src/test/generated-java).
EDIT: Another nice solution would be to create a maven project which only contains the generated source like myproject-generated-1.0.3.jar
. This project would be a dependency in your real application. Then you would just put your generated source int src/main/java
.