[英]where to put the properties file in an eclipse project
Please tell me where exactly do i put my .properties file in my eclipse project. 请告诉我将.properties文件放在Eclipse项目中的确切位置。 Do I make a separate folder for it ?
我要为此单独制作一个文件夹吗? I want to put it in such a way that I will be able to distribute my project easily in JAR form.
我希望以一种可以轻松以JAR形式分发我的项目的方式来放置它。
Thanks. 谢谢。
EDIT: 编辑:
I want to put the properties file in such a way that it can be easily edited later, on any OS. 我希望将属性文件放置在以后可以在任何OS上轻松编辑的方式。
The approach I use in order to be able to easily change configuration without redeployment . 我使用的方法是为了能够轻松更改配置而无需重新部署 。
One version of the property file (with the defaults) in the root of your project, I always let it in the application package (foo.bar.myapp). 在项目根目录中的属性文件的一个版本(具有默认值),我总是将其放在应用程序包(foo.bar.myapp)中。 I load the default one with
getResourceAsStream("/foo/bar/myapp/config.properties")
or like in the sample relative to the class package. 我使用
getResourceAsStream("/foo/bar/myapp/config.properties")
加载默认的,或者在相对于类包的示例中加载默认的。
And additionally I ready a system property: 另外,我准备了一个系统属性:
String configFileLocation = System.getProperty("config");
And just override the default with the properties read from the config file passed as property. 并使用从作为属性传递的配置文件中读取的属性覆盖默认值。
For instance: 例如:
Properties props = new Properties();
props.load(Main.class.getResourceAsStream("mydefault.properties"));
System.out.println("default loaded: " + props);
String configFile = System.getProperty("config");
if (configFile != null) {
props.load(new FileInputStream(configFile));
System.out.println("custom config loaded from " + configFile);
}
System.out.println("custom override: " + props);
This would load first your resource stored under your project in foo.bar
package named mydefault.properties
, and after it if system property config
is configured it will load override the loaded properties with the one the the referred path. 这将首先加载存储在名为
mydefault.properties
foo.bar
程序包中的项目下的资源,在此之后,如果配置了系统属性config
,它将使用引用路径替换加载的属性。
The system property can be set using -D
parameter, in this case would be something like: java -Dconfig=/home/user/custom.properties foo.bar.Main
. 可以使用
-D
参数设置系统属性,在这种情况下,将类似于: java -Dconfig=/home/user/custom.properties foo.bar.Main
。 Or if you are in a web application (Tomcat for instance) you can set this property using CATALINA_OPTS
. 或者,如果您在Web应用程序(例如Tomcat)中,则可以使用
CATALINA_OPTS
设置此属性。
src/main/resource
是一个理想的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.