Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Maven将源file.properties中的占位符替换为另一个file.properties_Maven_Pom.xml - Fatal编程技术网

如何使用Maven将源file.properties中的占位符替换为另一个file.properties

如何使用Maven将源file.properties中的占位符替换为另一个file.properties,maven,pom.xml,Maven,Pom.xml,我正在学习如何用Maven将一个文件属性的占位符替换为另一个文件属性。我的项目有一个名为testMaven的主文件夹,我在项目中创建了两个文件属性,其中第一个文件位于testMaven->local->value.properties中,文件中有: user=testUser 密码=测试密码 相反,位于src/main/java->test.properties中的第二个文件属性是: user=@user@ 密码=@password@ 我希望在文件Jar中有一个文件属性,其字段替换为: user

我正在学习如何用Maven将一个文件属性的占位符替换为另一个文件属性。我的项目有一个名为testMaven的主文件夹,我在项目中创建了两个文件属性,其中第一个文件位于testMaven->local->value.properties中,文件中有:

user=testUser 密码=测试密码

相反,位于src/main/java->test.properties中的第二个文件属性是:

user=@user@ 密码=@password@

我希望在文件Jar中有一个文件属性,其字段替换为: user=testUser 密码=测试密码

如何在pom.xml文件中写入上述描述?
谢谢

您需要使用两个插件:

  • 使用要使用的值读取属性文件
  • 替换
他们两人的可用文档都很简单

更新 给定一个custom.properties文件:

custom.user=testUser
custom.password=testPassword

请阅读以下内容:


org.codehaus.mojo
属性maven插件
${properties maven version}
初始化
读取项目属性
${jbake.inputDirectory}/custom.properties
现在,您需要为每个属性运行替换程序:


com.google.code.maven-replacer-plugin
替代者
${replacer maven版本
替换文件
准备包装
代替
真的
${basedir}/src/main/resources
${basedir}
总工程师/地盘
真的
假的
@
*.物业
用户
${custom.user
密码
${custom.password}

在src/main/resources下的属性文件中出现的任何@user@或@password@都将被替换。生成的文件将位于src/site

下,但我不知道如何替换另一个文件中的占位符。好的,谢谢您的回答。但是我希望自动替换占位符,因此我使用了:您的问题有点混乱唱歌,但我相信你说的是过滤器。请看一个例子:
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>${properties-maven-version}</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                      <files><file>${jbake.inputDirectory}/custom.properties</file>
                      </files> 
                    </configuration>
                </execution>
            </executions>
        </plugin>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>${replacer-maven-version</version>
                <executions>
                    <execution>
                        <id>replace-for-documentation</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                        <configuration>
                            <preserveDir>true</preserveDir>
                            <basedir>${basedir}/src/main/resources</basedir>
                            <outputBasedir>${basedir}</outputBasedir>
                            <outputDir>src/site</outputDir>
                            <ignoreErrors>true</ignoreErrors>
                            <regex>false</regex>
                            <delimiters>
                                <delimiter>@</delimiter>
                            </delimiters>
                            <filesToInclude>
                                *.properties
                            </filesToInclude>
                            <replacements>
                                <replacement>
                                    <token>user</token>
                                    <value>${custom.user</value>
                                </replacement>
                                <replacement>
                                    <token>password</token>
                                    <value>${custom.password}</value>
                                </replacement>
                            </replacements>
                        </configuration>
                    </execution>
                </executions>
            </plugin>