One of the great things about using Maven is that XMLBeans are handled quite intelligently. What we traditionally did with Ant was to create individual schema jars and have our actual code modules depend on them. This generally works, but it can be nicer to have the XMLBeans classes directly inserted into your project jars.
The Maven XMLBean plugin does this by default. You place your XML Schema files in src/main/xsd and the java source is first generated to target/generated-sources, then compiled to target/generated-classes, and finally copied over to target/classes with the rest of your code.
This works fine in Maven when using the comand line, but when we used the m2eclipse plugin there is a very strange error that can occasionally manifest.
The first manifestation of the error is when you have “Resolve Workspace Projects” enabled. What happens is that any project that has XMLBeans in it will not expose those beans correctly to projects which depend on it. Everything but the XMLBeans classes themselves is find, but you’ll keep getting spurious “class not found” problems for any XMLBean classes.
The second manifestation of the error is when you try to do a maven build run configuration with the “install” target. (This is when you don’t using the Maven Project Builder, which when you go to the project menu and hit clean works fine). At this point, occasionally (but not always) when you do an install, the compilation phase will fail and you will get the same sort of class not found errors as the mentioned above.
After a few weeks of struggling with this problem, we discovered that the root cause is the “scrub output directory” feature of Eclipse’s Java Builder. What occurs during a project rebuild is this:
- The Maven Builder runs. This generates the XML beans and compiles all of our other code.
- The Java Builder runs. This re-compiles all of our code. This is necessary to get all of Eclipse’s whiz-bang Java features to work.
The problem is that when you have the “srub output directory” feature enabled in the Java Builder, it will delete all of that XMLBeans code the Maven Builder worked so hard to compile. The code will still exist in target/generated-classes, but will effectively not have been copied over the the target/classes directory. Very annoying.
So, turn off scrub output directory when using the m2eclipse plugin.
