ShrinkWrap

ShrinkWrap is an open-source project sponsored by Red Hat. It provides an API and SPI for easily creating, importing, exporting and manipulating archives. ShrinkWrap works in-memory, but can also be used to create archives on disk.

ShrinkWrap Basics

The primary entry-point into ShrinkWrap is the ShrinkWrap class and its create(class) method. You can create simple Java or Web archives (.jar and .war, respectively) by passing JavaArchive.class or WebArchive.class to the create(…​) method.

WildFly Swarm additionally makes other types of archives available, depending on which fractions you include in your application. While they all ultimately produce a Java archive, they provide additional logic to make building certain types of archives easier.

The types of archives that WildFly Swarm makes available includes, but is not limited to:

Type Description

JARArchive

An enhanced version of .jar which provides easy methods for adding dependencies.

WARArchive

An enhanced version of a .war which provides easy methods for serving static content or adding dependencies.

JAXRSArchive

Web archive which can automatically create the necessary default Application/@ApplicationPath bindings.

Secured

Archive type which injects keycloak.json and sets up security constraints

RibbonArchive

Archive which can register Ribbon-based services or clients

Using Several Types of Archives

Each ShrinkWrap archive type is effectively a view onto the underlying archive. Using the archive’s own .as(class) method, the view can be changed several times while constructing it.

// First create and use generic JAX-RS archive
JAXRSArchive deployment = ShrinkWrap.create( JAXRSArchive.class );
deployment.addResource( MyResource.class );

// Next, view it as a Secured archive, which activated Keycloak
deployment.as( Secured.class )

// Then, view it as a RibbonArchive, and set its service name for discovery
deployment.as( RibbonArchive.class ).setApplicationName( "my-service" );