1

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
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • Why on earth would you want to do this? The forced directory structure that comes with the Java package structure is one of the features of Java. This sounds much like an XY problem to me. – Boris the Spider Sep 30 '14 at 22:45
  • If your class `ClassA` is declared to be in package `com.domain.name`, then it has to physically be in the directory `com/domain/name` on the file system or in a archive entry of the same name, if within a `.jar`. – Sotirios Delimanolis Sep 30 '14 at 22:45
  • @BoristheSpider Every *.java file will be in the same package, having them in the main source folder seems more natural to me, especially since I've learned to separate project and distribution structure. Basically, it's only about comfort. – Daniel Jour Sep 30 '14 at 22:52
  • @SotiriosDelimanolis So there is no way (except maybe for hardlink gymnastics) to accomplish this? – Daniel Jour Sep 30 '14 at 22:53

1 Answers1

1

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

Community
  • 1
  • 1
deyur
  • 557
  • 2
  • 14