【问题标题】:Scala build.sbt: Memcached Plugin for Play framework 2.8.6Scala build.sbt:Memcached Plugin for Play 框架 2.8.6
【发布时间】:2021-03-26 13:50:39
【问题描述】:

我正在尝试将mumoshu's Memcached Plugin for Play framework 升级到Play 2.8.6,在build.sbt

升级说明为:

对于 Play 2.6.x 及更高版本:!!!将 play.modules.cache.* 配置键更改为 play.cache.* !!!

val appDependencies = Seq(
  play.PlayImport.cacheApi,
  "com.github.mumoshu" %% "play2-memcached-play26" % "0.9.2"
)
val main = Project(appName).enablePlugins(play.PlayScala).settings(
  version := appVersion,
  libraryDependencies ++= appDependencies,
  resolvers += "Spy Repository" at "http://files.couchbase.com/maven2" // required to resolve `spymemcached`, the plugin's dependency.
)

在升级之前,我有:

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,  
  "com.github.mumoshu" %% "play2-memcached-play24" % "0.7.0",
  //...
}

(不成功)升级后:

  • cache 依赖没有解决,所以我删除了它。

更新后的build.sbt 现在看起来像这样:

name := "CH07"

version := "1.0"

lazy val `ch07` = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.13.4"

resolvers += "Spy Repository" at "http://files.couchbase.com/maven2"

val appDependencies = Seq(
  play.sbt.PlayImport.cache,
  "com.github.mumoshu" %% "play2-memcached-play28" % "0.11.0"
  //"com.github.mumoshu" %% "play2-memcached-play26" % "0.9.2"
)
val main = (project in file(".")).enablePlugins(play.sbt.PlayScala).settings(
  version := version.value,
  libraryDependencies ++= appDependencies,
  resolvers += "Spy Repository" at "http://files.couchbase.com/maven2" // required to resolve `spymemcached`, the plugin's dependency.
)

libraryDependencies ++= Seq(
  jdbc,
  //cache,
  ws,

  "org.hsqldb" % "hsqldb" % "2.5.0",
  "org.jooq" % "jooq" % "3.14.4",
  "org.jooq" % "jooq-codegen-maven" % "3.14.4",
  "org.jooq" % "jooq-meta" % "3.14.4",
  "joda-time" % "joda-time" % "2.7",
  "com.github.ironfish" %% "akka-persistence-mongo-casbah"  % "0.7.6",
  "com.ning" % "async-http-client" % "1.9.29"
)

routesGenerator := InjectedRoutesGenerator

我遇到的错误:

* build.sbt:
error: value cache is not a member of object play.sbt.PlayImport

* build.sbt:
"com.github.mumoshu" %% "play2-memcached-play28" % "0.11.0"
Intellij says: unresolved artifact. Not resolved or indexed.

* in project/plugins.sbt:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.6")
Intellij says: unresolved artifact. Not resolved or indexed.

您能看到我在build.sbt 中犯的任何错误吗?

换句话说,我正在为使用 Play 2.8.6 和 Memcached Plugin for Play 框架 0.9.2 的项目寻找一个有效的 build.sbt(以及其他必需的定义)示例。 (或后者的最高版本)。

更新:

这是我当前的编译配置:

build.sbt

name := "CH07"

version := "1.0"

lazy val `ch07` = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.13.4"

resolvers += "Spy Repository" at "http://files.couchbase.com/maven2"

libraryDependencies ++= Seq(
  jdbc,
  cacheApi,
  ws,
  "com.github.mumoshu" %% "play2-memcached-play28" % "0.11.0",
  "org.hsqldb" % "hsqldb" % "2.5.0",
  "org.jooq" % "jooq" % "3.14.4",
  "org.jooq" % "jooq-codegen-maven" % "3.14.4",
  "org.jooq" % "jooq-meta" % "3.14.4",
  "joda-time" % "joda-time" % "2.7",
  "com.ning" % "async-http-client" % "1.9.29",
  "com.github.scullxbones" %% "akka-persistence-mongo-common" % "3.0.5",
  "com.typesafe.akka" %% "akka-persistence" % "2.6.10",
  "com.typesafe.akka" %% "akka-persistence-query" % "2.6.10",
  "com.typesafe.akka" %% "akka-persistence-typed" % "2.6.10",
  "com.typesafe.play" %% "play-guice" % "2.8.7"
)

routesGenerator := InjectedRoutesGenerator

project/build.properties

sbt.version=1.3.10

project/plugins.sbt

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.7")

【问题讨论】:

    标签: scala intellij-idea playframework sbt memcached


    【解决方案1】:

    这个怎么样?

    name := """pla-memcache-example"""
    organization := "com.example"
    
    version := "1.0-SNAPSHOT"
    
    scalaVersion := "2.13.3"
    
    val appDependencies = Seq(
        guice,
        play.PlayImport.cacheApi,
        "com.github.mumoshu" %% "play2-memcached-play28" % "0.11.0",
        "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
    )
    
    lazy val root = (project in file(".")).enablePlugins(PlayScala).settings(
        version := appVersion,
        libraryDependencies ++= appDependencies,
        resolvers += "Spy Repository" at "http://files.couchbase.com/maven2" // required to resolve `spymemcached`, the plugin's dependency.
      )
    

    plugins.sbt

    sbt.version=1.3.13
    

    plugins.sbt

    addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.7")
    

    在这种情况下,首先找到giter8模板并使用“sbt new”命令。 接下来进入插件官方页面查看库名,然后使用Maven搜索找到你要使用的版本。 最好阅读插件的整个自述文件而不跳过它。

    您也可以尝试使用命令而不是使用 IntelliJ 来运行 sbt。
    如果还是不能解决问题,可以尝试清除 .ivy2 目录下的缓存。

    参考链接:
    https://github.com/mumoshu/play2-memcached
    https://github.com/playframework/play-scala-seed.g8
    https://search.maven.org/artifact/com.github.mumoshu/play2-memcached-play28_2.13/0.11.0/jar

    【讨论】:

    • 你能提供完整的 build.sbt、plugins.sbt、build.properties 吗?
    • 我已经添加了很多信息,所以请看一下。
    • * giter8 是什么,为什么我突然需要它? * 为什么我需要指定guice(它在以前的build.sbt版本中被用作依赖,我不必显式指定它)? * 我只是在我的build.sbt + memcached jar 版本中指定了cacheApi,它似乎编译了memcached 代码,但是cacheApi 的路径似乎是play.sbt.cacheApi...那我为什么需要@987654331 @ 它有什么作用? * 为什么我不能有一个简单的build.sbt 结构:scala 版本、所需的 repos、libraryDependencies,仅此而已?
    • 为了找出问题,更容易建立一个简单的环境,确保它可以工作,然后比较可以工作的环境和不工作的环境来得到答案。由于我想消除不确定性以达到验证的目的,所以我只将插件自述文件中描述的内容添加到模板生成的项目中,并没有更改任何其他部分。 giter8 上有许多可用的 scala 项目模板。在这里,我正在寻找一个简单的游戏项目并使用它。使用 Play2.8 时必须使用 Guice。
    • 那么您知道 Play 2.8 文档中的哪个位置提到有必要明确包含 Guice?
    【解决方案2】:

    您的问题是在尝试导入 play.sbt.PlayImport.cache 时。根据Cache APIs Migrationbyplay:

    新包

    现在 `cache` 已被拆分为仅包含 API 的 `cacheApi` 组件和包含 Ehcache 实现的 `ehcache`。如果您使用默认的 Ehcache 实现,只需在 `build.sbt` 中​​将 `cache` 更改为 `ehcache`:
    libraryDependencies ++= Seq(
      ehcache
    )
    

    如果您正在定义自定义缓存 API,或者正在编写缓存实现模块,您可以只依赖 API:

    libraryDependencies ++= Seq(
      cacheApi
    )
    

    删除的 API

    已删除已弃用的 Java 类 play.cache.Cache,您现在必须注入 play.cache.SyncCacheApi 或 play.cache.AsyncCacheApi。

    因此,完整的build.sbt 将是:

    name := "CH07"
    
    version := "1.0"
    
    lazy val `ch07` = (project in file(".")).enablePlugins(PlayScala)
    
    scalaVersion := "2.13.4"
    
    resolvers += "Spy Repository" at "https://files.couchbase.com/maven2"
    
    val appDependencies = Seq(
      play.sbt.PlayImport.ehcache,
      play.sbt.PlayImport.cacheApi,
      "com.github.mumoshu" %% "play2-memcached-play28" % "0.11.0"
      //"com.github.mumoshu" %% "play2-memcached-play26" % "0.9.2"
    )
    val main = (project in file(".")).enablePlugins(play.sbt.PlayScala).settings(
      version := version.value,
      libraryDependencies ++= appDependencies,
      resolvers += "Spy Repository" at "https://files.couchbase.com/maven2" // required to resolve `spymemcached`, the plugin's dependency.
    )
    
    libraryDependencies ++= Seq(
      jdbc,
      //cache,
      ws,
    
      "org.hsqldb" % "hsqldb" % "2.5.0",
      "org.jooq" % "jooq" % "3.14.4",
      "org.jooq" % "jooq-codegen-maven" % "3.14.4",
      "org.jooq" % "jooq-meta" % "3.14.4",
      "joda-time" % "joda-time" % "2.7",
      "com.github.scullxbones" %% "akka-persistence-mongo-common" % "3.0.5",
      "com.ning" % "async-http-client" % "1.9.29"
    )
    
    routesGenerator := InjectedRoutesGenerator
    

    build.properties:

    sbt.version=1.4.4
    

    plugins.sbt:

    logLevel := Level.Warn
    
    resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
    
    addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.7")
    

    【讨论】:

    • 有些事情没有意义。 * 如果我使用 cacheApi 的 memcached 实现,为什么我需要 ehcache 实现? * 我的代码编译时没有指定Typesafe repository,那为什么我需要指定呢? * 我不明白val main = .. 有什么用处。我们以后不使用mainmain 是 sbt 中的关键字吗? * 我可以通过在libraryDependencies ++= Seq(...) 中指定依赖项来编译项目,那么为什么我需要appDependenciesmain 呢? * 为什么我需要在(project in file(".")) 上重复两次?而这段代码是什么意思?
    • @rapt,我不熟悉memcached,所以我不确定你为什么需要memcachedcacheApi。我在这个答案中的总体意图是提供最接近您尝试生成的build.sbt,但确实可以编译。我在plugins.sbt 中指定了解析器,因为它是从游戏中自动生成的。所以我只是没有删除它。如果需要,您可以将其删除。 main 在此上下文中不是保留字。我们在这里使用它是为了启用游戏。否则很难加载插件。
    • 您绝对可以将appDependencieslibraryDependencies 合并。如前所述,我试图将其与您的结构保持一致。 (project in file(".")) 表示位于顶层的项目。你不需要两次。您可以选择其中之一。只要确保你启用了播放插件
    • 你的意思是“为什么你需要 memcached 和 ehcache”?
    • @rapt 我希望这能让事情更清楚。如果需要,我可以简化 build.sbt 并且不遵守您的约定。
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 2019-04-11
    • 2016-08-17
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2012-02-20
    相关资源
    最近更新 更多