我的困境是我对 Maven 中生成的源文件有疑问。
我正在尝试从 WSDL 生成一些类,但我真的不知道处理生成的源文件的标准方法是什么。
- 我应该在哪里生成 .java 源文件? (src/main/java, src/main/ generated)
- 我应该将它们包含在源代码控制下,还是让它们在签出后生成
- 如果我不使用 src/main/java 文件夹,如何说服 Eclipse 自动“看到”那些类作为源文件夹?
- 我真的需要 .java 文件,还是只需要 .class-es?
关于这个问题的最佳实践是什么?任何帮助或建议表示赞赏。
感谢您的热心解答,
标记
My dilemma is that I have my doubts regarding generated source files in maven.
I'm trying to generate some classes from a WSDL and I don't really know what is the standard way to handle the resulting source files.
- Where should I generate the .java source files? (src/main/java, src/main/generated)
- Should I include them under source control, or let them be generated after check-out
- If I don't use the src/main/java folder, how to convince Eclipse automatically to "see" those classes as source folder?
- Do I really need the .java files, or only the .class-es?
What are the best-practices regarding this issue? Any help or advice is appreciated.
Thanks for your kind answers,
Mark
发布评论
评论(3)
我遇到的大多数 Maven 生成代码的插件都遵循将生成的 Java 源文件放置在
target/ generated-sources
文件夹的子目录。例如, Maven 2 JAXB 2.x 插件 生成Java 源代码位于target/ generated-sources/xjc
文件夹中。只要构建是可重复的,我就不需要将生成的源提交到我的源代码存储库。所以我通常配置我的 Git,Mercurial、SVN 或任何我用来忽略下面所有内容的东西
目标
。我通常手动编辑
.classpath
文件以包含 Eclipse 的源文件夹,并且我将.classpath
和.project
文件存储在源代码存储库中。下面是一个示例:
需要注意的是,某些 Maven 插件不会将生成的源附加到 POM 。您可以使用 Build Helper Maven 插件 来解决这个问题。
Most of the Maven plugins I've come across that generate code follow a convention of placing the generated Java source files in a sub-directory of the
target/generated-sources
folder. For an example, the Maven 2 JAXB 2.x Plugin generates the Java sources in thetarget/generated-sources/xjc
folder.As long as the build is repeatable I don't see the need to commit the generated sources to my source code repository. So I usually configure my Git, Mercurial, SVN or whatever I am using to ignore everything under
target
.I usually manually edit the
.classpath
file to include the source folder for Eclipse and I store both the.classpath
and.project
files in the source code repository.Here is an example:
It is important to note that some Maven plugins don't attach the generated sources to the POM. You can use the Build Helper Maven Plugin to over come this.
我从未找到处理生成的源文件的标准方法。不过,根据我的经验,我会向您推荐以下内容:
只是我的两分钱,在使用 JAXB 多年之后,主要用于 WSDL 到 Java 的生成。
I never found a standard way to handle the resulting source files. However, based on my experience I would recommend you the following:
Just my two cents, after years of using JAXB, mainly for WSDL to Java generation.
这是我多年后的建议:将所有代码生成放在一个单独的 Maven 项目中,并在需要生成代码的正常项目中依赖它。
mvn install
对于您生成的代码(如果它是快照)。mvn install
虽然多模块方法在实践中似乎是个好主意,但它真的变得非常烦人,因为如果有新的快照,每次拉取时 Eclipse 都会不同步。
如果您生成的代码不是经常生成,那么只需将其释放,这样它就不是快照(显然这需要设置适当的 Maven 存储库)。
Here is my recommendation after many many years: Put all your code generation in a separate maven project and depend on it in your normal projects that need the generated code.
mvn install
for your generated code if it is SNAPSHOT.mvn install
on the individual projectWhile the multimodule approach seems like a good idea in practice it just becomes really freaking annoying because Eclipse will get out of sync every time you pull if there is a new SNAPSHOT.
If your generated code is not generated that often then just release it so it is not a SNAPSHOT (obviously this requires a proper maven repository being setup).