maven jaxb plugin configuration to generate XSD schema files

Here is how to configure your project in order to generate XSD schema files from Java JAXB annotated classes

<plugin>
 <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.5</version>
  <executions>
   <execution>
    <goals>
     <goal>schemagen</goal>
    </goals>
    <phase>generate-sources</phase>
    <configuration>
     <outputDirectory>${project.build.directory}/schemas</outputDirectory>
     <workDirectory>${project.build.directory}/generated-sources/jaxb</workDirectory>
    <includes>
     <include>${path_to_pojo_package}/*.java</include>
    </includes>
    <transformSchemas>
     <transformSchema>
      <uri>${namespace}</uri>
      <toFile>${xsd_filename}</toFile>
     </transformSchema>
    </transformSchemas>
   </configuration>
  </execution>
 </executions>
</plugin>

This will generate an XSD file named ${xsd_filename} in the target/schemas folder.

We chose the generate-source phase in order to make the schema file available for the sources (particularly for the test sources), in conjunction with the following resources configuration to add the schema to the generated artifact:

<resources>
 <resource>
  <directory>src/main/resources</directory>
 </resource>
 <resource>
 <directory>${project.build.directory}/schemas</directory>
  <targetPath>schemas</targetPath>
 </resource>
</resources>
Advertisements

A Maven configuration for a Java Webstart (JNLP) application – 2

In a previous post I detailed the Maven configuration to generate a Java Webstart application using the webstart-maven-plugin along with the maven-shade-plugin (the shade plugin allows to generate auto-executable jars).

I faced some troubles when I tried to use this configuration in another application requiring access to system resources (accessing files and directories for read and write operations) and using third party trusted libraries:

When the application needs grant access system resources, you must ask for that grant through the all-permissions configuration element in the jnlp file

<security>
     <all-permissions/>
</security>

However, to do this, you must sign all the jars: the application jar and the third party jars the application needs, and generally the third party jars you are using are signed and this makes the maven-shade-plugin fail when processing the whole. Even more, if the whole application is not signed with the same certification authority, it will fail executing on the client side.

So I ended up with this webstart-maven-plugin configuration (which happens to be even simpler than the one on my previous post):

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>webstart-maven-plugin</artifactId>
 <version>1.0-beta-3</version>
 <executions>
  <execution>
  <id>package</id>
  <phase>package</phase>
  <goals>
   <goal>jnlp</goal>
  </goals>
  <configuration>
   <codebase>${jnlp.URL}</codebase>
   <jnlp>
    <mainClass>${mainClass}/mainClass>
   </jnlp>
   <pack200>true</pack200>
   <sign>
    <keystore>${jks.filPath}</keystore>
    <keypass>${jks.keyPass}</keypass>
    <storePass>${jks.storePass}</storepass>
    <alias>${jks.alias}</alias>
    <verify>false</verify>
    </sign>
    <unsignAlreadySignedJars>true</unsignAlreadySignedJars>
   </configuration>
  </execution>
 </executions>
</plugin>

Note also that there is no need to the maven-shade-plugin.

The unsignAlreadySignedJars property allows to unsign the third party libraries you are using, and signing them again with your signing configuration.

Replace in the above plugin configuration the ${jnlp.URL}, ${mainClass} and ${jks.X} parameters with yours.

Customize your XML using Transform

A colleague of mine had a problem when generating an XML file (that he could not solve neither using XMLBeans nor using JAXB): his issue was to add a standalone=”no” in the XML file declaration (to be compliant with a third party specifications). You have to know that when setting standalone attribute to “no”, both libraries simply ignore that attribute (cause it’s the default value) and there is no way to get around that. We (I’d rather say He) have found a solution using the javax.xml.transform.Transformer class. In few words, this class allows you to transform a tree representation onto another tree representation, customizing and overriding any field attribute transformation.

Here is the way we achieved our goal (setting standalone=”on”) that could serve as a base usage example:

// get a new instance
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // to properly indent XML
StreamResult output = new StreamResult(new FileWriter(file2)); // output file to transform
StreamSource source = new StreamSource(file); // input file to transform
transformer.transform(source, output);
output.getWriter().flush();
output.getWriter().close();