如何使用 Maven 或 pom.xml 更新 属性 文件
how to update a property file using maven or pom.xml
我创建了一个自动化框架,我在其中读取 属性 文件中的值,例如 "config.properties".
我的 config.propertioes 文件包含以下内容:
BrowserName=${browser}
Environment=${env}
我正在从 属性 文件读取浏览器值并将其传递到我的 selenium 脚本以 运行 它。
现在我想使用 pom.xml 将“${browser}"
&& “${env}"
替换为值“ie
” && “IT"
”。有没有 way/plugin 使用它我可以使用 pom.xml.
编辑 属性 文件
求推荐。
@Keshava
我将整个示例放在下面,如下所示:
1.Have 2 属性 个文件:◦project.properties:这是我们在存储库中提交的文件。它包含如下数据: ◾project.connection.username=@@DB_USERNAME@@
project.connection.password=@@DB_PASSWORD@@
◦build.properties:这是我们不在存储库中提交并在每个部署环境中维护的文件,无论是开发环境、UAT 环境还是生产环境。这个文件的内容如下: ◾db.username=mydbuser
db.password=我的数据库密码
2.In项目的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
你可以尝试使用 maven replacer 插件
参见 https://code.google.com/archive/p/maven-replacer-plugin/
看例子here
您好,您可以使用maven resource plugin。
此插件实现“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>
我创建了一个自动化框架,我在其中读取 属性 文件中的值,例如 "config.properties".
我的 config.propertioes 文件包含以下内容:
BrowserName=${browser}
Environment=${env}
我正在从 属性 文件读取浏览器值并将其传递到我的 selenium 脚本以 运行 它。
现在我想使用 pom.xml 将“${browser}"
&& “${env}"
替换为值“ie
” && “IT"
”。有没有 way/plugin 使用它我可以使用 pom.xml.
求推荐。
@Keshava 我将整个示例放在下面,如下所示: 1.Have 2 属性 个文件:◦project.properties:这是我们在存储库中提交的文件。它包含如下数据: ◾project.connection.username=@@DB_USERNAME@@ project.connection.password=@@DB_PASSWORD@@
◦build.properties:这是我们不在存储库中提交并在每个部署环境中维护的文件,无论是开发环境、UAT 环境还是生产环境。这个文件的内容如下: ◾db.username=mydbuser db.password=我的数据库密码
2.In项目的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
你可以尝试使用 maven replacer 插件 参见 https://code.google.com/archive/p/maven-replacer-plugin/
看例子here
您好,您可以使用maven resource plugin。
此插件实现“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>