如何在多模块sbt项目中设置具体的工作目录 [英] how to set concrete working directory in multi module sbt project

查看:96
本文介绍了如何在多模块sbt项目中设置具体的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的包装结构与下面类似;

I have package structure similar to below;

myapplication/
├── my-library/
│   └── src
│       └──main
│          └── scala
│   
├── my-other-library/
│   └── src/
│       └──main/
│          └── scala/
│── my-executable-project/
│   │── src/
│   │   └──main/
│   │      └── scala/
│   └── resources/
│       └── somefile.txt
└── build.sbt

当我通过sbt或intellij运行测试时;

When I run the tests via sbt or intellij;

  • 相对文件(例如new File("build.sbt"))相对于myapplication.
  • relative files (e.g. new File("build.sbt")) being relative to myapplication.

当我通过sbt-revolver或二进制文件使用reStart运行项目时;

When I run the project with reStart via sbt-revolver or from the binary;

  • my-executable-project是工作目录.因此,要访问相同的build.sbt文件,我应该使用new File("../build.sbt")
  • my-executable-project is being the working directory. So to access same build.sbt file I should be using new File("../build.sbt")

这个项目结构对我来说很有意义,因为以后可能还会有其他可执行项目.我更喜欢将每个项目都放在父项目下.

This project structure make sense to me because there may be other executable projects later. I prefer keeping every project under the parent one.

仅将my-executable-project打包并在生产环境中运行.当它再次在那里运行时,my-executable-project是工作目录.

Only my-executable-project is being packaged and run in the production. And when it runs there again my-executable-project is being the working directory.

目前唯一的不便是当我要引用相对文件时,它在测试和常规运行中有所不同.

The only inconvenience right now is when I want to reference to a relative file it is different in tests and regular runs.

我通过使用classpath和classloader克服了资源加载问题,但是找不到相对文件引用的方法.当应用运行测试失败时,当测试运行应用失败时.

I overcome resource loading with the usage of classpath and classloader but couldn't find a way for relative file references. When app runs tests fail, when tests run app fails.

这就是我唯一的build.sbt的样子;

This is how my one and only build.sbt looks like;

lazy val root = project
  .in(file("."))
  .disablePlugins(RevolverPlugin)
  .aggregate(library1, library2, service, common)
  .settings(
    settings,
    name := "parent",
    version := "0.1"
  )

lazy val common = project
  .in(file("common"))
  .disablePlugins(RevolverPlugin)
  .settings(
    settings,
    name := "common",
    libraryDependencies ++= ... some deps ...
  )

lazy val library1 = project
  .in(file("library1"))
  .disablePlugins(RevolverPlugin)
  .dependsOn(common)
  .settings(
    settings,
    name := "library1",
    libraryDependencies ++= ... some deps ...
  )

lazy val library2 = project
  .in(file("library2"))
  .disablePlugins(RevolverPlugin)
  .dependsOn(common)
  .settings(
    settings,
    name := "library2",
    libraryDependencies ++= ... some deps ...
  )

lazy val service = project
  .in(file("service1"))
  .dependsOn(library1, library2)
  .enablePlugins(JavaServerAppPackaging)
  .settings(
    settings,
    name := "service1",
    mappings in Universal ++= directory("service1/src/main/resources"),
    mainClass in Compile := Some("my.main.class.service.Main"),
    Revolver.enableDebugging(port = 5005, suspend = false),
    libraryDependencies ++= ... some deps ...
  )

推荐答案

我解决了这些问题,将所有sbt内容放入了父项目.

I solved these issues putting all sbt stuff in the parent project.

在此处查看文档: https://www. scala-sbt.org/1.x/docs/Multi-Project.html

build.sbt的主要结构如下:

lazy val root = (project in file("."))
  .aggregate(util, core)

lazy val util = (project in file("util"))

lazy val core = (project in file("core"))

然后可以按以下方式配置每个项目:

Every project can then be configured like:

lazy val core = (project in file("core"))
  .enablePlugins(PlayScala, BuildInfoPlugin)
  .settings(generalConf.noPublishSettings)
  .settings(generalConf.buildInfoSettings)
  .settings(coreConf.settings)

在这里您看到我们使用常规配置generalConf和特殊项目配置(coreConf).这些文件然后放在/project文件夹中.

Here you see that we use general configs generalConf and special project config (coreConf). These files are then in the /project folder.

这篇关于如何在多模块sbt项目中设置具体的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆