You could follow maven's standard project layout. You don't have to actually use maven, but it would make the transition easier in the future (if necessary). Plus, other developers will be used to seeing that layout, since many open source projects are layed out this way,
Answer from johnstok on Stack OverflowYou could follow maven's standard project layout. You don't have to actually use maven, but it would make the transition easier in the future (if necessary). Plus, other developers will be used to seeing that layout, since many open source projects are layed out this way,
There are a few existing resources you might check:
- Properly Package Your Java Classes
- Spring 2.5 Architecture
- Java Tutorial - Naming a Package
- SUN Naming Conventions
For what it's worth, my own personal guidelines that I tend to use are as follows:
- Start with reverse domain, e.g. "com.mycompany".
- Use product name, e.g. "myproduct". In some cases I tend to have common packages that do not belong to a particular product. These would end up categorized according to the functionality of these common classes, e.g. "io", "util", "ui", etc.
- After this it becomes more free-form. Usually I group according to project, area of functionality, deployment, etc. For example I might have "project1", "project2", "ui", "client", etc.
A couple of other points:
- It's quite common in projects I've worked on for package names to flow from the design documentation. Usually products are separated into areas of functionality or purpose already.
- Don't stress too much about pushing common functionality into higher packages right away. Wait for there to be a need across projects, products, etc., and then refactor.
- Watch inter-package dependencies. They're not all bad, but it can signify tight coupling between what might be separate units. There are tools that can help you keep track of this.
what's with a Java project folder structure ?
It depends on the build system in use for the project.
In the days of Ant or if you let the IDE set it up for you without specifying a build, it was mostly:
src/ bin/
Maven and Gradle use the Maven Standard Directory Layout ( https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html )
src
main
{lang}
packages
resources
test
{lang}
packages
resourcesYou can see that when using Gradle for groovy - https://docs.gradle.org/current/userguide/groovy_plugin.html#sec:groovy_project_layout the lang for the groovy sources is 'groovy'. This is important if you mix groovy, scala, kotlin, and java all in one project. For example (not my project, just one I found out there) https://github.com/bellingard/sample_multi-language-project
More on reddit.comproject structure - Folder-by-type or Folder-by-feature - Software Engineering Stack Exchange
deployment - What is the project directory structure for a standalone Java SE application? - Stack Overflow
Java : Difference between folder and package
Videos
So here the thing guys. I'm a data engineer by profession meaning ETL/SQL master. Recently with the explosion of new tools that hit the market in big data, I'm trying to learn Java. The challenge is, wrapping my head around Java folder structure. When I look at code in GitHub for few of the Apache projects like spark/airflow, it's so convoluted and varies by developer. Is there a rule or strategy that's developers commonly follow?
It depends on the build system in use for the project.
In the days of Ant or if you let the IDE set it up for you without specifying a build, it was mostly:
src/
bin/
Maven and Gradle use the Maven Standard Directory Layout ( https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html )
src
main
{lang}
packages
resources
test
{lang}
packages
resources
You can see that when using Gradle for groovy - https://docs.gradle.org/current/userguide/groovy_plugin.html#sec:groovy_project_layout the lang for the groovy sources is 'groovy'. This is important if you mix groovy, scala, kotlin, and java all in one project. For example (not my project, just one I found out there) https://github.com/bellingard/sample_multi-language-project
The folder structure in the src directory or of the entire project in general? Project source directory is usually driven by the convention of the build tool you're using, like gradle. The folder structure in your src directory is building the package names for your classes. The goal here is to be consistent within an organization and unique so there is little chance of ambiguity when you bring in a lot of external libraries.
Folder-by-type only works on small-scale projects. Folder-by-feature is superior in the majority of cases.
Folder-by-type is ok when you only have a small number of files (under 10 per type, let's say). As soon as you get multiple components in your project, all with multiple files of the same type, it gets very hard to find the actual file you are looking for.
Therefore, folder-by-feature is better due to its scalability. However, if you use folder-by-feature, you end up losing information about the type of component a file represents (because it's no longer in a controller folder, let's say), so this too becomes confusing. There are 2 simple solutions for this.
First, you can abide by common naming conventions that imply its type in the file name. For example, John Papa's popular AngularJS style guide has the following:
Naming Guidelines
Use consistent names for all components following a pattern that describes the component's feature then (optionally) its type. My recommended pattern is
feature.type.js. There are 2 names for most assets:
- the file name (
avengers.controller.js)- the registered component name with Angular (
AvengersController)
Second, you can combine folder-by-type and folder-by-feature styles into folder-by-feature-by-type:
com.example
βββ pet
| βββ Controllers
β | βββ PetController1.java
| | βββ PetController2.java
| βββ Services
β βββ PetService1.java
β βββ PetService2.java
βββ user
| βββ Controllers
β | βββ UserController1.java
β | βββ UserController2.java
| βββ Services
β βββ UserService1.java
β βββ UserService2.java
This really has nothing to do with the technology in question, unless you use a framework that forces folder-by-type on you as part of a convention-over-configuration approach.
Personally, I am strongly of the opinion, that folder-by-feature is far superior and should be used everywhere as much as possible. It groups together classes that actually work together, whereas folder-by-type just duplicates something that is usually already present in the class name.
I would recommend to stick with default Maven layout ( and also use maven as build tool )
Productive classes / resources:
src/main/java
src/main/resources
Test data and classes:
src/test/java
src/test/resources
Maven can also take care to package your application properly with all the necessary jars ( look for maven assembly plugin )
src/com.enterprise_name.project_name. Main.java (the main class)
src/com.enterprise_name.project_name.model.(here all model classes)
src/com.enterprise_name.project_name.view.(here all view classes, JFrame, Jdialog,etc)
src/com.enterprise_name.project_name.view.resources.(here all the files and images used in the views *note)
src/com.enterprise_name.project_name.controller.(here all controller classes)
lib/(here all the external libraries - dont forget add to build path)
*note if you need some resource file (xml, config file, etc) create a package .resources. in the specific place where do you need (model, controller, view)