如何使用maven或pom.xml更新属性文件

问题描述 投票:0回答:3

我创建了一个自动化框架,在其中读取属性文件中的值,例如“config.properties”。

我的 config.propertioes 文件包含以下内容:

BrowserName=${browser}
Environment=${env}

我正在从属性文件中读取浏览器值并将其传递给我的 selenium 脚本来运行它。

现在我想使用 pom.xml 将“

${browser}"
&&”
${env}"
替换为值“
ie
”&&“
IT"
”。有没有什么方法/插件可以使用 pom.xml 编辑属性文件.

请推荐。

@Keshava 我将整个示例放在下面,如下所示: 1.有2个属性文件: ◦project.properties:这是我们在存储库中提交的文件。它包含如下数据: ◾project.connection.username=@@DB_USERNAME@@ project.connection.password=@@DB_PASSWORD@@

◦build.properties:这是我们不在存储库中提交的文件,而是在每个部署环境中维护,无论是开发人员环境、UAT 环境还是生产环境。该文件内容如下: ◾db.username=mydbuser db.password=mydbpassword

2.在项目的pom.xml中添加以下插件和执行:

<plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <version>1.3.5</version>
                <executions>
                    <execution>
                        <id>replaceTokens</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>target/classes/project.properties</file>
                    <replacements>
                        <replacement>
                            <token>@@DB_USERNAME@@</token>
                            <value>${db.username}</value>
                        </replacement>
                        <replacement>
                            <token>@@DB_PASSWORD@@</token>
                            <value>${db.password}</value>
                        </replacement>
                    </replacements>
                </configuration>
</plugin>

从上面,我明白

"@@DB_USERNAME@@"
来自
"project.properties"
。但是,这个
"${db.username}"
值将从哪个属性文件中获取?我的 pom 如何理解从哪里获取
"${db.username}"
.

我是否需要在 Maven 目标中传递这个值,如下所示:

mvn clean install -Ddb.username=myuserid
java maven pom.xml
3个回答
4
投票

您好,您可以使用 maven 资源插件

这个插件实现了“Maven Filtering”。

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <id>selenium-profile-chrome</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/selenium</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

1
投票

你可以尝试使用 Maven Replacer 插件 请参阅https://code.google.com/archive/p/maven-replacer-plugin/

查看示例此处


0
投票

maven 本质上也有可解析、可继承和可覆盖的部分。您可以一起使用 Maven 配置文件来实现任何可能的解决方案。您要搜索的关键字可能是“使用 Maven 配置文件管理 Maven 属性”

© www.soinside.com 2019 - 2024. All rights reserved.