在SBT中过滤资源
我正在尝试设置 SBT 来编译不使用 Maven 目录结构的现有项目。我正在使用 完整配置 并设置了我的 javaSource & resourcesDirectory 设置如下:
def settings = Defaults.defaultSettings ++ Seq(
resourceDirectory in Compile <<= baseDirectory( _ / "java" ),
javaSource in Compile <<= baseDirectory( _ / "java" )
)
现在我希望能够过滤我们包含在 jar 工件中的资源,就像我们目前对 ant 所做的那样,加上排除 .java 文件,因为我们的资源与源代码混合在一起。例如:
<fileset dir="java" includes="**/*.txt, **/*.csv" excludes="**/*.java" />
有什么办法可以做到这一点吗?
I am trying to setup SBT to compile an existing project which does not use the maven directory structure. I am using the full configuration and have set my javaSource & resourceDirectory settings as follows:
def settings = Defaults.defaultSettings ++ Seq(
resourceDirectory in Compile <<= baseDirectory( _ / "java" ),
javaSource in Compile <<= baseDirectory( _ / "java" )
)
Now I want to be able to filter the resources we include in the jar artifact, as we currently do with ant, plus exclude .java files as our resources are mixed in with source code. For example:
<fileset dir="java" includes="**/*.txt, **/*.csv" excludes="**/*.java" />
Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
defaultExcludes
作用域为unmanagedResources
任务以及可选的配置。例如,此设置从主资源中排除 .java 文件:in Compile
限制此设置仅适用于主资源。通过使用in Test
来代替,它将仅适用于测试资源。通过省略配置(即,没有编译
或测试
),该设置将应用于主资源和测试资源。in unmanagedResources
仅对资源应用这些排除。例如,要将排除应用到源,范围将是in unmanagedSources
。非托管部分的原因是强调这些仅适用于非托管(或手动编辑)源。defaultExcludes
键的类型为 sbt.FileFilter,因此设置值必须是该类型。在上面的示例中,"*.java"
被隐式转换为 FileFilter。*
被解释为通配符,因此过滤器接受名称以“.java”结尾的文件。要组合过滤器,请使用||
和&&
。例如,如果还需要排除 .scala 文件,则:=
的参数将为:在原始 Ant 文件集中,包含和排除过滤器选择互斥的文件集,因此只有一个是必要的。
还可以直接为
unmanagedResources
构建Seq[File]
。例如:**
方法选择与FileFilter
参数匹配的所有后代。您可以通过运行show unmanaged-resources
来验证是否按预期选择了文件。Use
defaultExcludes
scoped for theunmanagedResources
task and optionally the configuration. For example, this setting excludes .java files from the main resources:in Compile
restricts this setting to only apply to main resources. By usingin Test
instead, it would apply only to test resources. By omitting a configuration (that is, noin Compile
orin Test
), the setting would apply to both main and test resources.in unmanagedResources
applies these excludes for resources only. To apply excludes to sources, for example, the scope would bein unmanagedSources
. The reason for the unmanaged part is to emphasize that these apply to unmanaged (or manually edited) sources only.The
defaultExcludes
key has type sbt.FileFilter, so the setting value must be of this type. In the example above,"*.java"
is implicitly converted to a FileFilter.*
is interpreted as a wildcard and so the filter accepts files with a name that ends in '.java'. To combine filters, you use||
and&&
. For example, if .scala files needed to be excluded as well, the argument to:=
would be:In the original Ant fileset, the include and exclude filters select mutually exclusive sets of files, so only one is necessary.
It is also possible to directly build the
Seq[File]
forunmanagedResources
. For example:The
**
method selects all descendents that match theFileFilter
argument. You can verify that the files are selected as you expect by runningshow unmanaged-resources
.对于 sbt 1.48 我需要这种风格:
For sbt 1.48 I needed this style: