title: How can I tell JAXB to generate classes in separate packages when two separate schemas have the same namespace?
body: <p>I suspect that there's no good answer to this, but hopefully I'm missing something. Let's say I have two separate XML schemas both with the same namespace, defining some duplicate complexTypes, and I want to generate Java classes for them with JAXB. As a very simplified example:</p>
  
  <p><strong>schema1.xsd:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema elementFormDefault="qualified" targetNamespace="namespace1" xmlns="namespace1" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="foo" type="xs:string"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>and</p>
  
  <p><strong>schema2.xsd:</strong> (identical to the above)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema elementFormDefault="qualified" targetNamespace="namespace1" xmlns="namespace1" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="foo" type="xs:string"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>When I try to generate these with JAXB (Ant XJC task via gradle), I get the following, entirely reasonable error:</p>
  
  <pre><code>[ant:xjc] [ERROR] 'foo' is already defined
  [ant:xjc]   line 3 of file:/C:/dev/test/src/main/resources/schema2.xsd
  [ant:xjc]
  [ant:xjc] [ERROR] (related to above error) the first definition appears here
  [ant:xjc]   line 3 of file:/C:/dev/test/src/main/resources/schema1.xsd
  </code></pre>
  
  <p>As per the <a href="https://jaxb.java.net/guide/Customizing_Java_packages.html" rel="nofollow">JAXB docs</a>, I tried specifying the package in an external customisation file like this:</p>
  
  <pre><code>&lt;jaxb:bindings schemaLocation="schema1.xsd"&gt;
      &lt;jaxb:schemaBindings&gt;
          &lt;jaxb:package name="package1"/&gt;
      &lt;/jaxb:schemaBindings&gt;
  &lt;/jaxb:bindings&gt;
  &lt;jaxb:bindings schemaLocation="schema2.xsd"&gt;
      &lt;jaxb:schemaBindings&gt;
          &lt;jaxb:package name="package2"/&gt;
      &lt;/jaxb:schemaBindings&gt;
  &lt;/jaxb:bindings&gt;
  </code></pre>
  
  <p>but that doesn't work - I then realised that the same docs linked above say:</p>
  
  <blockquote>
    <p>Note that this customization is per namespace. That is, even if your schema is split into multiple schema documents, you cannot put them into different packages if they are all in the same namespace.</p>
  </blockquote>
  
  <p>So, I'm guessing that's never going to work.</p>
  
  <p>Is there any sensible approach to this? I suspect the correct answer is that the schemas should be in different namespaces, as that would resolve the issue, and probably make more logical sense, but I don't have any control over the schemas - they are provided by a 3rd party. I've asked if it's possible to change them, but I suspect the answer is no.</p>
  
  <p>The only other option I can see is to generate the Java classes twice, first for schema1.xsd, and then separately for schema2.xsd, each time with their own customisation file specifying a different package, but that feels like a very clumsy solution. Is there any more sensible option that I'm missing?</p>