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

System.out.println(bd.exportAsString());

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">
    <decorators>
        <class>org.demo.beans.BillingProcessorDecorator</class>
    </decorators>
    <alternatives>
        <class>org.demo.EventHandlers.EmailEventHandler</class>
        <class>org.demo.EventHandlers.LogEventHandler</class>
    </alternatives>
</beans>

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);

pd.persistenceUnit("myUnit")
		.classes(Person.class, User.class)
		.description("Default Persistence Unit")
		.formatSql()
		.schemaGenerationMode(SchemaGenerationModeType.CREATE_DROP)
		.jtaDataSource("java:/DefaultDS")
		.property("hibernate.cache.provider_class","org.hibernate.cache.HashtableCacheProvider")
		.version("2.0");

		System.out.println(pd.exportAsString());

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>
        <jta-data-source>java:/DefaultDS</jta-data-source>
        <class>org.demo.model.Person</class>
        <class>org.demo.model.User</class>
        <properties>
            <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"/>
        </properties>
    </persistence-unit>
</persistence>

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.