sbt 项目构建中的多个目标目录 [英] Multiple target directories in sbt project build
问题描述
我有一个采用这种结构的 sbt 项目:
<预><代码>.├── build.sbt├──项目│ ├── build.properties│ └── plugins.sbt└── src├── 主要│ └── java│ └── smcho│ └── App.java└── 测试└── 爪哇└── smcho└── AppTest.java使用sbt package
,我有3个target目录,target2和target3结构类似.
为什么会这样?有趣的是,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 plugins
或 reload return
以跳入和跳出(分别)当前项目的构建定义.在该上下文中调用 clean
将清理它们各自的 target
s.
至于将它们合并到一个易于删除的目录中,我不确定我是否看到了其中的价值.使用 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 target
s.
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屋!