Chris Chris - 7 months ago 10
Java Question

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

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


  • Project-A

    • bin

    • lib

    • src

      • main

        • java

          • RootLevelPackageClass.java







I currently use the following convention (as my projects are 100% java):


  • Project-A

    • bin

    • lib

    • src

      • RootLevelPackageClass.java





I'm not currently using Maven but am wondering if this is a Maven convention or not or if there is another reason. Can someone explain why the first version is so popular these days and if I should adopt this new convention or not?

Chris

Answer

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.