I would like to generate jaxb xsd schema from class files in jar. Currently, I am using jaxb2-maven-plugin to generate schema using java files.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>${maven.plugin.jaxb2}</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<quiet>true</quiet>
<includes>
<include>com/someProject/domain/*.java</include>
</includes>
<outputDirectory>${project.build.directory}/schemas</outputDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
But, I have a use case where I am getting a dependency jar file and would like to generate out of some classes from that jar file. Can anyone suggest how can it be done.
For simplicity reasons I reuse the code presented in this article to demonstrate the necessary steps to generate a Jaxb Schema from a Java class stored inside a Jar-archive
The code consists of two classes - Employee and Address:
and a referenced class:
This code is now compiled using the following command:
javac -d bin src/base/package/*.java
. This will compile all source files located in src to the bin directory:To get a proper Jar-archive for the compiled classes use:
jar -cf test.jar -C bin/ .
This will generate atest.jar
archive with the following content:You could now remove the bin directory and all of its content as all the files we need are stored within the archive and to prove that the schema is actually generated from the files inside the Jar archive.
As all preparations are finally done, the actual issue can be tackled - how to generate the schema from .class-files located in that test.jar archive:
Simply run this command:
schemagen -cp test.jar base.package.Employee
and it should generate a schema definition similar to the snippet below:Hope this was simple enough to follow
Edit: It seems that the jaxb2-maven-plugin as well as the ant-task are not able to use class-files at all, so probably the simplest and easiest solution would be to provide a script file (.bat on windows; .sh on *nix/Mac) where you just invoke the command manually:
As I'm currently running on Windows 7 a script that automatically generates the schema into a schemas subdirectory of the project would look like this:
You can then simply invoke that script (which I have placed in a
scripts
subdirectory of the project) using the maven exec-plugin which is bound to the generate-sources phase:The schemas should then get automatically generated on executing
mvn generate-sources
or any later phase maven provides.Edit: I've modified the script slightly as it is able to deal with wildcards, though you have to specify
*.jar
instead of only*
- but I guess this should be fine enough, at least this saves you from manually typing the names of the Jar-files that contain the JAXB classes