[英]Where to put properties file (in connection with maven assembly)
When I start my application within eclipse, it's no problem to have a properties file within the src/main/resources
folder. 当我在eclipse中启动我的应用程序时,在
src/main/resources
文件夹中有一个属性文件是没有问题的。 When I then run maven install
, this properties file is put into a config
folder with the help of maven assembly. 然后,当我运行
maven install
,该属性文件将在maven程序集的帮助下放入config
文件夹中。 So following issue: 所以下面的问题:
While working in eclipse, I access the properties file simply with: 在Eclipse中工作时,我只需使用以下命令即可访问属性文件:
File propsFile = new File( "src/main/resources/settings.properties" );
After maven install
I cannot reach this file anymore of course. maven install
之后,我当然再也无法访问该文件了。 But which workflow is the best, to reach the file during my eclipse development and after maven install
without having it twice?! 但是,在我的Eclipse开发期间和
maven install
之后,两次都不需要文件访问文件,哪种工作流程是最好的?
My assembly structure after maven install
looks like this: 在
maven install
之后,我的程序集结构如下所示:
application folder
|----bin
|----batch file to start my application
|----lib
|----my application jar
|----other dependencies jars
|----config
|----settings.properties
Thanks a lot in advance already :) 提前非常感谢:)
If you are launching your app through a script file, I'd suggest you to include into the classpath the folder where settings.properties
is located at: 如果您是通过一个脚本文件,启动应用程式,我建议你要加入到类路径,其中的文件夹
settings.properties
位于:
In Eclipse you have to do nothing at all, because all the files from src\\main\\resources
are actually copied right into the target\\classes
output directory, which is indeed at the heading of the classpath. 在Eclipse中,您根本不需要执行任何操作,因为
src\\main\\resources
中的所有文件实际上都被复制到了target\\classes
输出目录中,该目录实际上位于类路径的标题处。
And in your script file, you have only to pre-pend the <application>\\config
file at the beginning of the execution classpath. 并且,在脚本文件中,您只需在执行类路径的开头添加
<application>\\config
文件即可。
But then you have to change the way you read resources from program: Replace using the File
API by ClassLoader.getResourceAsStream
: 但是,然后您必须更改从程序读取资源的方式:使用
ClassLoader.getResourceAsStream
使用File
API进行替换:
InputStream input=getClass().getClassLoader().getResourceAsStream("settings.properties");
Properties properties=new Properties(input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.