sbt 项目构建中的多个目标目录 [英] Multiple target directories in sbt project build

查看:43
本文介绍了sbt 项目构建中的多个目标目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个采用这种结构的 sbt 项目:

<预><代码>.├── build.sbt├──项目│ ├── build.properties│ └── plugins.sbt└── src├── 主要│ └── java│ └── smcho│ └── App.java└── 测试└── 爪哇└── smcho└── AppTest.java

使用sbt package,我有3个target目录,target2和target3结构类似.

<预><代码>.├── build.sbt├──项目│ ├── build.properties│ ├── plugins.sbt│ ├──项目│ │ └── 目标 <--------- 目标 1│ └── 目标 <--------- 目标 2│ ├── 配置类│ ├──分辨率缓存│ ├── scala-2.10│ └── 流├── src│ ├── main│ │ └── java│ └── 测试│ └── java└── 目标 <--------------- 目标 3├── 分辨率缓存│ ├── com.example│ └── 报道├── scala-2.11│ ├──类│ └── hello_2.11-0.1.0.jar└── 流├── $全球└── 编译

为什么会这样?有趣的是,sbt clean 不会删除目标目录,有没有办法让一个简单的目标可以轻松删除它们?

这是build.sbt:

lazy val hello = taskKey[Unit]("一个示例任务")val junit = "junit" % "junit" % "4.11" % "test"惰性 val commonSettings = Seq(组织:=com.example",版本:=0.1.0",斯卡拉版本:=2.11.4")懒惰的 val root =(文件中的项目(.")).设置(通用设置:_*).设置(你好 := { println("你好!") },名称:=你好",libraryDependencies += junit)

解决方案

SBT 中的每个项目都有一个 target 目录.这就是它编译的类和其他生成的东西去的地方.

你的 root 是一个项目,你的图中的 TARGET 3 是它的目标.

您的构建定义(project 目录)也是一个项目.使用 SBT,可以编写 Scala 代码来实现与构建相关的任务和设置.编译后的代码必须放在某个地方.它位于您标记为 TARGET 2 - project/target 的目录中.

SBT 中的构建定义可以是递归的,即您的构建定义可以有自己的构建定义.由于您使用的是插件(在 project/plugins.sbt 中定义),因此您的构建定义需要一个构建定义,该定义最终会被编译为 project/project/target aka TARGET 1 在你的图表中.

当您在 SBT 控制台中运行 clean 时,它将清除当前项目目标目录中的文件.我不认为这意味着删除整个目录,但我可能错了.在任何情况下,在选择 root 项目时运行 clean 只会影响 root 项目的 target.

在 SBT 控制台中,您可以运行 reload pluginsreload return 以跳入和跳出(分别)当前项目的构建定义.在该上下文中调用 clean 将清理它们各自的 targets.

至于将它们合并到一个易于删除的目录中,我不确定我是否看到了其中的价值.使用 SBT 已经好几年了,各种 target 目录从来没有真正妨碍过.我想在过去一年左右的时间里,我什至不想删除一次目标目录.

I have a sbt project in this structure:

.
├── build.sbt
├── project
│   ├── build.properties
│   └── plugins.sbt
└── src
    ├── main
    │   └── java
    │       └── smcho
    │           └── App.java
    └── test
        └── java
            └── smcho
                └── AppTest.java

With sbt package, I have three target directories, and target2 and target3 have a similar structure.

.
├── build.sbt
├── project
│   ├── build.properties
│   ├── plugins.sbt
│   ├── project
│   │   └── target <---------- TARGET 1
│   └── target     <---------- TARGET 2
│       ├── config-classes
│       ├── resolution-cache
│       ├── scala-2.10
│       └── streams
├── src
│   ├── main
│   │   └── java
│   └── test
│       └── java
└── target  <--------------- TARGET 3
    ├── resolution-cache
    │   ├── com.example
    │   └── reports
    ├── scala-2.11
    │   ├── classes
    │   └── hello_2.11-0.1.0.jar
    └── streams
        ├── $global
        └── compile

Why so? Interestingly, sbt clean does not remove the target directories, is there a way to have one simple target so that I can easily remove them?

This is build.sbt:

lazy val hello = taskKey[Unit]("An example task")
val junit = "junit" % "junit" % "4.11" % "test"

lazy val commonSettings = Seq( 
    organization := "com.example", 
    version := "0.1.0", 
    scalaVersion := "2.11.4"
)

lazy val root = (project in file(".")). 
    settings(
        commonSettings: _*
). 
    settings(
        hello := { println("Hello!") },
        name := "hello",
        libraryDependencies += junit
)

解决方案

Every project in SBT has a target directory. That's where its compiled classes and other generated things go.

Your root is a project, and TARGET 3 in your diagram is its target.

Your build definition (the project directory) is also a project. With SBT it is possible to write scala code to implement build-related tasks and settings. That compiled code has to go somewhere. It goes in the directory you labeled as TARGET 2 - project/target.

Build definitions in SBT can be recursive, i.e. your build definition can have its own build definition. Since you are using plugins (defined in project/plugins.sbt), your build definition needs a build definition, which ends up being compiled to project/project/target aka TARGET 1 in your diagram.

When you run clean in the SBT console, it will clean files from the current project's target directory. I don't think it's meant to delete the entire directory, but I could be wrong there. In any case, running clean while you have the root project selected should only affect the root project's target.

In the SBT console, you can run reload plugins or reload return to jump into and out of (respectively) the current project's build definition. Calling clean within that context would clean their respective targets.

As for combining them into a single, easily-removed directory, I'm not sure I see the value in that. Having used SBT for several years now, the various target dirs haven't ever really been in the way. I don't think I've even wanted to delete the target directory a single time in the last year or so.

这篇关于sbt 项目构建中的多个目标目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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