mojohaus / jaxb2-maven-plugin

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

[question] transformSchema toFile with empty namespace #95

Closed basinilya closed 6 years ago

basinilya commented 6 years ago

How to get a predictable xsd file name (not schema1, schema2, etc.) when generating schema from a java package and with no namespace attribute? The <uri> tag is mandatory and cannot be empty.

package com.common.ftpconfiguration;

@XmlRootElement(name = "ftp_config")
public class FtpConfig {

2.2:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <encoding>${project.encoding}</encoding>
            </configuration>
            <executions>
                <execution>
                    <id>schemagen</id>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.build.sourceDirectory}/com/common/ftpconfiguration</source>
                        </sources>
                        <transformSchemas>
<!-- Silently ignored -->
                            <transformSchema>
                                <uri></uri>
                                <toFile>config.xsd</toFile>
                            </transformSchema>
                        </transformSchemas>
                    </configuration>
                </execution>
            </executions>
        </plugin>

1.6:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <encoding>${project.encoding}</encoding>
            </configuration>
            <executions>
                <execution>
                    <id>schemagen</id>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                    <configuration>
                        <transformSchemas>
                            <transformSchema>
<!-- Null or empty property 'uri' found in plugin configuration -->
                                <toFile>config.xsd</toFile>
                            </transformSchema>
                        </transformSchemas>
                        <includes>
                            <include>com/common/ftpconfiguration/*.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
simpletasks commented 6 years ago

Renaming should be possible. Cases without namespace attribute exists and for now only option is to use default schema1.xsd name.

lennartj commented 6 years ago

An XML namespace prefix is an alias for an XML namespace URI. Hence, well-formed XML documents define which namespace they belong to, on the form

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:foo="http://foo.com/someproject/bar" version="1.0">
    <foo:element>...</foo:element>
</xs:schema>

The plugin must therefore substitute the "foo" part within the generated XSD. Doing this with an empty namespace is not supported, and the schemagen tool emits such prefixes on the form "ns0", "ns1", "ns2" etc.

The same holds true for the naming of the generated schema files. The underlying SchemaGen tool is emitting files named "schema0.xsd", "schema1.xsd" etc.