-->

DropWizard可以从jar文件之外提供的资产?(Can DropWizard serve as

2019-07-18 00:21发布

在看文档 ,似乎DropWizard是只能提供静态内容活在的src / main /资源。 我想保持我的静态文件的jar文件之外的单独的目录。 那可能吗? 或者说大多数人使用的nginx / Apache的为他们的静态内容?

Answer 1:

是的,它可以使用这个插件- https://github.com/bazaarvoice/dropwizard-configurable-assets-bundle



Answer 2:

工作过马塞洛Nuccio的答案,但它仍然花了我我一天的大部分时间得到它的权利,所以这里是我的一些详细信息一样。

比方说,我有这样的目录结构:

  • 我-dropwizard-的server.jar
  • staticdocs
    • 资产
      • image.png

那么这是你必须做的,使其工作内容:

1)在你的dropwizard应用程序类,添加一个新的AssetsBundle。 如果你希望你的资产从不同的URL提供服务,改变第二个参数。

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

2)通过配置的maven-JAR-插件这样的文档根目录添加到您的类路径。 (中获取正确的形式“./staticdocs/”我花了一段时间。类路径是无情的。)

<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)本步骤是完全可选的。 如果你想从不同的根路径为你的球衣REST资源(例如,“应用程序”),以下内容添加到您的配置阳明:

server:
  rootPath: /app/*

现在,您可以访问您这样的静态内容,例如:

localhost:8080/assets/image.png


Answer 3:

该用户手册说:

使用扩展AssetsBundle构造服务资源资产从文件夹的根路径。

即文件被加载从类路径资源。 然后,你只需要正确设置该服务的类路径。

在默认配置下,这意味着你需要调用文档根assets ,并把文档根目录的父文件夹的类路径中。 然后,例如, assets/foo.html将可在

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


Answer 4:

有一个高达最新dropwizard-configurable-assets-bundle保持在官方dropwizard-束。 您可以在github上找到它https://github.com/dropwizard-bundles/dropwizard-configurable-assets-bundle 。 当前版本支持dropwizard 0.9.2

这可以用来从任意文件系统路径提供静态文件。



Answer 5:

绝大多数网站提供静态内容通过一个专用的网络服务器这样做,或者说,规模较大, CDN 。

有时,你可能希望将应用程序部署为完成这也正是Dropwizard进来所有资产独立单元。

它有可能获得Dropwizard从classpath中之外提供了资产,但要做到这一点最简单的方法是写自己的资产端点,从外部配置文件路径读取。



Answer 6:

为了补充craddack的答案:正确,你可以为你添加的资产类路径,只要使用普通的AssetsBundle。 如果您使用的gradle产出和oneJar,你可以添加一个目录中oneJar任务的类路径:

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

看到https://github.com/rholder/gradle-one-jar



文章来源: Can DropWizard serve assets from outside the jar file?