Why are project layout resources kept separate from Java sources?

后端 未结 8 2103
小鲜肉
小鲜肉 2020-12-10 02:05

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

From my experience, in Java the resource files are mostly treated like Java sou

相关标签:
8条回答
  • 2020-12-10 02:24

    I think that code/resources separation is often useful:

    1. Packages must follow Java specification rules, for example reserved word, like 'int', is not allowed. Having illegal packages in source code folder would be confusing.

    2. Resources has instance nature, classes has, well, classes nature. For example you may have classes Continent and Country in the same package but resources better organize as tree:

      • africa/
        • morocco.txt
        • kenya.txt
      • europe/
        • poland.txt
    3. Resources structure, in my opinion, is more stable than code structure. Code refactoring happens often, classes move from package to package for better readability. Obligation to move resources on refactoring discourages developer from refactor.

    4. If separated resource folder is required even only for 1% of resource files, is reasonable to move remaining 99% to that folder and keep resources in single place only.

    However, there are cases when keeping resources and java code together is useful. For example unit tests compares input and output - nice to have file with input and output in the same folder as unit test code.

    0 讨论(0)
  • 2020-12-10 02:25

    Maybe I'm the only one here who does not come from a Java background. I think that mixing resources and code is very messy. Separation of concerns make your project cleaner and easier to understand/maintain as it grows.

    I try to apply my own directory convention to all my software development projects:

    • bin: final binaries used by user
    • build: intermediate binaries, they may be deleted after build
    • lib: external libraries
    • src: ONLY source code (in this case, Java source files)
    • resources: all resources (configuration files, xmls, images, html, etc)
    • docs: documentation

    Rarely I've found a development context when I needed to change this directory structure.

    Using Maven, if you want to make a quick setup, use the defaults. If you want to use your directory convention, you just need to change settings in the pom.xml as explained before. Be aware, however, that some Maven plugins consider you use default Maven directories for resources/source/output binaries and you'll need to adjust some plugin settings if you change these directories.

    0 讨论(0)
提交回复
热议问题