简体   繁体   English

使用Scala和SBT为可以从SBT CLI构建的库创建样本的最佳方法是什么?

[英]What's the best way using Scala and SBT to create samples for a library that can be built from the SBT CLI?

I'm creating a library in Scala and would like to include some usage samples with the source. 我正在Scala中创建一个库,并希望在源代码中包含一些使用示例。 However, I don't want those samples to reside within the main source directory (since they're not a part of the library proper), nor do I want them to be built by default when you build the library. 但是,我不希望这些示例驻留在主源目录中(因为它们不是库的一部分),我也不希望在构建库时默认构建它们。 But I would like to be able to do something like "build-samples" from the SBT prompt. 但我希望能够从SBT提示中执行类似“构建示例”的操作。 What's the most straightforward way to do this? 最简单的方法是什么?

You can make a sub-project in SBT that depends on your main project. 您可以在SBT中创建一个取决于您的主项目的子项目。 This allows you to build your library without compiling the examples, but when you need to compile the examples, you can do so with a separate command that will build the library if it needs to. 这允许您在不编译示例的情况下构建库,但是当您需要编译示例时,您可以使用单独的命令来执行此操作,如果需要,将构建库。

A good example is with Twitter's Finagle project. Twitter的Finagle项目就是一个很好的例子。 You can see they have a sub-project "finagle-example" which contains a bunch of examples. 您可以看到他们有一个子项目“finagle-example”,其中包含大量示例。 Take a look at /project/Build.scala to see how they define all the sub-projects. 查看/project/Build.scala ,了解它们如何定义所有子项目。 You should be able to do the same with yours. 你应该能够和你的一样。

What you describes sounds a lot with most common use case of giter8 . 你描述的内容对于giter8的最常见用例来说听起来很多。 Check the unfiltered examples , they are all giter8 templates. 检查未过滤的示例 ,它们都是giter8模板。 And here is a giter8 template for a giter8 project 这是giter8项目的giter8模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM