2

I have a big problem with jaxb plugin. I have a project A where I have src/main/resources/xsd/common.xsd file. In this project I use cxf-xjc-plugin to generate java classes. I have also my episod file under src/main/resources/META-INF called sun-jaxb.episode

Next, I have project B which has maven dependency to project A. In this project I have src/main/resources/catalog.txt

PUBLIC "http://www.some_path" "maven:GROUP_ID_OF_PROJECT_A:ARTIFACT_ID_OF_PROJECT_A:jar::!/common.xsd"

In project B I have pom file with jaxb plugin

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <extension>true</extension>
                <episodes>
                    <episode>
                        <groupId>GROUP_ID_OF_PROJECT_A</groupId>
                        <artifactId>ARTIFACT_ID_OF_PROJECT_A</artifactId>
                    </episode>
                </episodes>
                <catalogs>
                    <catalog>src/main/resources/catalog.txt</catalog>
                </catalogs>
            </configuration>
 </plugin>

Next, I have in project B

 src/main/resources/other/xsd my main.xsd 

file where I use type definitions from common.xsd

I have xmlns:cmns="http://www.some_path" // it is the same as in catalog.txt and

<xs:import namespace="http://www.some_path"/> 

but the problem is that I get error undefined simple or complex type , because it doesn't recognize cmns

Xstian
  • 8,184
  • 10
  • 42
  • 72
Unyx
  • 97
  • 2
  • 8

1 Answers1

3

I use this plugin to solve the public import of the xsd

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <args>
            <arg>-Xannotate</arg>
            <arg>-Xnamespace-prefix</arg>
            <arg>-nv</arg>
        </args>
        <extension>true</extension>
        <forceRegenerate>true</forceRegenerate>
        <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory>
        <bindingIncludes>
            <include>*.xjb</include>
        </bindingIncludes>
        <schemas>
            <schema>
                <fileset>
                    <directory>${basedir}/src/main/resources/xsd</directory>
                    <includes>
                        <include>*.xsd</include>
                    </includes>
                </fileset>
            </schema>
            <schema>
                <dependencyResource>
                    <groupId>groupID</groupId>
                    <artifactId>artifactID</artifactId>
                    <resource>target.xsd</resource>
                </dependencyResource>
            </schema>

        </schemas>
        <episodes>
            <episode>
                <groupId>groupID</groupId>
                <artifactId>artifactID</artifactId>
            </episode>
        </episodes>
        <debug>true</debug>
        <verbose>true</verbose>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.2</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.2</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-namespace-prefix</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

Plugin for pom.xml of project A

<plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.8.1</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <args>
                <arg>-Xannotate</arg>
                <arg>-nv</arg>
                <arg>-Xnamespace-prefix</arg>
            </args>
            <extension>true</extension>
            <schemas>
                <schema>
                    <fileset>
                        <directory>${basedir}/src/main/resources/xsd/</directory>
                        <includes>
                            <include>A.xsd</include>
                        </includes>
                    </fileset>
                </schema>
            </schemas>
            <bindingDirectory>src/main/resources/xjb</bindingDirectory>
            <bindingIncludes>
                <include>*.xjb</include>
            </bindingIncludes>
            <debug>true</debug>
            <verbose>true</verbose>
            <forceRegenerate>true</forceRegenerate>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.0</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>0.6.0</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-namespace-prefix</artifactId>
                    <version>1.1</version>
                </plugin>
            </plugins>
        </configuration>
    </plugin>

Plugin for pom.xml of project B

<plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.8.1</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <args>
                <arg>-Xannotate</arg>
                <arg>-Xnamespace-prefix</arg>
                <arg>-nv</arg>
            </args>
            <extension>true</extension>
            <forceRegenerate>true</forceRegenerate>
            <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory>
            <bindingIncludes>
                <include>*.xjb</include>
            </bindingIncludes>
            <schemas>
                <schema>
                    <fileset>
                        <directory>${basedir}/src/main/resources/xsd/</directory>
                        <includes>
                            <include>B.xsd</include>
                        </includes>
                    </fileset>
                </schema>
                <schema>
                    <dependencyResource>
                        <groupId>AgroupID</groupId>
                        <artifactId>AartifactID</artifactId>
                        <resource>xsd/A.xsd</resource>
                    </dependencyResource>
                </schema>
            </schemas>
            <episodes>
                <episode>
                    <groupId>AgroupID</groupId>
                    <artifactId>AartifactID</artifactId>
                </episode>
            </episodes>
            <debug>true</debug>
            <verbose>true</verbose>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-namespace-prefix</artifactId>
                    <version>1.1</version>
                </plugin>
            </plugins>
        </configuration>
    </plugin>

You must perform a mvn install of project A before the project B


Add this plugin to add to source target generated classes

          <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/xjc</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Remember to perform Maven - update project

Xstian
  • 8,184
  • 10
  • 42
  • 72
  • 1
    +1 for using the official JAXB plugin. https://jaxb.java.net/ https://java.net/projects/maven-jaxb2-plugin/pages/Home – Puce Sep 24 '14 at 08:04
  • Strange, i work in an architecture complex based on this plugin. See this [link](http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Separateschemacompilation) to separate compilation and [this](http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Resourceentries) to refer a resource. – Xstian Sep 24 '14 at 08:24
  • I added an example of as it should be :) – Xstian Sep 24 '14 at 09:02
  • @Puce I am the author of the `maven-jaxb2-plugin` you mention. To be clear, this is NOT an "official" JAXB plugin. I am not an Oracle employee and not affiliated. – lexicore Sep 24 '14 at 09:15
  • It turns out that I can use in my main.xsd, but episod file seems to be ignored and common classes are generated twice ( in project A and B) – Unyx Sep 24 '14 at 09:34
  • You must remove schemaLocation="common.xsd" else identify as local source – Xstian Sep 24 '14 at 09:39
  • 1
    @lexicore thanks for the clarifiaction. But at least it's the only one Maven plugin mentioned on the official JAXB homepage: https://jaxb.java.net/ – Puce Sep 24 '14 at 09:42
  • It generates now, but in target folder. Can I specify it to generate in src/main/generated? – Unyx Sep 24 '14 at 11:09
  • I added src/main/generated/ but it still generate in target folder – Unyx Sep 24 '14 at 11:18
  • You should only copy build-helper-maven-plugin this plugin in your pom.xml (below my answer).. Let me know – Xstian Sep 24 '14 at 11:21
  • ? I have this one maven-jaxb2-plugin . Why should I have another plugin with the same executions etc – Unyx Sep 24 '14 at 11:25
  • The second one adds the classes generated to source. You need of this to work fine everything and use the generated classes. – Xstian Sep 24 '14 at 11:26
  • Have you perform a maven update project? .. as i say in my answer – Xstian Sep 24 '14 at 11:31
  • now it created target/generated-sourcex/xjc but I wanted something different. I have src/main/generated and I want that thse generated classes will be placed there, but when I specify src/main/generated it still create target/generated.. – Unyx Sep 24 '14 at 11:39
  • See the guide of that plugin [here](http://confluence.highsource.org/display/MJIIP/User+Guide). is that you needed. Anyway the topic is solved, you were able to resolve all by maven plugins. – Xstian Sep 24 '14 at 12:02