Skip to content

LayingInYourFirstHammock

John Ament edited this page May 30, 2016 · 13 revisions

Referencing

Hammock is made available through Maven Central. You can add it to your project as a dependency:

<dependency>
    <groupId>ws.ament.hammock</groupId>
    <artifactId>web-undertow</artifactId>
    <version>0.0.3</version>
</dependency>
<dependency>
    <groupId>ws.ament.hammock</groupId>
    <artifactId>rest-resteasy</artifactId>
    <version>0.0.3</version>
</dependency>

This will give you a basic application structure that will include the necessary dependencies to run Hammock. If at this point, you just run the main class ws.ament.hammock.Bootstrap you'll get a running container. Now this won't do a whole lot, so let's add our first REST endpoint.

@Path("/hello")
@RequestScoped
public class HelloWorldEndpoint {
    @GET
    public Response doGet() {
        return Response.ok("Hello, world!").build();
    }
}

If you're not familiar with JAX-RS, it might be best to read through the RestEasy User Guide. This exposes a single REST endpoint available at /hello and returns back a simple Hello, world! response. If you now run your application and access http://localhost:8080/hello you'll get this out. Cool!

Packaging your app

I'll be honest, I could write a custom maven or gradle plugin to do packaging, but I'd prefer to leverage what's available already. One word of caution, there is a bug https://github.com/johnament/hammock/issues/4 open for some bad package structures. Will be fixed soon, I promise.

The best way to package your app, if you're using maven, is to leverage the shade plugin to build an uber-jar. The configuration is pretty straight forward:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>hammock</shadedClassifierName>
                            <!-- this section should come out when #4 is fixed -->
                            <artifactSet>
                                <excludes>
                                    <exclude>org.jboss.arquillian.core:*</exclude>
                                    <exclude>org.jboss.arquillian.container:*</exclude>
                                    <exclude>org.jboss.arquillian.junit:*</exclude>
                                    <exclude>org.jboss.arquillian.test:*</exclude>
                                    <exclude>org.jboss.shrinkwrap:*</exclude>
                                    <exclude>org.jboss.shrinkwrap.descriptors:*</exclude>
                                </excludes>
                            </artifactSet>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>ws.ament.hammock.Bootstrap</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

You'll now have an executable JAR file, just run java -jar target/yourapp-hammock.jar

Clone this wiki locally