Friday, March 7, 2008

Maven 2, Xalan, and the evil 2.2D11 / 2.4.1 version

A few days ago, I ran into one of those frustrating situations with Maven 2: a multi-module build where each sub-project builds perfectly individually, but building from the top-level POM fails.

In that particular case, Maven was trying to download the Spring JARs in a mysterious "2.2D11" version, which was not at all the one declared in my POM:
Missing: ---------- 1) org.springframework:spring-beans:jar:2.2.D11
Try downloading the file manually from the project website.

Fortunately, this was not the first time I ran into phantom dependency versions: there used to be a similar bug with the m2eclipse plugin, where "2.4.1" versions would sometimes inexplicably pop up in Eclipse. But, what is even more fortunate, someone pintpointed the problem:

- if Java 1.4.2 is the first entry in the %PATH% on the machine, this java version will be used to run eclipse - Java 1.4.2 contains some Apache XML parser that defines, among others, a silly environment variable "version" with value "2.4.1" - m2plugin tells Maven (if I understand correctly) via Maven Embedder to evaluate dependencies - Maven Embedder has a bug (or "feature") by which variables like {project.version} are overridden by corresponding system variables (i.e. "version"). - as a result, Maven is looking for version "2.4.1" of such dependencies instead of the desired value of {project.version}, and fails to build

"Some Apache XML parser" is actually Xalan-J. And it turns out that my project uses a custom mojo that outputs an XML file using the javax.xml.transform.Transformer interface, which is precisely what Xalan implements:
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);

StreamResult result = new StreamResult(dest);
transformer.transform(source, result);

I figured that if a system property called version created the problem, removing it could be the solution. Therefore, I added the following line just after the preceding code:
   System.getProperties().remove("version");

And that did the trick :-)

So the bottom line is: Maven + Xalan = beware of the evil version property...

Environment:
  • Apache Maven 2.0.8
  • JDK 1.4.1_05 from BEA Weblogic 8.1 SP2 (I guess that explains the different Xalan version)
  • Windows XP

0 comments: