title: JAXB Overwriting package-info.java: What should be "namespace"?
body: <p>We're using xjc to generate JAXB Java classes for XML generation. Everything works fine except we tried to adjust the generated namespace prefixes <a href="http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html" rel="nofollow noreferrer">as described here</a>. We're stuck with "solution 2", adjusting package-info.java, due to the JAXB version we're using.</p>
  
  <p>The structure we have is several imports deep: root namespace imports other namespace, which in turn imports yet a third one. </p>
  
  <p><strong>MCVE xsds</strong></p>
  
  <p>root.xsd (imports other.xsd):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="root" xmlns:other="other" targetNamespace="root" version="1.0"&gt;
      &lt;xs:import namespace="other" schemaLocation="other.xsd" /&gt;
      &lt;xs:element name="rootElem"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:choice&gt;
                  &lt;xs:element ref="rootElem1"/&gt;
              &lt;/xs:choice&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="rootElem1" nillable="false"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="data"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:choice&gt;
                              &lt;xs:element ref="other:otherElem"/&gt;
                          &lt;/xs:choice&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;/xs:schema&gt;
  </code></pre>
  
  <p>other.xsd (imports third.xsd):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:other="other" xmlns:third="third" targetNamespace="other" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0"&gt;
  &lt;xsd:import namespace="third" schemaLocation="third.xsd" /&gt;
      &lt;xsd:element name="otherElem"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:sequence&gt;
                  &lt;xsd:element ref="third:thirdElem" /&gt;
              &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>third.xsd:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:third="third" targetNamespace="third" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0"&gt;
      &lt;xsd:element name="thirdElem"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:sequence&gt;
                  &lt;xsd:element name="thirdData" type="xsd:string" /&gt;
              &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>With this </p>
  
  <p><strong>Simple test case</strong></p>
  
  <pre><code>@Test
  public void test() throws JAXBException
  {
      Marshaller marshaller = JAXBContext.newInstance("test.jaxb.generated").createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
      marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
  
      RootElem root = new RootElem();
      RootElem1 root1 = new RootElem1();
      Data d = new Data();
      OtherElem other = new OtherElem();
      ThirdElem thirdElem = new ThirdElem();
      thirdElem.setThirdData("third");
      other.setThirdElem(thirdElem);
      d.setOtherElem(other);
      root1.setData(d);
      root.setRootElem1(root1);
  
      Path path = Paths.get("target", "outfile.xml");
  
      Result result = new StreamResult(path.toFile());
      marshaller.marshal(root, result);
  }
  </code></pre>
  
  <p>this results in this</p>
  
  <p><strong>Generated XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns4:rootElem xmlns:ns2="other" xmlns:ns3="third" xmlns:ns4="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd"&gt;
      &lt;ns4:rootElem1&gt;
          &lt;data&gt;
              &lt;ns2:otherElem&gt;
                  &lt;ns3:thirdElem&gt;
                      &lt;ns3:thirdData&gt;third&lt;/ns3:thirdData&gt;
                  &lt;/ns3:thirdElem&gt;
              &lt;/ns2:otherElem&gt;
          &lt;/data&gt;
      &lt;/ns4:rootElem1&gt;
  &lt;/ns4:rootElem&gt;
  </code></pre>
  
  <p>and everything is fine (except <code>data</code> which doesn't have any namespace associated, I'm assuming because that's an inner type).</p>
  
  <p>Now this is partly the question: here's the </p>
  
  <p><strong>generated package-info.java</strong></p>
  
  <pre><code>@javax.xml.bind.annotation.XmlSchema(namespace = "other", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
  package test.jaxb.generated;
  </code></pre>
  
  <p>Why is the <code>namespace</code> given referring to <code>other</code>? My root namespace is <code>root</code>. (<code>root.xsd</code> is the only file we're giving our Maven <code>jaxb2-maven-plugin</code>; we can include the others, it makes no difference).</p>
  
  <p><strong>Wrong replacement package-info.java</strong></p>
  
  <p>If we overwrite the generated one with this:</p>
  
  <pre><code>@javax.xml.bind.annotation.XmlSchema(
          namespace = "root",
          xmlns = {
                  @javax.xml.bind.annotation.XmlNs(prefix = "t", namespaceURI = "third"),
                  @javax.xml.bind.annotation.XmlNs(prefix = "o", namespaceURI = "other"),
                  @javax.xml.bind.annotation.XmlNs(prefix = "r", namespaceURI = "root")
          },
          elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
  package test.jaxb.generated;
  </code></pre>
  
  <p>which we initially did because we assumed we have to give the root namespace here - this is the</p>
  
  <p><strong>Wrong Generated XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;r:rootElem xmlns:t="third" xmlns:o="other" xmlns:r="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd"&gt;
      &lt;r:rootElem1&gt;
          &lt;data&gt;
              &lt;r:otherElem&gt;
                  &lt;t:thirdElem&gt;
                      &lt;t:thirdData&gt;third&lt;/t:thirdData&gt;
                  &lt;/t:thirdElem&gt;
              &lt;/r:otherElem&gt;
          &lt;/data&gt;
      &lt;/r:rootElem1&gt;
  &lt;/r:rootElem&gt;
  </code></pre>
  
  <p>Now the namespaces are pretty, but wrong! <code>otherElem</code> belongs to <code>o</code>, not <code>r</code>.</p>
  
  <p>Changing the namespace in the overwriting file back to <code>other</code> fixes the error, but again:</p>
  
  <p>the question is why is the required namespace <code>other</code> here? Just as confusing is the fact that the <code>third</code> imported layer is correct either way.</p>
  
  <p>The problem is fixed, but we'd like to understand the concept. What are we missing?</p>
  
  <p>EDIT:</p>
  
  <p>For completeness' sake, here's the <code>build</code> section of my <code>pom.xml</code>:</p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
          &lt;!-- run xjc --&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb2-maven-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;xjc-generate_classes&lt;/id&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;xjc&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;packageName&gt;test.jaxb.generated&lt;/packageName&gt;
                          &lt;schemaFiles&gt;
                              root.xsd
                          &lt;/schemaFiles&gt;
                          &lt;schemaDirectory&gt;${basedir}/src/main/resources/schemata&lt;/schemaDirectory&gt;
                          &lt;extension&gt;true&lt;/extension&gt;
                          &lt;bindingDirectory&gt;${basedir}/src/main/resources/bindings&lt;/bindingDirectory&gt;
                          &lt;outputDirectory&gt;${basedir}/target/generated-sources/jaxb/&lt;/outputDirectory&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
          &lt;!-- overwrite created package-info.java --&gt;
           &lt;plugin&gt;
              &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;copy-resources&lt;/id&gt;
                      &lt;phase&gt;process-resources&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;copy-resources&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;overwrite&gt;true&lt;/overwrite&gt;
                          &lt;outputDirectory&gt;${basedir}/target/generated-sources/jaxb/test/jaxb/generated&lt;/outputDirectory&gt;
                          &lt;resources&gt;
                              &lt;resource&gt;
                                  &lt;directory&gt;${basedir}/src/main/resources/bindings&lt;/directory&gt;
                                  &lt;include&gt;package-info.java&lt;/include&gt;
                              &lt;/resource&gt;
                          &lt;/resources&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>