首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置多项目的envVars?

如何设置多项目的envVars?
EN

Stack Overflow用户
提问于 2017-12-25 16:04:49
回答 1查看 1.4K关注 0票数 1

我有sbt多个项目,并尝试将子项目的envVars设置为:

代码语言:javascript
复制
envVars in Test := Map("KAFKA_SERVER" -> "localhost:9092")

带有以下消息的测试中止:

代码语言:javascript
复制
[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)  

在测试文件中,我试图获得如下值:

代码语言:javascript
复制
sys.env.get("KAFKA_SERVER").get  

Intellj提供设置环境变量如下:

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

更新

build.sbt如下所示:

代码语言:javascript
复制
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)

我还是有例外。

EN

回答 1

Stack Overflow用户

发布于 2017-12-25 19:38:03

您希望应用于子项目的任何设置都可以仅为该子项目指定,也可以在commonSettings中指定,如下面的build.sbt所示。

您没有显示您的多项目定义,下面是一个简短的示例。建立这类项目的方法有很多种,我不打算详细说明可能的方法;这是一个复杂的问题,多年来,特别是最近的几年来,它已经有了很大的发展。

代码语言:javascript
复制
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"
  )

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47970180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档