如何通过SBT项目依赖项将sbt-web输出与xsbt-web-plugin一起使用? [英] How can sbt-web output be used with xsbt-web-plugin via a SBT project dependency?

查看:110
本文介绍了如何通过SBT项目依赖项将sbt-web输出与xsbt-web-plugin一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不带play框架的sbt-web插件,而是使用xsbt-web-plugin构建一个webapp.

我已使sbt-web插件在处理资产管道中正常工作,并使其创建了有效的webjar输出(通过packageBin)以及标准的"web/public/main"输出(通过资产).

另外,我一直在使用xsbt-web-plugin开发一个webapp,并从SBT(通过container:start)中为该webapp提供服务.该webapp项目可以使用来自mavenCentral的webjar依赖项,并且可以毫无问题地引用这些资源.

我一直无法弄清楚的是如何使xsbt-web-plugin包含来自Web应用程序中sbt-web管道的资产.看来,我能做的最好的就是将它们放到CLASSPATH中. (据我了解,这是所有需要满足的需求,因为它们具有资产控制器",可以为CLASSPATH中的这些资产提供服务,因此不需要将它们作为静态资产提供给Web应用程序.)

我已经建立了一个公共GitHub存储库( https://github.com/MartinSnyder/服务-sbt-web-output )来演示我正在尝试做的事情.

我的plugins.sbt是:

resolvers += Resolver.typesafeRepo("releases")

addSbtPlugin("com.typesafe.sbt" % "sbt-web" % "1.1.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "1.0.0")

我的build.sbt是:

name := "Example project serving sbt-web output from within SBT"

organization in ThisBuild := "com.martinsnyder"

version in ThisBuild := "0.0.1"

scalaVersion in ThisBuild := "2.11.6"

lazy val example_webjar =
  project
    .in(file("example_webjar"))
    .settings(libraryDependencies ++= Seq("org.webjars" % "requirejs" % "2.1.16"))
    .enablePlugins(SbtWeb)

lazy val example_webapp =
  project
    .in(file("example_webapp"))
    .dependsOn(example_webjar)
    .settings(libraryDependencies ++= Seq(
      "javax.servlet" % "servlet-api" % "2.5" % "provided",
      "org.eclipse.jetty" % "jetty-webapp" % "9.3.0.M2" % "container",
      "org.eclipse.jetty" % "jetty-plus"   % "9.3.0.M2" % "container",
      "commons-logging" % "commons-logging" % "1.2" % "container"
    ))
    .enablePlugins(SbtWeb)
    .settings(jetty(): _*)

Web应用程序中的HTML文件为:

<html>
    <head>
        <title>Example</title>
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <link rel="stylesheet" type="text/css" href="lib/example_webjar/css/main.css">
        <link rel="stylesheet" type="text/css" href="webjar/example_webjar/0.0.1/css/main.css">
        <script src="webjars/requirejs/2.1.16/require.js"></script>
    </head>
    <body>
        <div class="red">Red</div>
        <div class="green">Green</div>
    </body>
</html>

就目前的情况而言,requirejs来自预构建的webjar,因此可以成功为其提供服务.这三个标记完全不同,并且都无法引用sbt-web的资产输出.

我要实现的最佳方案是获取xsbt-web-plugin webapp输出(target/webapp/中)中包含的sbt-web插件输出(target/web/public/main/) ).我希望xsbt-web-plugin能够以webjar的形式访问项目依赖项.

解决方案

如果您

I am trying to use the sbt-web plugin without the play framework and instead build a webapp using the xsbt-web-plugin.

I have gotten the sbt-web plugin working correctly in handling the asset pipeline and have it creating a valid webjar output (via packageBin) as well as the standard "web/public/main" output (via assets).

Separately, I have been using the xsbt-web-plugin to develop a webapp and serve that webapp from within SBT (via container:start). The webapp project can consume webjar dependencies from mavenCentral and refer to those resources without issue.

What I have not been able to figure out is how can I get the xsbt-web-plugin to include assets coming out of the sbt-web pipeline in the web application. It seems that the best I can do is to get them into the CLASSPATH. (From what I understand, this is all that play needs, because they have an "Asset Controller" that serves those assets out of the CLASSPATH and therefore doesn't need them to be available as static assets to the web application).

I have made a public GitHub repository (https://github.com/MartinSnyder/serving-sbt-web-output) that demonstrates what I am trying to do.

My plugins.sbt is:

resolvers += Resolver.typesafeRepo("releases")

addSbtPlugin("com.typesafe.sbt" % "sbt-web" % "1.1.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "1.0.0")

My build.sbt is:

name := "Example project serving sbt-web output from within SBT"

organization in ThisBuild := "com.martinsnyder"

version in ThisBuild := "0.0.1"

scalaVersion in ThisBuild := "2.11.6"

lazy val example_webjar =
  project
    .in(file("example_webjar"))
    .settings(libraryDependencies ++= Seq("org.webjars" % "requirejs" % "2.1.16"))
    .enablePlugins(SbtWeb)

lazy val example_webapp =
  project
    .in(file("example_webapp"))
    .dependsOn(example_webjar)
    .settings(libraryDependencies ++= Seq(
      "javax.servlet" % "servlet-api" % "2.5" % "provided",
      "org.eclipse.jetty" % "jetty-webapp" % "9.3.0.M2" % "container",
      "org.eclipse.jetty" % "jetty-plus"   % "9.3.0.M2" % "container",
      "commons-logging" % "commons-logging" % "1.2" % "container"
    ))
    .enablePlugins(SbtWeb)
    .settings(jetty(): _*)

The HTML file in the webapp is:

<html>
    <head>
        <title>Example</title>
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <link rel="stylesheet" type="text/css" href="lib/example_webjar/css/main.css">
        <link rel="stylesheet" type="text/css" href="webjar/example_webjar/0.0.1/css/main.css">
        <script src="webjars/requirejs/2.1.16/require.js"></script>
    </head>
    <body>
        <div class="red">Red</div>
        <div class="green">Green</div>
    </body>
</html>

As things currently stand, requirejs is served successfully, as it is coming from a pre-build webjar. The three tags are all different and failing attempts to reference the asset output from sbt-web.

The best-case scenario I am trying to achieve is to get the sbt-web plugin output(target/web/public/main/) included in the xsbt-web-plugin webapp output (target/webapp/). I would settle for the xsbt-web-plugin being able to access the project dependency as a webjar.

解决方案

If you tell xsbt-web-plugin to use sbt-web's output as its Web application resources directory, it should get you most of the way.

Just add webappSrc in webapp <<= WebKeys.assets in Assets as a setting to your example_webapp project:

lazy val example_webapp =
  project
    .in(file("example_webapp"))
    .dependsOn(example_webjar)
    .settings(libraryDependencies ++= Seq(
      "javax.servlet" % "servlet-api" % "2.5" % "provided",
      "org.eclipse.jetty" % "jetty-webapp" % "9.3.0.M2" % "container",
      "org.eclipse.jetty" % "jetty-plus"   % "9.3.0.M2" % "container",
      "commons-logging" % "commons-logging" % "1.2" % "container"
    ))
    .enablePlugins(SbtWeb)
    .settings(jetty(): _*)
    .settings(webappSrc in webapp <<= WebKeys.assets in Assets)

这篇关于如何通过SBT项目依赖项将sbt-web输出与xsbt-web-plugin一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆