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

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

问题描述

为什么maven将资源保存在Java源的单独源文件夹中?

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

根据我的经验,在Java中,资源文件大多被视为Java源文件,当编译时,只需要按类复制,并最终打包在jar中,并通过 classloader 的方法访问 getResource / getResourceAsStream ,通过类路径

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 / resources src / test / resources 并在 src / main / java <中保留
    资源/ code>和 src / 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.:


  • src / main

    • 资源

    • java

    • groovy

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

    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天全站免登陆