Java 如何从Jenkin管道groovy脚本更新配置属性文件?

Java 如何从Jenkin管道groovy脚本更新配置属性文件?,java,selenium-webdriver,jenkins-pipeline,maven-2,jenkins-groovy,Java,Selenium Webdriver,Jenkins Pipeline,Maven 2,Jenkins Groovy,我是詹金管道的新手。我们有一个基于maven的SeleniumJava自动化框架。我正在创建一个Jenkin管道groovy脚本来调用自动化框架 在这个框架中,我们有config.properties文件,其中存储了应用程序url、用户名和密码 config.properties: 网址= 用户名=######## 密码=######## 要求:我们需要将应用程序URL、用户名和密码作为Jenkin参数,并相应地运行自动化套件 问题:从管道groovy脚本如何在运行时更新config.prope

我是詹金管道的新手。我们有一个基于maven的SeleniumJava自动化框架。我正在创建一个Jenkin管道groovy脚本来调用自动化框架

在这个框架中,我们有config.properties文件,其中存储了应用程序url、用户名和密码

config.properties:

网址=

用户名=########

密码=########

要求:我们需要将应用程序URL、用户名和密码作为Jenkin参数,并相应地运行自动化套件

问题:从管道groovy脚本如何在运行时更新config.properties文件?是否有可能在框架内创建一个java类来更新配置文件并从groovy脚本调用java类

我尝试了以下代码

node{ 
  stage("props file"){ 
    script { 
      def props = """Url=https://#########/login.jsp Username=######## 
                     Password=########""" 
      writeFile interpolate: true ,file: 'ui-automation/fw/config/config.properties', text: props 
      def str = readFile file: 'ui-automation-fw/config/config.properties' 
      echo str
    } 
   }
 } 
感谢您提供有关如何修复代码以获得所需结果的帮助

使用步骤编写文件

下面的示例写入和读取文件
config.properties

pipeline {
   agent any

   stages{
     stage("props file"){
        steps{
            script {

                def props = """Url=https://#########/login.jsp
Username=########
Password=########"""
                writeFile file: "config.properties", text: props
                def str =  readFile file: "config.properties"
                echo str

            }
         }
      }
   }
}
更新:
如果属性文件已存在,则可以使用步骤加载属性

请分享你的工作。你好。解决方法是使用maven命令中的参数,但pom.xml.Hello Yuri中的变量除外。谢谢你的回复。Config.properties文件位于我的框架内。Jenkin build首先在工作区中从git中提取源代码。我需要在脚本中提供属性文件的完整路径吗?@sudhirranjanSwain我已经添加了readProperties步骤的链接。请阅读文档并尝试实施您的解决方案。如果您有任何问题,请编辑您的问题,并提供更多详细信息。伟大的谢谢。这很有效。只有一个问题。此解决方案覆盖整个文件。如果我只想更新这三个值而不接触config.properties.node{stage(“props file”){script{def props=“””Url=https://writeFile interpolate:true,file:'ui automation fw/config/config.properties',text:props def str=readFile:'ui automation fw/config/config.properties'echo str}}}--这将覆盖所有内容并添加以上三行。但我只想更新这些键的值。因为我的配置文件包含其他keys@sudhirranjanSwain也许是这样: