Maven - 如何处理生成的类

发布于 2024-12-10 08:02:54 字数 360 浏览 1 评论 0 原文

我的困境是我对 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未央 2024-12-17 08:02:54

我遇到的大多数 Maven 生成代码的插件都遵循将生成的 Java 源文件放置在target/ generated-sources 文件夹的子目录。例如, Maven 2 JAXB 2.x 插件 生成Java 源代码位于 target/ generated-sources/xjc 文件夹中。

只要构建是可重复的,我就不需要将生成的源提交到我的源代码存储库。所以我通常配置我的 GitMercurialSVN 或任何我用来忽略下面所有内容的东西目标

我通常手动编辑 .classpath 文件以包含 Eclipse 的源文件夹,并且我将 .classpath.project 文件存储在源代码存储库中。

下面是一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
    <classpathentry kind="src" path="target/generated-sources/xjc"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

需要注意的是,某些 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 the target/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:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
    <classpathentry kind="src" path="target/generated-sources/xjc"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

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.

菩提树下叶撕阳。 2024-12-17 08:02:54

我从未找到处理生成的源文件的标准方法。不过,根据我的经验,我会向您推荐以下内容:

  • 我应该在哪里生成 .java 源文件? (src/main/java,
    src/main/生成)
  • 我把它们放在 src/main/com/mypackage/ generated 下。这样,它们已经在类路径中,并且您将不需要任何其他手动配置来让 Eclipse 编译而不会出现错误。
  • 我应该将它们纳入源代码控制之下,还是让它们在签出后生成
  • 过去不包含它们,但是在出现一些问题之后(开发人员没有生成它们,因为他们忘记了,缺少 IDE Maven 插件等)我们最终将它们添加到源代码管理中。这帮助任何人知道有一个生成的源包并没有神奇地出现,只需浏览一个文件夹即可快速检查模式更改(某些源文件不再存在,等等)并查看该源的实际大小应用程序。
  • 如果我不使用 src/main/java 文件夹,如何说服 Eclipse 自动将这些类“视为”源文件夹?
  • 通过使用 src/main/com/mypackage/ generated 解决。
  • 我真的需要 .java 文件,还是只需要 .class-es?
  • 我建议使用 .java 文件。

只是我的两分钱,在使用 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:

  • Where should I generate the .java source files? (src/main/java,
    src/main/generated)
  • I put them under src/main/com/mypackage/generated. This way, they are already in the classpath and you will not need any other manual configuration to have Eclipse compile without errors.
  • Should I include them under source control, or let them be generated after check-out
  • I used to NOT include them, but after some issues (developers not generating them because they forgot, lack of IDE Maven plugin, etc.) we end up adding them to source control. This helped anyone know that there is a package of generated sources that didn't appear magically, quickly check schemas changes by just exploring a folder (some source files do not exist anymore, etc.) and see the real size of the app.
  • If I don't use the src/main/java folder, how to convince Eclipse automatically to "see" those classes as source folder?
  • Solved by using src/main/com/mypackage/generated.
  • Do I really need the .java files, or only the .class-es?
  • I would suggest to use the .java files.

Just my two cents, after years of using JAXB, mainly for WSDL to Java generation.

俯瞰星空 2024-12-17 08:02:54

这是我多年后的建议:将所有代码生成放在一个单独的 Maven 项目中,并在需要生成代码的正常项目中依赖它。

  • 执行 mvn install 对于您生成的代码(如果它是快照)。
  • 除非您知道自己在做什么,否则我不建议将生成的代码放入子模块中,否则 Eclipse 会不断感到困惑,因为大多数人都会导入所有子模块(即多模块项目)
  • 但是如果您确实决定进行多模块(即生成的代码是同级项目)您可以:
    • 特别不要将项目导入 Eclipse 并在单个项目上使用 mvn install
    • 依赖 m2e 生命周期插件和 Build Helper 插件(这只适用于某些受到大力支持的代码生成)。
  • 如果您需要调试生成的代码,我强烈建议使用 maven-attach-sources 插件。

虽然多模块方法在实践中似乎是个好主意,但它真的变得非常烦人,因为如果有新的快照,每次拉取时 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.

  • Do a mvn install for your generated code if it is SNAPSHOT.
  • Unless you know what you are doing I do not recommend putting the generated code in a submodule otherwise Eclipse will constantly get confused because most people import all submodules (ie multimodule project)
  • However If you do decide to do multimodule (ie generated code is a sibling project) you can either:
    • Specifically not import the project into Eclipse and use mvn install on the individual project
    • Rely on m2e lifecycle plugins and Build Helper plugin (this only works well on certain code generation that is heavily supported).
  • If you need to debug generated code I highly recommend attaching sources to the jar with the maven-attach-sources plugin.

While 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文