eclipse Maven - 如何处理生成的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7766338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven - how to handle generated classes
提问by nihilist84
My dilemma is that I have my doubts regarding generated source files in maven.
我的困境是我对 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.
我正在尝试从 WSDL 生成一些类,但我真的不知道处理生成的源文件的标准方法是什么。
- 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?
- 我应该在哪里生成 .java 源文件?(src/main/java, src/main/生成)
- 我应该将它们包含在源代码控制下,还是让它们在签出后生成
- 如果我不使用 src/main/java 文件夹,如何自动说服 Eclipse 将这些类“视为”源文件夹?
- 我真的需要 .java 文件,还是只需要 .class-es?
What are the best-practices regarding this issue? Any help or advice is appreciated.
关于这个问题的最佳实践是什么?任何帮助或建议表示赞赏。
Thanks for your kind answers, Mark
感谢您的友好回答,马克
回答by Brian Matthews
Most of the Mavenplugins 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 Plugingenerates the Java sources in the target/generated-sources/xjc
folder.
我遇到的大多数生成代码的Maven插件都遵循将生成的 Java 源文件放在文件夹子目录中的约定target/generated-sources
。例如,Maven 2 JAXB 2.x 插件在target/generated-sources/xjc
文件夹中生成 Java 源代码。
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, SVNor whatever I am using to ignore everything under target
.
只要构建是可重复的,我就不需要将生成的源代码提交到我的源代码存储库。所以我通常配置我的Git、Mercurial、SVN或任何我用来忽略target
.
I usually manually edit the .classpath
file to include the source folder for Eclipseand I store both the .classpath
and .project
files in the source code repository.
我通常手动编辑.classpath
文件以包含Eclipse的源文件夹,并将.classpath
和.project
文件都存储在源代码存储库中。
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 Mavenplugins don't attach the generated sources to the POM. You can use the Build Helper Maven Pluginto over come this.
需要注意的是,某些Maven插件不会将生成的源附加到 POM。您可以使用Build Helper Maven Plugin来解决这个问题。
回答by Javi Vazquez
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)
- 我应该在哪里生成 .java 源文件?(src/main/java, src/main/生成)
- 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 configurationto have Eclipse compile without errors.
- 我把它们放在 src/main/com/mypackage/generated 下。这样,它们已经在类路径中,您将不需要任何其他手动配置来让 Eclipse 编译没有错误。
- 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.
- 我曾经不包括它们,但是在一些问题之后(开发人员因为忘记而没有生成它们,缺少 IDE Maven 插件等),我们最终将它们添加到源代码控制中。这有助于任何人知道有一个生成的源包没有神奇地出现,只需浏览一个文件夹(一些源文件不再存在等)即可快速检查模式更改并查看应用程序的实际大小。
- If I don't use the src/main/java folder, how to convince Eclipse automatically to "see" those classes as source folder?
- 如果我不使用 src/main/java 文件夹,如何自动说服 Eclipse 将这些类“视为”源文件夹?
- Solved by using src/main/com/mypackage/generated.
- 通过使用 src/main/com/mypackage/generated.解决。
- Do I really need the .java files, or only the .class-es?
- 我真的需要 .java 文件,还是只需要 .class-es?
- I would suggest to use the .java files.
- 我建议使用 .java 文件。
Just my two cents, after years of using JAXB, mainly for WSDL to Java generation.
只是我的两分钱,经过多年使用 JAXB,主要用于 WSDL 到 Java 的生成。
回答by Adam Gent
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.
这是我多年后的建议:将所有代码生成放在一个单独的 maven 项目中,并在需要生成代码的普通项目中依赖它。
- 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)
- HoweverIf 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).
- Specifically not import the project into Eclipse and use
- If you need to debug generated code I highly recommend attaching sources to the jar with the maven-attach-sourcesplugin.
mvn install
如果是 SNAPSHOT,请为您生成的代码做一个。- 除非您知道自己在做什么,否则我不建议将生成的代码放在子模块中,否则 Eclipse 会不断混淆,因为大多数人导入所有子模块(即多模块项目)
- 但是,如果您决定执行多模块(即生成的代码是同级项目),您可以:
- 特别是不要将项目导入 Eclipse 并
mvn install
在单个项目上使用 - 依赖于 m2e 生命周期插件和 Build Helper 插件(这只适用于某些受高度支持的代码生成)。
- 特别是不要将项目导入 Eclipse 并
- 如果您需要调试生成的代码,我强烈建议使用maven-attach-sources插件将源附加到 jar 。
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.
虽然多模块方法在实践中似乎是一个好主意,但它变得非常烦人,因为如果有新的 SNAPSHOT,每次拉取时 Eclipse 都会不同步。
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).
如果您生成的代码不是经常生成,那么只需发布它,使其不是快照(显然这需要设置适当的 maven 存储库)。