在Maven配置文件中使用properties标记 [英] Using the properties tag within maven profiles
问题描述
我指的是" Maven:完整参考",尤其是有关配置文件的部分,该部分在此处记录了在<profile...
标记内使用<properties...
标记的情况:请参阅此处
I am in reference to "Maven: The Complete Reference" and especially the section regarding profiles which documents the use of a <properties...
tag within the <profile...
tag here: see here
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>environment.type</name>
<value>dev</value>
</property>
</activation>
<properties>
<database.driverClassName>com.mysql.jdbc.Driver</database.driverClassName>
<database.url>
jdbc:mysql://localhost:3306/app_dev
</database.url>
<database.user>development_user</database.user>
<database.password>development_password</database.password>
</properties>
</profile>
我不确定运行mvn install -Denvironment.type=dev
命令时会发生什么:
What I am not sure about is what happens when the mvn install -Denvironment.type=dev
command is run:
- 这会创建一个
.properties
文件吗? - 如果不是这样,那么在开发人员中测试该应用程序时,tomcat(例如)将如何以及在何处读取各个属性?
- Will this create a
.properties
file? - If not how and where will tomcat (for instance) read the individual properties when the app is tested in dev?
推荐答案
这会创建一个.properties文件吗?
Will this create a .properties file?
不,不会.这将设置maven使用的属性.也就是说,对于mvn install -Denvironment.type=development
,maven会将变量"database.user"(可以在poms和过滤的资源中用作$ {database.user}的变量)使用值"development_user".
No, it won't. This would set the properties used by maven. This is, with mvn install -Denvironment.type=development
maven would use the value 'development_user' for the variable 'database.user' (that you can use as ${database.user} in poms and filtered resources).
如果没有,例如,当在开发人员中测试该应用程序时,tomcat将如何(例如)在何处读取各个属性?
If not how and where will tomcat (for instance) read the individual properties when the app is tested in dev?
要做的是告诉Maven根据配置文件(properties.files)来过滤(和修改)您要自定义的资源.
The thing is to tell maven to filter (and modify) the resources that you want to customize depending on the profile (properties.files).
因此,首先您必须说maven来过滤资源:
So, first you have to say maven to filter the resources:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
然后修改您的属性文件以使用maven变量.例如,您的数据库属性文件如下所示:
Then modify your properties files to use maven variables. For example, your db properties file would look like this:
database.driverClassName=${database.driverClassName}
database.url=${database.url}
#...
这篇关于在Maven配置文件中使用properties标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!