gpt4 book ai didi

scala - SBT 从子项目运行 Main 方法

转载 作者:行者123 更新时间:2023-12-05 00:55:59 26 4
gpt4 key购买 nike

我正在编写一个包含 Scala 宏的项目。这就是为什么我将我的项目组织成两个子项目,称为“macroProj”和“coreProj”。 “coreProj”依赖于“macroProj”并且还包含运行我的代码的主要方法。

我的要求是,当我做 sbt run从根项目来看,main方法取自coreProj子项目。我搜索并找到了一个解决方案的线程

sbt: run task on subproject

基于此,我的 build.sbt 看起来像

lazy val root = project.aggregate(coreProj,macroProj).dependsOn(coreProj,macroProj)

lazy val commonSettings = Seq(
scalaVersion := "2.11.7",
organization := "com.abhi"
)

lazy val coreProj = (project in file("core"))
.dependsOn(macroProj)
.settings(commonSettings : _*)
.settings(
mainClass in Compile := Some("demo.Usage")
)

lazy val macroProj = (project in file("macro"))
.settings(commonSettings : _*)
.settings(libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value)

mainClass in Compile <<= (run in Compile in coreProj)

但是当我这样做时 sbt run从根目录。我收到一个错误
/Users/abhishek.srivastava/MyProjects/sbt-macro/built.sbt:19: error: type mismatch;
found : sbt.InputKey[Unit]
required: sbt.Def.Initialize[sbt.Task[Option[String]]]
mainClass in Compile <<= (run in Compile in coreProj)
^
[error] Type error in expression

最佳答案

在 sbt,mainClass是一个将返回程序入口点的任务(如果有)。

你要做的就是拥有mainClassmainClass 的值在 coreProj .

您可以通过以下方式实现:

mainClass in Compile := (mainClass in Compile in coreProj).value

这也可以通过
mainClass in Compile <<= mainClass in Compile in coreProj

但是,首选语法是 (...).value since sbt 0.13.0 .

关于scala - SBT 从子项目运行 Main 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105041/

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