If you’ve used Shrinkwrap you might have noticed that creating configuration files can be a bit of a burden requiring you to manually build XML configuration files yourself as strings. This article shows how the DSLs being added to Shrinkwrap will make configuring your deployments far easier.

I’m not sure on the current status of the DSL project, but you can find it on GitHub on the shrinkwrap / descriptors page. These are still in development, but as they are fairly stand-alone, they work well enough to use. To get started, download the maven project from the GitHub page, build and install it.

The descriptors project contains classes implementing DSLs for creating configuration files that can be exported to text. These files can then be added to the Shrinkwrap archive. Here’s a basic example that adds a beans.xml for CDI support.

// first create the descriptor
BeansDescriptor bd = Descriptors.create(BeansDescriptor.class);

// add alternatives classes
bd.alternativeClasses(EmailEventHandler.class, LogEventHandler.class)		
.decorator(BillingProcessorDecorator.class);// add decorator classes


Produces a result of

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans1_0.xsd">

First you must get an instance of the descriptor which is the basis for the dsl, and then you can define the properties for the descriptor using the methods defined on it, chaining the calls as needed.

Another example uses the PersistenceDescriptor and can be used to create persistence.xml files :

PersistenceDescriptor pd = Descriptors.create(PersistenceDescriptor.class);

		.classes(Person.class, User.class)
		.description("Default Persistence Unit")


Produces :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="myUnit">
        <description>Default Persistence Unit</description>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>

The project also supports importing configuration files to the associated description type since the descriptors are modeled using JAXB XML bindings. At the moment the following descriptor types are supported :

  • web.xml – Web deployment descriptor file
  • persistence.xml – JPA Persistence config file
  • faces-config.xml – Configuration file for Java Server Faces
  • beans.xml – Triggers the use of CDI and can contain configuration info

While the goal is to use these to help generate Shrinkwrap archives, particularly for Arquillian testing, there’s nothing wrong with using them to automatically generate configuration files, especially for new projects.