为什么项目布局资源与 Java 源分开? [英] Why are project layout resources kept separate from Java sources?

查看:17
本文介绍了为什么项目布局资源与 Java 源分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 maven 将资源保存在与 Java 源不同的源文件夹"中?

Why does maven keep resources in a separate "source folder" from the Java sources?

根据我的经验,在 Java 中,资源文件大多被视为 Java 源文件,在编译"时,只需要按原样与类一起复制,并最终打包在 jar 中,并由 classloader 的方法 getResource/getResourceAsStream,通过 classpath.

From my experience, in Java the resource files are mostly treated like Java source files which, when "compiled", just need to be copied as-is with the classes, and eventually packaged in the jar, and accessed by the classloader's methods getResource/getResourceAsStream, via the classpath.

我个人认为将资源文件与 Java 源分开是一种无用的复杂性.

Personally I find it a useless complexity to keep resource files separate from Java sources.

  1. 你怎么看?
  2. maven 将资源与来源分开是否有充分的理由?
  3. 不使用 src/main/resourcessrc/test/resources 并保持src/main/javasrc/test/java 中使用 maven 的资源?
  1. What do you think?
  2. Is there a good reason why maven keeps resources separate from sources?
  3. Is there any counter indication in not using src/main/resources and src/test/resources and keeping resources in src/main/java and src/test/java using maven?

推荐答案

尚未提出的一点是,您显然已经习惯于查看仅包含 Java 源代码的项目.但是,如果您加入其他一些源文件类型,我认为该组织更有意义,例如:

One point that hasn't been brought up yet is that you are obviously used to seeing projects with only java sources in them. however, if you throw in some other source file types, i think the organization makes more sense, e.g.:

  • 源/主
    • 资源
    • java
    • 时髦

    每个子目录都有特定的文件分类:

    each sub-dir has a specific classification of files:

    • java -> 编译为 java 文件的东西
    • groovy -> 编译为 groovy 脚本的东西
    • 资源 -> 用于任何用途的未编译数据...(此外,这些可能会被过滤以添加编译时信息)

    还有(正如我已经在一些评论中指出的那样),我通常不会使资源目录平坦.文件可以嵌套到类似包的结构中或适当地嵌套到其他子目录中(根据我的组织感).

    also (as i've noted in some comments already), i don't usually make the resources directory flat. files may be nested into a package-like structure or into other sub-directories as appropriate (to my sense of organization).

    这篇关于为什么项目布局资源与 Java 源分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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