Search code examples
javaantdirectory-structure

Avoid deep directory structure in a java project


Is it possible, for example with the help of ant, to avoid deep directory structures that come along with deep package nesting?

Suppose I wrote ClassA and ClassB, both in package com.domain.name, is it possible to avoid a directory layout like

project/
- build.xml
- src/
  - com/
    - domain/
      - name/
        - ClassA.java
        - ClassB.java

and go instead for the simpler

project/
- build.xml
- src/
  - ClassA.java
  - ClassB.java

Solution

  • The reason that this sort of structure is discouraged is that it doesn't scale well. Your example is constrained to having two classes in the same package. What happens when you have classes in different packages or with different package ancestries?

    For example
    project/
    - build.xml
    - src/
      - com/
        - domain/
          - package1/
            - ClassA.java
            - ClassB.java
          - package2
            - ClassC.java
    

    This is much more maintainable with moderately sized projects (or even small projects). The alternative that you suggested would already get confusing because it does not communicate structure at all.

    For example
    project/
    - build.xml
    - src/
        - ClassA.java
        - ClassB.java        
        - ClassC.java
    

    As to your larger question of 'is it possible', Java does not mandate that directory structure matches package structure, though the convention is strong. See Package name is different than the folder structure but still Java code compiles