Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - how to handle generated classes

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

like image 391
nihilist84 Avatar asked Oct 14 '11 10:10

nihilist84


2 Answers

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.

like image 106
Brian Matthews Avatar answered Sep 21 '22 21:09

Brian Matthews


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.

like image 33
Javi Vazquez Avatar answered Sep 19 '22 21:09

Javi Vazquez