• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best practice for project directory structure in java  RSS feed

 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. Pic shows the project directory which I used for my java project(without IDE). Just tell me whether the structure is correct or not i.e keeping localization files in localization folder, postgres jar file in database folder , dbconfig.properties file in config folder etc
projectstr.JPG
[Thumbnail for projectstr.JPG]
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev,

If you are doing this for yourself, then you are the judge of right or wrong, but if you are doing this at work, then your work standards should be implemented.

Rajeev Pedada wrote:Hello all. Pic shows the project directory which I used for my java project(without IDE). Just tell me whether the structure is correct or not i.e keeping localization files in localization folder, postgres jar file in database folder , dbconfig.properties file in config folder etc
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears you're using a Maven project layout.

Personally, I'm a fan of keeping localization and configuration files in a hierarchy that reflects the packages where they are used.

For instance, if you have a package that deals with persistence, let's say tld.company.project.repo, then I would put my DB configuration in a file src/main/config/tld/company/project/repo/db.properties, and add src/main/config as one of my project's resource folders.

Whatever you do, be consistent.
 
Carey Brown
Saloon Keeper
Posts: 2538
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maven separates these two:
src/main/java
src/test/java

You don't want your release package to include your tests.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can elaborate Carey, I'm not sure how this relates to OP's question?
 
Rob Spoor
Sheriff
Posts: 20897
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Maybe you can elaborate Carey, I'm not sure how this relates to OP's question?

There appears to be a test package next to the customexception and service packages.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I missed that! Yes, Carey is correct, test cases should go in src/test/java.
 
Rajeev Srikhar
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But after compiling , class files of java files present in src and test should be in same directory ,right?
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. After compilation, resources should be in the same directory as the compiled classes (somewhere within the 'target' folder, but test classes should live in a separate directory.

When running tests, you should add both the compiled main classes directory and the compiled test classes directory on the classpath. When running the application, only the compiled main classes should be on the classpath.

If you're really using Maven, all of this happens automatically and you don't have to worry about it.
 
Stephan van Hulst
Saloon Keeper
Posts: 6981
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a small test application in Maven, with:

  • src/main/java/com/example/Greeting.java
  • src/main/resources/com/example/hello.txt
  • src/test/java/com/example/GreetingTest.java


  • After compilation, Maven puts these files here:

  • target/classes/com/example/Greeting.class
  • target/classes/com/example/hello.txt
  • target/test-classes/com/example/GreetingTest.class
  •  
    Tim Holloway
    Bartender
    Posts: 18531
    61
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:It appears you're using a Maven project layout.

    Personally, I'm a fan of keeping localization and configuration files in a hierarchy that reflects the packages where they are used.

    For instance, if you have a package that deals with persistence, let's say tld.company.project.repo, then I would put my DB configuration in a file src/main/config/tld/company/project/repo/db.properties, and add src/main/config as one of my project's resource folders.

    Whatever you do, be consistent.


    For Maven to pick it up properly, that should be src/main/resources/tld/company/project/repo/db.properties

    That will cause the properties file to automatically be copied into target/classes/tld/company/project/repo/db.properties without any additional tweaking of the POM.
     
    Tim Holloway
    Bartender
    Posts: 18531
    61
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Note that for web applications, the path would normally be

    src/main/resources/WEB-INF/classes/tld/company/project/repo/db.properties
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 6981
    110
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree. I should have mentioned I use my proposed method when I have a lot of config files and localized resource bundles, so I have different folders for configuration, localization and other resources.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!