zslayton
zslayton

Reputation: 52785

Can DropWizard serve assets from outside the jar file?

In looking at the documentation, it appears that DropWizard is only able to serve static content living in src/main/resources. I'd like to keep my static files in a separate directory outside the jar file. Is that possible? Or do most people use nginx/Apache for their static content?

Upvotes: 15

Views: 13661

Answers (6)

Chathurika Sandarenu
Chathurika Sandarenu

Reputation: 1492

There is a upto-date dropwizard-configurable-assets-bundle maintained at official dropwizard-bundles. You can find it at github https://github.com/dropwizard-bundles/dropwizard-configurable-assets-bundle. Current version supports dropwizard 0.9.2

This can be used to serve static files from arbitrary file system path.

Upvotes: 3

user1146522
user1146522

Reputation: 61

To complement craddack's answer: Correct, you can use the regular AssetsBundle as long as you add the assets to your classpath. If you use gradle and oneJar, you can add a directory to the classpath in the oneJar task:

task oneJar(type: OneJar) {
  mainClass = '...'
  additionalDir = file('...')
  manifest {
    attributes 'Class-Path': '.. here goes the directory ..'
  }
}

see https://github.com/rholder/gradle-one-jar

Upvotes: 1

craddack
craddack

Reputation: 116

Working off of Marcello Nuccio's answer, it still took me the better part of my day to get it right, so here is what I did in a bit more detail.

Let's say I have this directory structure:

  • my-dropwizard-server.jar
  • staticdocs
    • assets
      • image.png

Then this is what you have to do to make it work:

1) In your dropwizard Application class, add a new AssetsBundle. If you want your assets to be served from a different URL, change the second parameter.

@Override
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/assets/", "/assets/"));       
}

2) Add the document root to your classpath by configuring the maven-jar-plugin like this. (Getting the "./staticdocs/" in the correct form took me a while. Classpaths are unforgiving.)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addClasspath>true</addClasspath>
      </manifest>
      <manifestEntries>
        <Class-Path>./staticdocs/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

3) This step is entirely optional. If you want to serve your Jersey REST Resources from a different root path (e.g. "app"), add the following to your configuration YML:

server:
  rootPath: /app/*

Now you can access your static content like this, for example:

localhost:8080/assets/image.png

Upvotes: 7

Marcello Nuccio
Marcello Nuccio

Reputation: 3901

The user manual says:

use an extended AssetsBundle constructor to serve resources in the assets folder from the root path.

i.e. the files are loaded as resources from the classpath. Then you only need to properly set the classpath for the service.

With the default configuration, this means that you need to call the document root assets, and put the parent folder of the document root in the classpath. Then, for example, assets/foo.html will be available at

http://localhost:8080/assets/foo.html

Upvotes: 4

LiorH
LiorH

Reputation: 18824

yes, it can, using this plugin - https://github.com/bazaarvoice/dropwizard-configurable-assets-bundle

Upvotes: 12

Gary
Gary

Reputation: 7257

The vast majority of websites that serve static content do so through a dedicated webserver, or, at larger scale, a CDN.

Occasionally, you might want to deploy an application as a self-contained unit complete with all assets which is where Dropwizard comes in.

It is possible to get Dropwizard to serve up assets from outside the classpath, but the easiest way to do this is to write your own asset endpoint that reads from an externally configured file path.

Upvotes: 2

Related Questions