Creating XML Schema from JAXB Class Files in Ant

Can shemagen ant Task be used to generate an xsd schema from class files instead of a source?

+3
source share
1 answer

Perhaps you could write something quite easily, and then call it Ant:

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class SchemaGenerator {

    public static void main(String[] args) throws Exception {
        String contextPath = args[0];
        String outputDir = args[1];
        JAXBContext jc = JAXBContext.newInstance(contextPath);
        jc.generateSchema(new MySchemaOutputResolver(schemaFileName));
    }

    private static class MySchemaOutputResolver extends SchemaOutputResolver {

        private String outputDir;

        public MySchemaOutputResolver(String outputDir) {
            this.outputDir = outputDir;
        }

        public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
            File file = new File(outputDir + "/" + suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }

    }   

}

In your context, you will need a jaxb.index file with a list of classes that will be included in your JAXBContext. Or you can pass class names to the SchemaGenerator class and load them through ClassLoader.

+2
source

Source: https://habr.com/ru/post/1756407/


All Articles