2

I use the XMLBeans Maven plugin to generate classes based on an XSD file. I am able to write code using my generated classes and Eclipse shows target/generated-sources/xmlbeans as a source folder. However, when I try to run my test code I get the classic error:

java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.sCFA0DE5D65ADE16E20A85EAFD5A886E4.TypeSystemHolder

If I look in my project folder, I can see this class file in the folder target\generated-classes\xmlbeans\schemaorg_apache_xmlbeans\system\sCFA0DE5D65ADE16E20A85EAFD5A886E4.

Is there a change I can make to my POM file to make Eclipse know where to find these classes? I imagine there are a number of ways to manually fix this problem and tell Eclipse to add that folder to the classpath, but I'd prefer an automatic solution.

POM snippet

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xmlbeans-maven-plugin</artifactId>
    <version>2.3.3</version>
    <executions>
      <execution>
        <goals>
          <goal>xmlbeans</goal>
        </goals>
      </execution>
    </executions>
    <inherited>true</inherited>
    <configuration>
      <schemaDirectory>src/main/xsd</schemaDirectory>
      <download>true</download>
      <javaSource>1.5</javaSource>
    </configuration>
  </plugin>
Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254

2 Answers2

2

I have used this to incorporate generated code. Make sure to bind the plugin to a phase after the code generation happens, or if using the same phase, that this plugin configuration appears after the xmlbeans-maven-plugin configuration.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>${build.helper.maven.plugin.version}</version>
  <executions>
    <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>${xmlbeans.sourceGenerationDirectory}</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
user944849
  • 14,524
  • 2
  • 61
  • 83
  • Thanks for the response. I found a similar solution a few minutes ago, tried it and it seemed to work. But now I'm getting inconsistent results (i.e. removing this plugin and doing a clean/compile still results in code that works in Eclipse). Once I'm convinced this solves the problem, I'll mark as the answer. – Duncan Jones Mar 25 '14 at 16:55
  • In Eclipse, you might have to do Maven --> Update Project between POM edits to get Eclipse to actually recognize your change. What happens if you run Maven at the command line, with and without the plugin? – user944849 Mar 25 '14 at 16:59
  • If I execute from the command line (i.e. `mvn exec:java -Dexec.mainClass=...`) then the code works fine without any POM modifications. So the issue lies within Eclipse. Sadly, adding your POM alterations and performing a very thorough "clean" in Eclipse (delete project from workspace, run `mvn clean` in command line, re-add project to Eclipse) still gives me the error. – Duncan Jones Mar 26 '14 at 09:10
  • For now I've decided to [create a separate project to hold my xmlbeans output](http://stackoverflow.com/a/1198796/474189). But I would be interested in other solutions that don't require that. – Duncan Jones Mar 26 '14 at 09:31
  • 2
    This is a good answer. Unfortunately Eclipse m2e plugin does not understand the maven `build-helper-plugin` out of the box. So you either need to install a m2e connector (http://stackoverflow.com/a/12688755/1081849) or run the `generate-sources` phase at least once before running `Maven -> Update Project' in eclipse (because until that is run even Maven doens't know about the generated-sources directory). – sbk Mar 28 '14 at 13:48
1

Found a working solution for me - these guys wrote a maven connector. So you basically just have to install the XMLBeans connector from here.

Lonzak
  • 9,334
  • 5
  • 57
  • 88