gpt4 book ai didi

scala - 解决SBT中Scala Macros和Compiler Framework的依赖

转载 作者:行者123 更新时间:2023-12-02 04:58:55 24 4
gpt4 key购买 nike

我正在尝试编写一个框架来简化编写 Scala 编译器插件的过程,我正在做的是在 Scala 准引用之上编写一个框架。所以我的项目依赖于来自 macro-paradise 的宏以及 scala-compiler 和 scala-reflect 库。

我按照此处提到的说明编写了一个 SBT 构建脚本:https://github.com/scalamacros/sbt-example-paradise/blob/master/project/Build.scala

并使用 scalaVersion 2.11.0-SNAPSHOT、2.10.3-SNAPSHOT、2.10.3-RC1、2.10.2 来编译我的项目,但它们都不起作用。这是我的 sbt 构建脚本:

import sbt._
import Keys._

object LombrelloBuildSettings {
val sversion = "2.10.3-SNAPSHOT"
val buildSettings = Defaults.defaultSettings ++ Seq(
name := "lombrello",
organization := "ch.usi.inf.l3",
version := "0.1-SNAPSHOT",
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
scalaVersion := sversion,
scalaOrganization := "org.scala-lang.macro-paradise",
resolvers += Resolver.sonatypeRepo("snapshots"),
licenses := ("BSD 3-Clause", new java.net.URL("http://opensource.org/licenses/BSD-3-Clause")) :: Nil,
libraryDependencies ++= Seq("org.scala-lang.macro-paradise" % "scala-reflect" % sversion,
"org.scala-lang" % "scala-compiler" % sversion),
addCompilerPlugin("org.scala-lang.plugins" % "macro-paradise" % "2.0.0-SNAPSHOT" cross CrossVersion.full))
}

object LombrelloBuild extends Build {
import LombrelloBuildSettings._

lazy val root: Project = Project(
"root",
file("."),
settings = buildSettings ++ Seq(
run <<= run in Compile in tests
)
) aggregate (main, tests)

lazy val main: Project = Project(
"main",
file("src/main"),
settings = buildSettings

)

lazy val tests: Project = Project(
"tests",
file("src/test"),
settings = buildSettings ++ Seq(name := "tests")) dependsOn (main)
}

使用 scalaVersion 2.10-3-RC1,我得到以下错误:

[warn]  :::::::::::::::::::::::::::::::::::::::::::::: 
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.scala-lang.macro-paradise#scala-library;2.10.3-RC1: not found
[warn] :: org.scala-lang.macro-paradise#scala-reflect;2.10.3-RC1: not found
[warn] :: org.scala-lang.macro-paradise#scala-compiler;2.10.3-RC1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: org.scala-lang.macro-paradise#scala-library;2.10.3-RC1: not found
unresolved dependency: org.scala-lang.macro-paradise#scala-reflect;2.10.3-RC1: not found
unresolved dependency: org.scala-lang.macro-paradise#scala-compiler;2.10.3-RC1: not found
at sbt.IvyActions$.sbt$IvyActions$$resolve(IvyActions.scala:213)
at sbt.IvyActions$$anonfun$update$1.apply(IvyActions.scala:122)

使用 scalaVersion 2.11.0-SNAPSHOT,出现以下错误:

 java.lang.NoClassDefFoundError: scala/tools/nsc/typechecker/TypersTracking$class
at org.scalalang.macroparadise.Plugin$$anon$1.<init>(Plugin.scala:20)
at org.scalalang.macroparadise.Plugin.<init>(Plugin.scala:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

在使用 2.10.3-SNAPSHOT 版本时,我得到了以下信息:

 [warn]     ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.scala-lang.plugins#macro-paradise_2.10.3-SNAPSHOT;2.0.0-SNAPSHOT: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: org.scala-lang.plugins#macro-paradise_2.10.3-SNAPSHOT;2.0.0-SNAPSHOT: not found

而且2.10.2版本根本无法解决scala-library、scala-reflect和2.10.2的依赖关系(比如2.10.3-RC1)!

我的问题是,是否可以混合编译器 API 和宏 API 并使它们在 SBT 下工作,如果可以,我的构建脚本到底出了什么问题?

最佳答案

看来我在 SBT 配置中使用了一些错误的设置。我不需要更改 scalaOrganization,也不需要将 macro-paradise 添加到我的库依赖项中。所以设置应该变成这样:

val sversion = "2.10.2"
val buildSettings = Defaults.defaultSettings ++ Seq(
name := "lombrello",
organization := "ch.usi.inf.l3",
version := "0.1-SNAPSHOT",
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
scalaVersion := sversion,
resolvers += Resolver.sonatypeRepo("snapshots"),
licenses := ("BSD 3-Clause", new java.net.URL("http://opensource.org/licenses/BSD-3-Clause")) :: Nil,
libraryDependencies ++= Seq("org.scala-lang" % "scala-reflect" % sversion,
"org.scala-lang" % "scala-compiler" % sversion),
addCompilerPlugin("org.scala-lang.plugins" % "macro-paradise" % "2.0.0-SNAPSHOT" cross CrossVersion.full)
)

所有学分转到Eugene Burmako在这个comment .

关于scala - 解决SBT中Scala Macros和Compiler Framework的依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18767580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com