• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best practice for project directory structure in java

 
Ranch Hand
Posts: 75
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can elaborate Carey, I'm not sure how this relates to OP's question?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  •  
    Saloon Keeper
    Posts: 27752
    196
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Saloon Keeper
    Posts: 27752
    196
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 15484
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic