【问题标题】:Creating an aspectJ library using sbt-aspect使用 sbt-aspect 创建 aspectJ 库
【发布时间】:2019-12-29 15:34:02
【问题描述】:

我正在编写一个用于监控/OpenTracing 的库,并且我正在尝试使用 sbt-aspectj,以便该库的用户无需手动检测他们的代码。但是,我目前在创建代表此类库的 sbt 项目时遇到了问题。

我的想法是我想要一个外部库,如此示例中所示https://github.com/sbt/sbt-aspectj/tree/master/src/sbt-test/weave/external 但是该外部库依赖于外部依赖项(即akka-actors)。基本上我正在尝试将https://github.com/sbt/sbt-aspectj/tree/master/src/sbt-test/weave/externalhttps://github.com/sbt/sbt-aspectj/tree/master/src/sbt-test/weave/jar 结合起来。我在这里创建了一个示例项目https://github.com/mdedetrich/sbt-aspectj-issue 来说明我遇到的问题,但是下面是相关示例

lazy val root = (project in file("."))
  .enablePlugins(SbtAspectj)
  .settings(
    name := RootName,
    version := Version,
    // add akka-actor as an aspectj input (find it in the update report)
//    aspectjInputs in Aspectj ++= update.value.matching(
//      moduleFilter(organization = "com.typesafe.akka", name = "akka-actor*")),
    // replace the original akka-actor jar with the instrumented classes in runtime
//    fullClasspath in Runtime := aspectjUseInstrumentedClasses(Runtime).value,
    // only compile the aspects (no weaving)
    aspectjCompileOnly in Aspectj := true,
    // ignore warnings (we don't have the target classes at this point)
    aspectjLintProperties in Aspectj += "invalidAbsoluteTypeName = ignore",
    // replace regular products with compiled aspects
    products in Compile ++= (products in Aspectj).value,
    libraryDependencies ++= Seq(
      "com.typesafe.akka" %% "akka-actor" % akkaVersion
    )
  )

lazy val test = (project in file("test"))
  .enablePlugins(SbtAspectj)
  .settings(
    aspectjBinaries in Aspectj ++= update.value.matching(
      moduleFilter(organization = Organization, name = s"$RootName*")),
    aspectjInputs in Aspectj ++= update.value.matching(
      moduleFilter(organization = "com.typesafe.akka", name = "akka-actor*")),
    fullClasspath in Runtime := aspectjUseInstrumentedClasses(Runtime).value,
    // weave this project's classes
    aspectjInputs in Aspectj += (aspectjCompiledClasses in Aspectj).value,
    products in Compile := (products in Aspectj).value,
    products in Runtime := (products in Compile).value,
    libraryDependencies ++= Seq(
      Organization %% RootName % Version
    )
  )

我们的想法是我们使用root/publishLocal 发布root 项目,而测试项目只是设计为包含root 作为libraryDependency,因此我们可以查看aspect-j 是否正常工作。

问题很简单,我无法让它工作。 https://github.com/mdedetrich/sbt-aspectj-issue 的当前代码与 root/publishLocal 一起发布(虽然不确定它是否正确)但是当我这样做时 test/run 我明白了

[info] Weaving 2 inputs with 1 AspectJ binary to /home/mdedetrich/github/sbt-aspectj-issue/test/target/scala-2.13/aspectj/classes...
[error] stack trace is suppressed; run last test / Compile / packageBin for the full output
[error] (test / Compile / packageBin) java.util.zip.ZipException: duplicate entry: META-INF/MANIFEST.MF
[error] Total time: 1 s, completed Dec 29, 2019 4:31:27 PM
sbt:sbt-aspectj-issue> 

这似乎是重复 akka-actor 条目的问题。我尝试切换 build.sbt 中的各种条目,但没有成功。

编辑:这也作为 github 问题发布在这里 https://github.com/sbt/sbt-aspectj/issues/44

【问题讨论】:

    标签: scala sbt aspectj aspect sbt-aspectj


    【解决方案1】:

    通常,您可以从编织的外部库中排除META-INF 目录。

    mappings in (Compile, packageBin) := {
        (mappings in (Compile, packageBin)).value
            .filterNot(_._2.startsWith("META-INF/"))
    }
    

    但是对于 akka 库,还有另一个问题。在每个 akka 库中,都有一个 reference.conf,其中包含所提供功能的后备配置。这也会导致像META-INF 那样的冲突。但它不能像META-INF 那样被排除在外,因为它们对于akka 正常工作是必不可少的。

    如果您排除它们,则必须在您的 application.conf 中提供所有必需的 akka 配置,或者在您的项目中提供合并(而不是简单地连接)reference.conf。这不是微不足道的,并且受akka版本变化的影响。

    另一种解决方案是单独编织和重新打包 akka 库,因此 reference.conf 可以保留在重新打包的库中。项目布局和构建脚本会稍微复杂一些,但如果您计划将来升级到更新版本的 akka,也更容易维护。

    【讨论】:

    • 这听起来像是一种解决方法,而不是原始问题的解决方案。 Another solution would be weaving and repackaging the akka libraries individually, so the reference.conf can be kept in the repackaged libraries. The project layout and build script will a bit more complicated, but also be easier to maintain if you have plan to upgrade to newer versions of akka in the future.你能举个例子吗?你的意思是你只有一个只包含 AspectJ 类的项目吗?我也很好奇像 kamon(也使用 AspectJ weaving)这样的项目是如何解决这个问题的
    猜你喜欢
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2018-02-09
    相关资源
    最近更新 更多