mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
105 stars 76 forks source link

XjcMojo does not work when pom.relativePath is used for Parent Project reference #173

Open evanchsa opened 3 years ago

evanchsa commented 3 years ago

The XjcMojo (and perhaps others) calls relativize with the baseDir set to something like System.getProperty("user.dir") which appears to be the project from which the Maven invocation occurs (mvn clean install).

This does not work if the parent of a project is not in the base path of that project, for example:

as compared to a more standard Maven structure:

shoffmeister commented 3 years ago

I am hitting this problem, too.

This is a challenge whenever an aggregator POM is used, and where that aggregator pom is not hierarchically located "above" the pom.xml which includes the plugin.

The key is the specific current working directory from which "mvn " is invoked. I have attached a sample project which demonstrates the problem clearly.

Steps:

--> failure triggered by jaxb2-maven-plugin, and reported by the plugin as

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/home/myself/jaxb2-maven-plugin-error/aggregator/home/myself/jaxb2-maven-plugin-error/my-module/thing/my.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

while maven reports the error as

[ERROR] +=================== [XJC Error]
[ERROR] |
[ERROR] | 0: file:/home/myself/jaxb2-maven-plugin-error/my-module/thing/my.xsd
[ERROR] |
[ERROR] +=================== [End XJC Error]

Do note file:/home/myself/jaxb2-maven-plugin-error/my-module/thing/my.xsd (which exists) vs file:/home/myself/jaxb2-maven-plugin-error/aggregator/home/myself/jaxb2-maven-plugin-error/my-module/thing/my.xsd which does not exist. An in the latter case, note the assembled file string by the jaxb2-maven-plugin

file:
/home/myself/jaxb2-maven-plugin-error/aggregator
/home/myself/jaxb2-maven-plugin-error/my-module
/thing/my.xsd

so there was too much expansion. Note, also that ${project.basedir} is always correct in my-module; it's just that "some magic" is done inside the plugin that expands the current working directory.

shoffmeister commented 3 years ago

jaxb2-maven-plugin-error.zip

Archive with minimally reproducing set of poms for the the above.

shoffmeister commented 3 years ago

Note that https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/example_xjc_basic.html has

Note: The plugin currently requires that you run it from within the maven project directory, i.e. where the pom.xml file resides. If the project is part of a multi-module Maven build, Maven will take care of this for you.

Basically I read the note to say: "do not worry about multi-module", but, in fact, that's exactly where the problem is.

This is all with maven 3.6.3 (on Linux, JDK 8), and I suspect that, perhaps, jaxb2-maven-plugin uses the current working directory inappropriately.

shoffmeister commented 3 years ago

Possible work-around:

When triggering builds through an aggregator pom, with a multi-module build, make sure to have the operating system current working above the module.

In the above example with the aggregator pom, invoke mvn -f aggregator/pom.xml clean package instead of mvn -f pom.xml clean package

shoffmeister commented 3 years ago

Is it possible that PR #168 addresses this, problem, too?

bekwam commented 2 years ago

I ran into this today with Java 11 on Ubuntu 20.04 on a flat, multi-module project.

This is a repo that demonstrates the problem

https://github.com/bekwam/Maven-JAXB-2-Bug-Demo

juanjoc commented 2 years ago

In the above example with the aggregator pom, invoke mvn -f aggregator/pom.xml clean package instead of mvn -f pom.xml clean package

This work for me...