4

I have two projects, A and B. In project A I have common.xsd file where I use cxf-xjc-plugin to generate java classes which work good. Next, I want to reuse this created files and type difinitions from common.xsd in main.xsd file in project B and don't generate classes from project A again, but there is a problem, because I can't include full path in my main.xsd file

 <xs:import namespace="..." schemaLocation="PATH_TO_PROJECT_A/common.xsd" /> 

or you classpath I read that it is possible to use http, but I can't use it. Is there a way to use a maven or something to do this ?

lexicore
  • 42,748
  • 17
  • 132
  • 221
Unyx
  • 97
  • 2
  • 8

1 Answers1

3

Yes, this is possible, keywords are:

  • Separate schema compilation with episodes to avoid generating classes which are already generated
  • Catalogs to override schema location including pointing to the schema inside the Maven artifact

Required disclaimer: I'm the author of the maven-jaxb2-plugin which provides these features.

Update:

I tried to use separate schema compilation, but unfortunately I can't undersant it. Should I create separate jar only for xsd file and use it as a dependency ? Now I have dependency to the whole project A which contains generated classes and xsd file

This is enough. You need a JAR with generated classes, XSD files and META-INF/sun-jaxb.episode. All of this is normally generated by default.

Then you can use this JAR as an "episode". XJC will use sun-jaxb.episode resource as binding file automatically, and this file basically says something like "don't generate anything for the schema in namespace X, use the following classes instead." This is one part which allows you to avoid redundant generation of classes. This works mostly, but sometimes XJC still generates a few things - they can be safely deleted.

The other thing is that you want to use schemas from the JAR. This can be done with catalogs and a special entity resolver which is by default delivered by the maven-jaxb2-plugin.

So just use any schemaLocation and then rewrite it in the catalog file:

REWRITE_SYSTEM
 "https://acme.com/foo/a.xsd"
 "maven:com.acme.foo:foo-a!/a.xsd"
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • I tried to use separate schema compilation, but unfortunately I can't undersant it. Should I create separate jar only for xsd file and use it as a dependency ? Now I have dependency to the whole project A which contains generated classes and xsd file – Unyx Sep 23 '14 at 09:50
  • I click on common.xsd in eclipse to generate jaxb classes and I have it, but I don't have in this project this episod file – Unyx Sep 23 '14 at 10:36
  • Well, then you probably can't solve it with pure Eclipse. See if there is a setting to include `-episode` argument there. – lexicore Sep 23 '14 at 10:43
  • Thank you, generating class work good, but I still don't have access to simple type definitions from common.xsd file in my main.xsd in project B. Could you explain in more detail how to do it? I would like to not use http.Is it possible ? I use maven – Unyx Sep 23 '14 at 12:06
  • Yes. Just write any URL like https://acme.com/foo/a.xsd and use a catalog to point it to maven:com.acme.foo:foo-a!/a.xsd. Catalog + entity resolver will get your schema from JAR then, no HTTP requests. – lexicore Sep 23 '14 at 13:40
  • but as I wrote I don't want to use http and upload my xsd file, because I can't do this. I found this https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/d/src/main/resources/catalog.cat and I tried to implement this, but I still get null pointer because types declared in common.xsd are invisible – Unyx Sep 23 '14 at 13:47
  • Should this catalog has extension .cat or may it be txt? – Unyx Sep 23 '14 at 13:49
  • If you rewrite the URL using the catalog file, you don't need to upload anything. Catalog file may have any extension as long as you configure it in the POM: https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/d/pom.xml – lexicore Sep 23 '14 at 13:50
  • I have REWRITE_SYSTEM "https://tmp/common.xsd" "maven:groupId_of_projectA:artifact_of_projectA!/common.xsd" in my src/main/resources/catalog.txt in project B and src/main/resources/catalog.txt in project B pom.xml How should look like import in my main.xsd . I tried but doesn't work – Unyx Sep 23 '14 at 14:07
  • Please check the https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/, it is a fully working example. If you use REWRITE_SYSTEM you have to specify the same URL in REWRITE_SYSTEM and in the schemaLocation of the import. You specify different URLs and use namespace instead of schemaLocation, no wonder it does not work. – lexicore Sep 23 '14 at 14:14
  • See this example of schema: https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/b/src/main/resources/b.xsd and this example of catalog: https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/b/src/main/resources/catalog.cat. – lexicore Sep 23 '14 at 14:16
  • I got nullpointer : Caused by: java.net.UnknownHostException: maven-jaxb2-plugin.dev.java.net – Unyx Sep 23 '14 at 14:26
  • 1
    The highsource.org and java.net links do not seem to be working at this time. I found https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Catalogs which may be more current and links to https://github.com/highsource/maven-jaxb2-plugin/wiki/Sample-Project-Using-Catalog – mdfst13 Aug 13 '18 at 05:49