在SBT中过滤资源

发布于 2024-11-19 01:05:32 字数 598 浏览 4 评论 0原文

我正在尝试设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-11-26 01:05:32

使用 defaultExcludes 作用域为 unmanagedResources 任务以及可选的配置。例如,此设置从主资源中排除 .java 文件:

defaultExcludes in Compile in unmanagedResources := "*.java"

in Compile 限制此设置仅适用于主资源。通过使用 in Test 来代替,它将仅适用于测试资源。通过省略配置(即,没有编译测试),该设置将应用于主资源和测试资源。

in unmanagedResources 仅对资源应用这些排除。例如,要将排除应用到源,范围将是 in unmanagedSources。非托管部分的原因是强调这些仅适用于非托管(或手动编辑)源。

defaultExcludes 键的类型为 sbt.FileFilter,因此设置值必须是该类型。在上面的示例中,"*.java" 被隐式转换为 FileFilter。 * 被解释为通配符,因此过滤器接受名称以“.java”结尾的文件。要组合过滤器,请使用 ||&&。例如,如果还需要排除 .scala 文件,则 := 的参数将为:

"*.java" || "*.scala"

在原始 Ant 文件集中,包含和排除过滤器选择互斥的文件集,因此只有一个是必要的。

还可以直接为 unmanagedResources 构建 Seq[File]。例如:

unmanagedResources in Compile <<=
  unmanagedResourceDirectories in Compile map { (dirs: Seq[File]) =>
    ( dirs ** ("*.txt" || "*.csv" -- "*.java") ).get
  }

** 方法选择与 FileFilter 参数匹配的所有后代。您可以通过运行 show unmanaged-resources 来验证是否按预期选择了文件。

Use defaultExcludes scoped for the unmanagedResources task and optionally the configuration. For example, this setting excludes .java files from the main resources:

defaultExcludes in Compile in unmanagedResources := "*.java"

in Compile restricts this setting to only apply to main resources. By using in Test instead, it would apply only to test resources. By omitting a configuration (that is, no in Compile or in 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 be in 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:

"*.java" || "*.scala"

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] for unmanagedResources. For example:

unmanagedResources in Compile <<=
  unmanagedResourceDirectories in Compile map { (dirs: Seq[File]) =>
    ( dirs ** ("*.txt" || "*.csv" -- "*.java") ).get
  }

The ** method selects all descendents that match the FileFilter argument. You can verify that the files are selected as you expect by running show unmanaged-resources.

唔猫 2024-11-26 01:05:32

对于 sbt 1.48 我需要这种风格:

.settings(
  Compile / sources := Seq(file("/path/to/your/file"))
)

For sbt 1.48 I needed this style:

.settings(
  Compile / sources := Seq(file("/path/to/your/file"))
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文