What is the advantage of the 'src/main/java'' convention?

前端 未结 5 1185
无人及你
无人及你 2020-12-04 11:51

I\'ve noticed that a lot of projects have the following structure:

  • Project-A
    • bin
    • lib
    • src
      • main
        • java
          • Ro
相关标签:
5条回答
  • 2020-12-04 12:28

    Others have already told you it's a Maven convention, I'm going to answer your question instead:

    Absolutely none. Certainly it's beneficial to separate pieces of code to separate root folders, but usually you could achieve the same with

    • [root]
      • src
        • com.org.net
          • Your.class
      • test
        • com.org.net
          • YourTest.class
      • lib
      • bin
      • resources

    instead. In fact here's a big thing Maven does that's actually hugely wrong: It wants to add binary content to source code repository which is meant for textual content only! All binary content should be managed outside the source code repository, that includes images in web applications and whatnot.

    But OK, lets assume that you've decided to live in the somewhat smelly Maven ecosystem; then you should of course follow the Maven conventions as strictly as possible.

    0 讨论(0)
  • 2020-12-04 12:32

    Main benefit is in having the test directory as subdirectory of src with the same directory structure as the one in main:

    • Project-A
      • bin
      • lib
      • src
        • main
          • java
            • RootLevelPackageClass.java
          • resources
        • test
          • java
            • TestRootLevelPackageClass.java
          • resources

    All package private methods of RootLevelPackageClass will be visible, i.e. testable from TestRootLevelPackageClass. Since the testing code is also source its place should be under src directory.

    0 讨论(0)
  • 2020-12-04 12:44

    Yes, this is the Maven convention.

    Even if your project is 100% Java (as is typical with Maven btw), you often have resource files (which go to src/main/resources according to the Maven convention), or web app stuff, or ... all these fit into the Maven system easily.

    If you are happy with your current build system (whatever it is), there is no reason to switch to Maven. Otherwise, or if starting a new project, you could evaluate your options, including Maven.

    0 讨论(0)
  • 2020-12-04 12:44

    Its a Maven convention.

    Maven is based on Convention over configuration paradigm. Thats means: if you dont follow this convention you must configure where the sources are located. Thats the main benefit IMHO.

    0 讨论(0)
  • 2020-12-04 12:44

    Yes, this is a maven convention, but even if you're not using maven, there are benefits to using it:

    1. people new to the project will have an easier time coming up to speed, since it's a "standard"
    2. this convention is flexible and has a place for non-Java code and other things you don't have at this point. This is one reason it's popular and you may find it evolves better than a scheme you come up with on your own
    3. if you want to move to maven at some point it will be easy

    Although I wouldn't argue you should switch just to switch, when starting a new project there's really no reason to not use it-- unless you disagree philosophically with how it breaks the code up.

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