Java应用程序配置文件的最佳实践是什么?

时间:2023-01-12 23:44:13

Is there a best practice for where configuration files should be stored in a Java project. The file type is a Java properties file in this case, but I do use other file types in other projects.

是否存在配置文件应存储在Java项目中的最佳实践。在这种情况下,文件类型是Java属性文件,但我在其他项目中使用其他文件类型。

Would the recommendation vary from stand alone application(.jar) to web app(.war)?

建议会从独立应用程序(.jar)到Web应用程序(.war)不等吗?

3 个解决方案

#1


You'll find that many open-source projects follow the directory structure used by Maven. In this setup your application source code is kept in src/main/java, application resources, including properties files, in src/main/resources, and other config files in src/main/config. Files related to unit tests use a similar directory structure; src/test/java and src/test/resources.

您会发现许多开源项目都遵循Maven使用的目录结构。在此设置中,您的应用程序源代码保存在src / main / java,应用程序资源(包括属性文件),src / main / resources以及src / main / config中的其他配置文件中。与单元测试相关的文件使用类似的目录结构; src / test / java和src / test / resources。

Personally I tend to use this layout because of its widespread use. I also keep an "etc" directory beneath the project root to house files that aren't directly related to the application. For example, I keep configuration files for PMD and Checkstyle in etc.

我个人倾向于使用这种布局,因为它的广泛使用。我还在项目根目录下面保留了一个“etc”目录,用于容纳与应用程序没有直接关系的文件。例如,我保留了PMD和Checkstyle的配置文件等。

#2


In general a common practice is to have a resources directory for configuration files which is copied into the build artifact by the build process. Maven uses this in its default project structure. Within the resources directory, you might also have a META-INF directory and/or a WEB-INF directory in an application packaged as a war.

通常,通常的做法是为配置文件提供资源目录,该目录由构建过程复制到构建工件中。 Maven在其默认项目结构中使用它。在资源目录中,您可能还在打包为战争的应用程序中具有META-INF目录和/或WEB-INF目录。

#3


I use:

  • META-INF/ for jar files
  • META-INF /用于jar文件

  • WEB-INF/ for war files
  • WEB-INF /用于战争文件

#1


You'll find that many open-source projects follow the directory structure used by Maven. In this setup your application source code is kept in src/main/java, application resources, including properties files, in src/main/resources, and other config files in src/main/config. Files related to unit tests use a similar directory structure; src/test/java and src/test/resources.

您会发现许多开源项目都遵循Maven使用的目录结构。在此设置中,您的应用程序源代码保存在src / main / java,应用程序资源(包括属性文件),src / main / resources以及src / main / config中的其他配置文件中。与单元测试相关的文件使用类似的目录结构; src / test / java和src / test / resources。

Personally I tend to use this layout because of its widespread use. I also keep an "etc" directory beneath the project root to house files that aren't directly related to the application. For example, I keep configuration files for PMD and Checkstyle in etc.

我个人倾向于使用这种布局,因为它的广泛使用。我还在项目根目录下面保留了一个“etc”目录,用于容纳与应用程序没有直接关系的文件。例如,我保留了PMD和Checkstyle的配置文件等。

#2


In general a common practice is to have a resources directory for configuration files which is copied into the build artifact by the build process. Maven uses this in its default project structure. Within the resources directory, you might also have a META-INF directory and/or a WEB-INF directory in an application packaged as a war.

通常,通常的做法是为配置文件提供资源目录,该目录由构建过程复制到构建工件中。 Maven在其默认项目结构中使用它。在资源目录中,您可能还在打包为战争的应用程序中具有META-INF目录和/或WEB-INF目录。

#3


I use:

  • META-INF/ for jar files
  • META-INF /用于jar文件

  • WEB-INF/ for war files
  • WEB-INF /用于战争文件