开发者

right place in java source tree for auxiliary data?

开发者 https://www.devze.com 2023-03-11 11:15 出处:网络
I understand the basic structure of a java source tree & jar file: com example mypackage myclass1.java

I understand the basic structure of a java source tree & jar file:

com
  example
    mypackage
      myclass1.java
      myclass2.java

But if I have bitmaps or h开发者_C百科tml files, are there conventions for where I should put them?

In the past, I've added a ui directory in the source tree root (e.g. a sibling directory from the com directory above). But this feels "sneaky": technically there could be a "ui" package.


I prefer the Maven approach, of having a resources directory that is separate from the Java source files:

my-app
|-- pom.xml
`-- src
    |-- main
    | `--- resources <-- Resources go here (sibling directory of java)
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

If you want a more "enterprisey" approach, use the "Project Conventions for Enterprise Applications" developed quite a long time back, by Sun. AFAIK, only the Netbeans IDE implements this to some (or all) extent.

At runtime, all resources ought to be in the META-INF directory. There may be sub-directories, but I think there is enough agreement in this area, unlike source code organization.


i don't know of any conventions. personally i put all my stuff in a data/ directory.

even if there is a package called like this, chances are very small that there is a file present which conflicts with class files.


If you use maven, it encourages/forces you to use well-adopted project structure (single WAR module):

.
├── pom.xml
└── src
    └── main
    ├── java
    │   └── com
    │       └── example
    │           └── Test.java
    ├── resources
    │   └── com
    │       └── example
    │           └── data.csv
    └── webapp
        ├── public.png
        └── WEB-INF
            ├── hidden.jsp
            └── web.xml

Few hints: placing data.csv in the same directory as Test.java package allows you to easily open this file inside Test.java (and keeping everything in order since data files are in multiple directories):

getClass().getResource("data.csv");  //will only work in com.example package

Maven will put /src/main/resources contents on your CLASSPATH automatically. /webapp subdirectory is only used for WAR files.

0

精彩评论

暂无评论...
验证码 换一张
取 消