如何将Maven配置文件值注入属性文件 [英] How to inject Maven profile value into properties file
问题描述
有点卡在这里.我有一个带有3个配置文件的pom. Theese配置文件具有不同的版本名称.我要在构建特定配置文件时将该版本名称注入属性文件.
Little stuck here. I have a pom with 3 profiles. Theese profiles have different version name. I want to inject that version name into properties file when a specific profile is building.
我的个人资料:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>DEV</projectVersion>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-RC1</projectVersion>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
</profile>
</profiles>
和filter.properties看起来像这样:
and filter.properties looks like this:
projectName = defaultName
versionName = defaultVersion
该怎么做?我通过命令构建项目:
How to do that? Im building project by command:
mvn clean install -D profile_name
推荐答案
您需要做的是在POM文件的<build>
部分中添加一个新部分.
What you need to do is to add a new section to your <build>
section of your POM file.
赞:
<build>
<resources>
<resource>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
这将查找指定文件**/*.properties
上指定文件夹(src/main/resources
)的内部,并在遇到定义的变量时更改文件.
This will look inside the specified folder (src/main/resources
) on the specified files **/*.properties
and change the files when it encounters defined variables.
因此,要进行此项工作,您的属性文件必须采用以下方式:
So in order to this work your propertie file must be this way:
projectName = ${defaultName}
versionName = ${defaultVersion}
请注意这些变量的名称. Maven将用您定义的名称替换它,或者Maven结构的名称(例如$ {projectVersion})将被pom文件的<version>1.0</version>
标记替换.
Be aware with these variables name. Maven will replace it with the defined names by you or the names of the Maven structure like ${projectVersion} will be replaced by the <version>1.0</version>
tag of your pom file.
所以不要使用:
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
将此变量的名称(和版本)更改为其他名称,例如:
Change the name (and the version) of this variable to something else like:
<properties>
<defaultVersion>1.0.0-Final</defaultVersion>
<defaultName>someName</defaultName>
</properties>
在所有个人资料上.
然后只需以以下方式运行您的maven命令:
And just run your maven command as:
mvn install -Pprofilename
这篇关于如何将Maven配置文件值注入属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!