Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 396
Chris Avatar asked Jun 09 '10 09:06

Chris


People also ask

What is the use of src main Java in Maven?

The src/main Directory As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here. Its subdirectories are: src/main/java – Java source code for the artifact.

What is src in Java?

Describes the Java™ source tree. When you create an API project, it is set up as a Java project with separate folders for source and class files. The source folder is named src . It contains the Java code of the application.

What should be in the src folder?

The /src folder comprises of the raw non-minified code. The /src folder is used to store the file with the primary purpose of reading (and/or editing) the code. The /src folder contains all the sources, i.e. the code which is required to be manipulated before it can be used.

Is src a package in Java?

At the top you see a directory called "src". This is the source root directory. It is not a Java package itself.


2 Answers

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.

like image 192
Boris Pavlović Avatar answered Oct 25 '22 23:10

Boris Pavlović


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.

like image 36
Péter Török Avatar answered Oct 26 '22 00:10

Péter Török