【问题标题】:How can one change the location of the "webapp" directory for a Scalatra application?如何更改 Scalatra 应用程序的“webapp”目录的位置?
【发布时间】:2013-05-07 02:20:03
【问题描述】:

默认情况下,Scalatra 期望“webapp”目录位于src/main/webapp。如何将其更改为例如content/doc-root

sbt 允许使用类似以下的方式自定义其默认目录:

scalaSource <<= (baseDirectory)(_ / "src")

所以我认为这只是知道要使用的正确“配置密钥”的问题......

【问题讨论】:

    标签: scala sbt scalatra scalatra-sbt


    【解决方案1】:

    @Kelsey Gilmore-Innis 有正确的答案,但既然它不被接受,我们就打破它,打破它,打破它。

    首先,我假设您正在关注 Getting started guide 以使用 g8 安装 Scalatra。希望和我刚得到的版本相同。

    g8 scalatra/scalatra-sbt

    g8 模板所做的是设置使用 scalatra-sbt 0.3.2 插件的 sbt 0.13 构建:

    addSbtPlugin("org.scalatra.sbt" % "scalatra-sbt" % "0.3.2")
    

    这个插件内部使用JamesEarlDouglas/xsbt-web-plugin0.4.0 做webapp相关的设置。

    xsbt-web-plugin 0.4.0

    这就是为什么 xsbt-web-plugin 变得相关的原因,即使您只是想更改 Scalatra 的设置。您需要重新接线的设置称为webappResources in Compile。它是如何工作的?

    重新连接 webappResources

    要重新连接设置,请打开 project/build.scala。添加

    import com.earldouglas.xsbtwebplugin.PluginKeys.webappResources
    

    导入子句。然后更改设置如下:

      lazy val project = Project (
        "foo",
        file("."),
        settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
          organization := Organization,
          name := Name,
          version := Version,
          scalaVersion := ScalaVersion,
          resolvers += Classpaths.typesafeReleases,
          webappResources in Compile := Seq(baseDirectory.value / "content" / "doc-root"),
          ...
        )
      )
    

    现在将 src/main/webapp 移动到 content/doc-root,重新加载 sbt,就可以了。

    【讨论】:

      【解决方案2】:

      资源文件夹是 Jetty 属性。如果您正在运行嵌入式 Jetty,则指定为 here。您可以手动编辑它或通过设置 PUBLIC 环境变量来覆盖它。

      您也可以在您的 SBT 构建文件中覆盖它。它使用 xsbt-web-plugin 运行,你 can override that plugin's settings.

      【讨论】:

        【解决方案3】:

        对于较新版本的 xsbt-web-plugin(写作时为 1.0.0),更改源路径的方式不同。

        首先将所有相应设置移至XwpPlugin.webappSettings。而你需要这两个

        webappSrc in webapp <<= (baseDirectory in Compile) map { _ / "content" / "doc-root" },
        webappDest in webapp <<= (baseDirectory in Compile) map { _ / "content" / "doc-root" },
        

        【讨论】:

          【解决方案4】:

          如果您不想更改 sbt 设置,您也可以通过重写 serveStaticResource 并使用 forward 以编程方式完成

          override protected def serveStaticResource(): Option[Any] = {
            // check to see if we need to alter the path to find the TRUE disk url
            val incUrl = request.getRequestURI
          
            if(incUrl.startsWith("/otherDir")) {
              servletContext.resource(request) map { _ =>
                servletContext.getNamedDispatcher("default").forward(request, response)
              }
            } else {
               val trueUrl = "/otherdir" + incUrl
               Option(servletContext.getRequestDispatcher(trueUrl).forward(request, response))
            }
          }
          

          免责声明:您还应该检查它没有进入无限循环。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-11-23
            • 2013-12-11
            • 2019-11-10
            • 1970-01-01
            • 1970-01-01
            • 2012-10-24
            相关资源
            最近更新 更多