English 中文(简体)
你如何组织Java项目中classpath中可用的非源资源?
原标题:
  • 时间:2008-12-08 14:59:55
  •  标签:

In a software-project written in java you often have resources, that are part of the project and should be included on the classpath. For instance some templates or images, that should be accessible through the classpath (getResource). These files should be included into a produced JAR-file.

It s clear that these resources should be added to the revision-control-system. But in which directory you put these files? Parallel to the java-source-files or in another directory that also reproduces the needed package-structure?

最佳回答

I prefer your latter solution: a separate directory that mimicks the source code’s package structure. This way they won’t clobber your source code but will be right next to the compiled class files in the packaged JAR file.

问题回答

Maven将它们放在src/main/resources/中(Java代码将放在src/main/java/中)。主要原因是Maven可以编译各种语言的代码,将它们分开保持清晰,避免由于其他文件而引起编译混淆。

在资源方面,Maven会在它们添加到Jar之前替换变量,因此它们也是“编译”源。

Personally, I d prefer having them together with the source code if they are strongly related to specific parts of the code, and if there are not too many of them.

But if it makes sense for those files to have their own organization structure, or if there are hundreds of files that would make it hard to find the source code amongst them, I d keep them separate.

It depends on your preferences. Having them mixed with the source code files makes refactorings like renaming of packages easier (as the ressources are moved toghether with the sources), but with more than a handful of icons, images and localization files, you get a clutter of files very easily.

With todays tools it also doesn t really matter. Whether you use Eclipse, Netbeans or another tool, they all allow you to have binary packages with a different layout than source code. So in the end you can do it like you want.

Personally I try to avoid mixing source with resources, because I typically change resources very seldom, but source code very frequently.

I usually go like this:

project/src
project/resources
project/classes
project/lib
project/dist

srcresources都有包结构,构建文件将classesresources作为jar的输入,该jar放置在dist目录下。

当在内部运行时,类路径类似于:lib;classes;resources;

如果应用程序有安装程序,则安装程序会创建并将资源目录完整地放置在安装中。

如果您的构建工具不会混淆,您可以将这些东西放入类路径中。Eclipse不会尝试编译您的JPEG文件,Ant也不会,因此没有太多可担心的。好处是如果您有许多相关的东西,可以将它们保存在一起,按功能组织,而不是按文件类型组织。

我們在我們工作的地方這樣做:我們有需要一些代碼和一些靜態資源的電子郵件消息。所有這些東西都被保存在java源路徑中的一個文件夾中。我們可以輕鬆地向系統添加和刪除郵件,而無需忘記任何東西或犯錯誤,例如src /資源路徑不匹配或在一個樹中的孤立文件不在另一個樹中。

As already said, it s up to you and may differ per project. For example, with Wicket, it s quite usual and practical to keep .html files right next to classes. Also, I often keep some configuration together with source code - META-INF, WEB-INF, logging.

为了让Maven从源代码树中获取资源,请使用这个:

<build>
   <resources>
     <resource>
         <directory>src/main/resources</directory>
     </resource>
     <!-- Web - Wicket -->
     <resource>
         <filtering>false</filtering>
         <directory>src/main/java</directory>
         <includes><include>**</include></includes>
         <excludes><exclude>**/*.java</exclude></excludes>
     </resource>
  </resources>

  <testResources>
     <testResource>
         <directory>src/test/resources</directory>
     </testResource>
     <!-- Web - Wicket -->
     <testResource>
         <filtering>false</filtering>
         <directory>src/main/java</directory>
         <includes><include>**</include></includes>
         <excludes><exclude>**/*.java</exclude></excludes>
     </testResource>
  </testResources>

保持资源分离的主要原因是(依我之见)工作角色的分离。例如,如果您有一个翻译团队的项目,您将把包含字符串的资源与代码分开,并使用不同的源代码管理权限。





相关问题
热门标签