如何为多项目设置envVars?

时间:2017-12-25 16:04:49

标签: scala sbt

我有多个项目,并尝试在子项目的envVars中设置build.sbt,如下所示:

envVars in Test := Map("KAFKA_SERVER" -> "localhost:9092")

测试中止,并显示以下消息:

[info]   java.util.NoSuchElementException: None.get
[info]   at scala.None$.get(Option.scala:349)
[info]   at scala.None$.get(Option.scala:347)
[info]   at io.khinkali.auth.AppSpec.<init>(AppSpec.scala:23)
[info]   at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  

在测试文件中,我尝试获取如下值:

sys.env.get("KAFKA_SERVER").get  

Intellj提供环境变量集如下:

enter image description here

如何在sbt中为subprojects设置环境变量? 更新

build.sbt如下所示:

name := "bary"

scalacOptions += "-Ypartial-unification"
scalacOptions += "-feature"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)


val Cats = "1.0.0"
val Shiro = "1.4.0"
val Logback = "1.2.3"
val CatsEffect = "0.5"
val Kafka = "1.0.0"
val Bean = "1.9.3"
val Circe = "0.9.0-M3"
val Log4j = "1.7.25"
val ScalaCheck = "1.13.4"
val Scalactic = "3.0.4"
val Scalatest = "3.0.4"
val JavaJwt = "3.3.0"
val Simulacrum = "0.11.0"
val Http4s = "0.18.0-M7"


lazy val commonSettings = Seq(
  organization := "io.khinkali",
  version := "0.1.0-SNAPSHOT",
  scalaVersion := "2.12.4",
  envVars in Test := Map("KAFKA_SERVER" -> "localhost:9092"),
  fork in Test := true,
  libraryDependencies ++= Seq(
    "org.slf4j" % "slf4j-simple" % Log4j,
    "ch.qos.logback" % "logback-core" % Logback,
    "org.apache.shiro" % "shiro-all" % Shiro,
    "org.typelevel" %% "cats-core" % Cats,
    "org.typelevel" %% "cats-effect" % CatsEffect,
    "org.apache.kafka" % "kafka-streams" % Kafka,
    "org.apache.kafka" % "kafka-clients" % Kafka,
    "commons-beanutils" % "commons-beanutils" % Bean,
    "io.circe" %% "circe-core" % Circe,
    "io.circe" %% "circe-generic" % Circe,
    "io.circe" %% "circe-parser" % Circe,
    "io.circe" %% "circe-literal" % Circe,
    "com.github.mpilquist" %% "simulacrum" % Simulacrum,

    "org.scalactic" %% "scalactic" % Scalactic,
    "org.scalatest" %% "scalatest" % Scalatest % "test",
    "org.scalacheck" %% "scalacheck" % ScalaCheck % "test",
  ),
  resolvers ++= Seq(
    "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
  ),
  fork in run := true,
)

lazy val root = (project in file("."))
  .settings(commonSettings)
  .settings(
    name := "bary",
    organization := "io.khinkali",
    moduleName := "bary"
  ).
  aggregate(
    kafka_api,
    auth_stream,
    rest)

lazy val kafka_api = (project in file("kafka-api")).
  settings(commonSettings).
  settings(
    name := "kafka-api",
    moduleName := "kafka-api"
  )

lazy val auth_stream = (project in file("auth-stream")).
  settings(commonSettings).
  settings(
    name := "auth-stream",
    moduleName := "auth-stream",
    libraryDependencies ++= Seq(
      "com.auth0" % "java-jwt" % JavaJwt,
    )
  ).dependsOn(kafka_api)

lazy val rest = (project in file("rest")).
  settings(commonSettings).
  settings(
    name := "rest",
    moduleName := "rest",
    libraryDependencies ++= Seq(
      "org.http4s" %% "http4s-dsl" % Http4s,
      "org.http4s" %% "http4s-blaze-server" % Http4s,
      "org.http4s" %% "http4s-blaze-client" % Http4s,
      "org.http4s" %% "http4s-circe" % Http4s,
    )
  ).dependsOn(kafka_api, auth_stream)

我仍然有例外。

1 个答案:

答案 0 :(得分:1)

您要应用于子项目的任何设置都可以仅针对该子项目或commonSettings指定,如下面的build.sbt所示。

您没有显示多项目定义,因此这是一个简短的示例。设置这些类型的项目有很多方法,我不打算详细说明可能的方法;这是一个复杂的话题,多年来发展很多,特别是最近。

lazy val commonSettings = Seq(
  envVars in Test := Map("KAFKA_SERVER" -> "localhost:9092"),
  fork in Test := true, // required for envVars task to work
  javacOptions ++= Seq(
    "-Xlint:deprecation",
    "-Xlint:unchecked",
    "-source", "1.8",
    "-target", "1.8",
    "-g:vars"
  ),
  licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html")),
  version := "0.5.0"
)

lazy val demo = project
  .settings(commonSettings:_*)
  .settings(
    name := "demo"
  ).dependsOn(root)

lazy val root = (project in file("root"))
  .settings(commonSettings:_*)
  .settings(
    name := "root"
  )

一个更重要的事实:使用IntelliJ IDEA测试运行器运行时,test的环境设置将不受尊重。作为解决方法,您可以在Run/Debug Configurations - &gt;中设置环境变量。 Environment variables窗口。但是,当您运行sbt test时,将设置build.sbt中指定的环境变量。

通常,将环境变量应用于流程的唯一方法是使用该环境变量启动流程。如果您希望SBT在具有特定环境变量的环境中运行程序,则需要在新环境中启动它。这称为forking