简体   繁体   English

maven 目录结构的生成源文件的位置

[英]Location of generated source files for maven directory structure

Besides src/main/java folder, we have one folder that contains some generated java sources that are required for the main sources.除了src/main/java文件夹外,我们还有一个文件夹,其中包含一些生成的 java 源代码,这些源代码是主要源代码所必需的。 Code generation is invoked manually , when needed.需要时手动调用代码生成。 Generated source is checked into the source repo.生成的源代码被入源代码库。 Everything will be built and packed together .一切都将被构建并打包在一起

What would be the best location for generated java sources that are going to be compiled together with main sources?生成的 Java将与主要源一起编译的最佳位置是什么? Should it be:应该是:

  • /src/generated/java (following the same naming logic for src/testInt/java for integration tests) /src/generated/java (遵循与src/testInt/java相同的命名逻辑进行集成测试)
  • /generated-src/main/java (in collision with "The src directory contains all of the source material for building the project" ) /generated-src/main/java (与“src 目录包含构建项目的所有源材料”相冲突)
  • /src/main/generated-java (well... generated-java is not a type) /src/main/generated-java (嗯... generated-java不是类型)
  • ...? ...?

The first option seems like the most appropriate one for this case.第一个选项似乎最适合这种情况。 What do you think?你怎么看? Is there anything in Maven docs that describes this situation (that I have overlooked)? Maven 文档中是否有任何描述这种情况的内容(我忽略了)? Do you know any repo with similar structure?你知道有类似结构的回购吗?

Thank you.谢谢你。

Answer回答

As suggested by @Absurd-Mind, direction we are thinking about is to split the source into the submodules (which works nice in gradle).正如@Absurd-Mind 所建议的,我们正在考虑的方向是将源代码拆分为子模块(这在 gradle 中效果很好)。 So, the generated source and some other related source will go into its own submodule (they will produce the separate artifact) and the rest will go in other submodule, that uses this one.因此,生成的源和其他一些相关源将进入它自己的子模块(它们将产生单独的工件),其余的将进入使用这个的其他子模块。 Thank you.谢谢你。

I think the location depends on how the source is generated and handled.我认为位置取决于源的生成和处理方式。

  1. The source code is generated automatically during the build process : Then i would use target/main/java/ , target/test/java/ and so on.源代码在构建过程中自动生成:然后我会使用target/main/java/target/test/java/等等。 This code is not checked in into CVS since you can rebuild it fairly easy.这段代码没有签入 CVS,因为你可以很容易地重建它。 In case you clean your project the target directory will be removed and the source will be rebuild.如果您清理项目, target目录将被删除,源代码将被重建。

  2. 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.源代码是由外部工具或类似工具手动生成的:我会使用generated/src/main/java/generated/src/test/java/generated/src/main/resources/等等。 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.应该签入此代码。好处是,一旦您看到generated了顶级目录名称,您就知道下面的所有文件/目录也都生成了。 Also you have the standard maven directory structure under the top-level directory.您还拥有顶级目录下的标准 Maven 目录结构。 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).另一点是清理很容易,只需删除generated并重新创建它,无需查看许多其他目录(如您的示例中: src/main/generated-java和 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 .编辑:另一个不错的解决方案是创建一个仅包含生成源的 maven 项目,例如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 .然后,您只需将生成的源代码放入 int src/main/java即可。

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.在我的项目中,我更喜欢src/gen/java类型的符号。

I totally agree with the accepted answer .我完全同意接受的答案 I just want to offer a slightly different suggestion for naming the directory that contains code generated by third-party tools:我只是想提供一个稍微不同的建议来命名包含第三方工具生成的代码的目录:

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.背景:在Eclipse / Maven Tycho世界中(代码/资源生成经常扮演重要角色)有src-gen目录用于生成代码,该目录已被建立为某种标准约定。 (the default project layout is a bit different compared to Maven, as all source files are directly in src and src-gen ). (与 Maven 相比,默认项目布局有点不同,因为所有源文件都直接在srcsrc-gen中)。

In a Maven project that could be translated for example to src-gen/main/java , src-gen/main/resources , src-gen/test/java , src-gen/test/resources .在一个 Maven 项目中,例如可以翻译为src-gen/main/javasrc-gen/main/resourcessrc-gen/test/javasrc-gen/test/resources I like that more than moving everything into a "generated" directory, because我更喜欢将所有内容都移到“生成”目录中,因为

  • Sources in src/main/java and src-gen/main/java are on the same depth in the directory tree src/main/javasrc-gen/main/java中的源在目录树中的深度相同
  • It's more clear that src-gen contains generated sources/resources that contribute to the build.更清楚的是src-gen包含有助于构建的生成源/资源。 On the other hand a folder just named "generated" dosn't tell you much about its content.另一方面,刚刚命名为“生成”的文件夹并没有告诉您太多有关其内容的信息。 It could contain anything, like generated documentation or generated test data or it could be just a temporary folder.它可以包含任何内容,例如生成的文档或生成的测试数据,也可以只是一个临时文件夹。
  • All the mentioned advantages of generated/src/main/java still apply (eg easy cleanup) generated/src/main/java的所有提到的优点仍然适用(例如易于清理)
  • After a quick google search it looks like there are already projects on Github that use this pattern在快速谷歌搜索之后,看起来 Github 上已经有使用这种模式的项目

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 ( <lang>-gen is also another pattern already used in Eclipse projects)而不是/src/main/generated-java我可能更愿意使用/src/main/java-gen类的东西,当按字母顺序对目录进行排序时,生成的代码和常规代码彼此相邻( <lang>-gen也是Eclipse 项目中已经使用的另一种模式)
  • In my opinion gen fits in with the brief official names like src , it etc. more than generated .在我看来gen更适合于src等简短的官方名称, it不是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 .我已经在野外见过src/gen/java几次,感觉它比/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...另一方面,一些 Maven 插件使用相当冗长的target/generated-sources/<lang>目录,因此如果您不使用短名称,那么generated-sources/main/java也可以是一个选项......

Ultimately I think the naming doesn't matter that much and it is up to your preference since none of this is "official" convention.最终,我认为命名并不重要,这取决于您的喜好,因为这些都不是“官方”约定。

NetBeans adopted <project>/target/generated-sources/<tool> as the location for generated code. NetBeans 采用<project>/target/generated-sources/<tool>作为生成代码的位置。 As a result you would see an extra leaf appear in your project tree named as Generated Sources (<tool>) , and source code will be navigable and not show up with compiler errors in the markup.结果,您会在项目树中看到一个名为Generated Sources (<tool>)的额外叶子,并且源代码将是可导航的,并且不会在标记中显示编译器错误。

So if you commit your code, it will not be under the /target directory, and you will have to setup your maven project for the additional Source Code locations.因此,如果您提交代码,它将不在/target目录下,并且您必须为其他源代码位置设置 maven 项目。 Otherwise, I will suggest to keep with that adopted standard and put things under <project>/target/generated-sources/<tool> .否则,我会建议保持采用的标准并将内容放在<project>/target/generated-sources/<tool>下。

In Maven project source file store inside src/main/java , src/main/resources and test class store inside src/test/java .src/main/java内的 Maven 项目源文件存储中, src/main/resourcessrc/test/java内的测试类存储。
In Maven generated code (Compile code) stored into target/ folder.在 Maven 生成的代码(编译代码)中存储到target/文件夹中。
When you build your Maven project, all generated code to be updated in target folder.构建 Maven 项目时,所有生成的代码都会在目标文件夹中更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM