【问题标题】:error while modifying pom.xml file using PowerShell使用 PowerShell 修改 pom.xml 文件时出错
【发布时间】:2023-01-11 19:32:08
【问题描述】:

我想将<module>features/com.bw.rest.runtime.feature</module>中的一些文本值修改为<module>features/com.bw.rest.runtime.feature.eclipse</module>。我无法使用 PowerShell 找到节点值 <module>features/com.bw.rest.runtime.feature.eclipse</module>。因为我想将模块下的所有模块值更新为字符串末尾的 .eclipse。下面是我要修改文本的完整代码部分。请帮忙。

<modelVersion>4.0.0</modelVersion>
<groupId>com.bw.hello.feature</groupId>
<groupId>com.bw.hello.feature</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
   <modules>
     <module>features/com.bw.rest.runtime.feature</module>
     <module>plugins/com.bw.rest.jaxrs.runtime</module>
     <module>plugins/com.bw.rest.runtime</module>
     <module>plugins/com.bw.rest.runtime.schema</module> 
     <module>plugins/com.bw.rest.swagger.runtime</module>
     <module>plugins/com.bw.swagger.model.client</module>
     </modules>

我试过下面的代码。但我不想一一更改我想用 .eclipse 更新模块中的所有字符串

$filePath = "C:\Users\Documents\example\temp1.xml"
$f = "features/com.bw.rest.runtime.feature"
Get-ChildItem $filePath -Recurse | ForEach-Object {
# Read the file and use replace()
(Get-Content $_).Replace("$f","features/com.bw.rest.runtime.feature.eclipse") | Set-Content $_

}

【问题讨论】:

  • 您可以添加您已经尝试编写的代码来解析 xml 吗?
  • @ThomErnst 我已经尝试使用下面的代码来替换模块部分下给出的特征字符串。但是想要在每个字符串末尾使用 .eclipse 扩展名更新功能和插件的 <Modules ><module> 下的所有字符串。 $filePath = "C:\Users\Documents\example\temp1.xml" $f = "com.bw.rest.runtime.feature" # 从文件夹中获取文件并使用 Foreach Get-ChildItem 进行迭代 $filePath -Recurse | ForEach-Object { # 读取文件并使用 replace() (Get-Content $_).Replace("$f","com.bw.rest.runtime.feature.eclipse") |设置内容 $_ }

标签: arrays xml powershell loops


【解决方案1】:

使用 Xml Linq:

using assembly System 
using assembly System.Linq
using assembly System.Xml.Linq 

$inputFilename = "c:	emp	est.xml"
$outputFilename = "c:	emp	est1.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($inputFilename)

$modules = $xDoc.Descendants("module")
foreach($module in $modules)
{
   $module.SetValue($module.Value + ".eclipse")
}
$xDoc.Save($outputFilename)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2022-08-11
    相关资源
    最近更新 更多