微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 SBT 一次为多个项目生成 Scaladoc

如何解决使用 SBT 一次为多个项目生成 Scaladoc

我有一个多项目构建,从 packageDoc 的角度来看,所有项目的源都包含在 core 子项目中。

core.settings(
    Compile / doc / sources ++= (common / Compile / doc / sources).value,Compile / doc / sources ++= (macros / Compile / doc / sources).value,Compile / doc / sources ++= (premacros / Compile / doc / sources).value,)

该合并是 scala-doc 进程正常工作所必需的解决方法;鉴于它不支持多项目。

仅使用该解决方法,当 packageDoc 项目的宏实现调用 macros 项目的宏函数时,premacros 任务会抛出以下消息:

[错误]:未找到宏实现: [错误](最常见的原因是你不能在定义它们的同一个编译运行中使用宏实现) [错误] 同时为多个项目生成 scaladoc 时,请考虑使用 -Ymacro-no-expand 完全禁用宏扩展。

该消息告诉我宏实现及其用法应该在不同的项目中。因此,它们是从 compile 任务的角度来看的,但不适用于 packageDoc 任务,这要归功于我上面为使 scala-doc 进程正常工作所做的解决方法

错误消息还建议我使用“-Ymacro-no-expand”来解决由第一个解决方法引起的问题。所以我添加了这一行:

core / Compile /  doc / scalacoptions += "-Ymacro-no-expand"

但随后 packageDoc 任务抛出的错误变为:

[错误] 错误选项:'-Ymacro-no-expand'

我做错了什么?

这是整个“build.sbt”文件

ThisBuild / scalaVersion := "2.13.4"
ThisBuild / autoAPIMappings := true

//// Inter subproject dependency and packaging settings

lazy val root = (project in file("."))
    .aggregate(core,macros,premacros,common)
    .settings(
        publish / skip := true,)

lazy val core = (project in file("core"))
    .dependsOn(
        macros,common % "compile-internal,test-internal" // the "compile-internal" removes `common` from the set of dependencies for publishing because its content is provided by the artifact of this package. See the mappings below. Also see "https://www.scala-sbt.org/1.x/docs/Macro-Projects.html"
    ) 
    .settings(
        // append the content of the common subproject's binary package to the core's binary package. This library users don't require the `core` and `common` binary artifacts be separated. This setting merges them.
        Compile / packageBin / mappings ++= (common / Compile / packageBin / mappings).value,// append the content of the common subproject's source package to the core's source package. This library users don't require the `core` and `common` source artifacts be separated. This setting merges them.
        Compile / packageSrc / mappings ++= (common / Compile / packageSrc / mappings).value,// append the content of the common subproject's javadoc package to the core javadoc package. This is convenient and also necessary for the doc links between projects to work.
        Compile / doc / sources ++= (common / Compile / doc / sources).value,// append the content of the macros subproject's javadoc package to the core javadoc package. This is convenient and also necessary for the doc links between projects to work.
        Compile / doc / sources ++= (macros / Compile / doc / sources).value,// append the content of the pre-macros subproject's javadoc package to the core javadoc package. This is convenient and also necessary for the doc links between projects to work.
        Compile / doc / sources ++= (premacros / Compile / doc / sources).value,)

lazy val macros = (project in file("macros"))
    .dependsOn(
        premacros % "compile-internal,test-internal",// the "compile-internal" removes `premacros` from the set of dependencies for publishing because its content is provided by the artifact of this subproject. See the mappings below. Also see "https://www.scala-sbt.org/1.x/docs/Macro-Projects.html"
        common % "compile-internal,// the "compile-internal" removes `common` from the set of dependencies for publishing because its content is provided by the core artifact. See the mappings of the `core` subproject.
    )
    .settings(
        // append the content of the pre-macros subproject's binary package to the macros binary package. This library users don't require the `macro` and `premacro` binary artifacts be separated. This setting merges them.
        Compile / packageBin / mappings ++= (premacros / Compile / packageBin / mappings).value,// append the content of the pre-macros subrpoject's source package to the core source package. This library users don't require the `macro` and `premacro` source artifacts be separated. This setting merges them.
        Compile / packageSrc / mappings ++= (premacros / Compile / packageSrc / mappings).value,// do not publish the javadoc artifact of this project. It's content is appended to the core project's javadoc package.
        Compile / packageDoc / publishArtifact := false,)

// This subproject contains macros used by the macros contained in the "macros" subproject
lazy val premacros = (project in file("premacros"))
    .settings(
        publish / skip := true,// don't publish any artifact of this subproject because all of them are provided by other subprojects: the binaries and source by `macro`,and the java-doc by `core`.
    )

lazy val common = (project in file("common"))
    .settings(
        publish / skip := true,// don't publish any artifact of this subproject because all of them are provided by the `core` subproject.
    )

//// Library dependencies ////

premacros / libraryDependencies ++= Seq(
    // scala reflection required for macros
    "org.scala-lang" % "scala-reflect" % scalaVersion.value,)
macros / libraryDependencies ++= Seq(
    // scala reflection required for macros
    "org.scala-lang" % "scala-reflect" % scalaVersion.value,)

//// scalac options ////

core / scalacoptions += "-language:experimental.macros"
core / Compile /  doc / scalacoptions += "-Ymacro-no-expand"
premacros / scalacoptions += "-language:experimental.macros"
macros / scalacoptions += "-language:experimental.macros"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。