Powershell和windows命令执行sed操作

mrwjdhj3  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(176)

我有shell命令,它将在整个项目的所有pom文件中替换pom版本,我试图为powershell和windows获得等效的命令,它可以执行所需的操作。
shell命令:

find . -name "pom.xml" -exec sed -i s/1.0.1/2.0.0/g {} +

字符串
pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.foo.primo</groupId>
        <artifactId>primo-parent</artifactId>
        <version>1.0</version>
    </parent>
    <groupId>com.foo</groupId>
    <artifactId>core</artifactId>
    <version>1.0.1</version>
    <packaging>pom</packaging>
    <name>foo</name>
    <profiles>
        <profile>
            <id>profile_1</id>
            <modules>
                <module>module_1</module>
            </modules>
        </profile>


在执行shell command之后,我的pom.xml将如下所示。
版本替换后的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.foo.primo</groupId>
        <artifactId>primo-parent</artifactId>
        <version>1.0</version>
    </parent>
    <groupId>com.foo</groupId>
    <artifactId>core</artifactId>
    <version>2.0.0</version>
    <packaging>pom</packaging>
    <name>foo</name>
    <profiles>
        <profile>
            <id>profile_1</id>
            <modules>
                <module>module_1</module>
            </modules>
        </profile>


版本替换将发生在所有pom.xml文件中,甚至在子模块级别也存在。
已尝试构造powershell命令,但它无法按预期工作。它仅在单个pom.xml文件中执行sed操作,但版本仍未正确替换。
我尝试过的powershell命令:

Get-ChildItem -Path . -Filter pom.xml | ForEach-Object {
  $content = Get-Content $_.FullName
  $content = $content -replace "VERSION", $version
  Set-Content $_.FullName $content
}

pinkon5k

pinkon5k1#

正如评论中所建议的,使用字符串操作(如-replace)来查看和/或插入结构化文本是一个坏主意,而是使用专用的(xml)解析器。
要更改xml文件中的版本,请执行以下操作:

$Content = @'
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.foo.primo</groupId>
        <artifactId>primo-parent</artifactId>
        <version>1.0</version>
    </parent>
    <groupId>com.foo</groupId>
    <artifactId>core</artifactId>
    <version>1.0.1</version>
    <packaging>pom</packaging>
    <name>foo</name>
    <profiles>
        <profile>
            <id>profile_1</id>
            <modules>
                <module>module_1</module>
            </modules>
        </profile>
    </profiles>
</project>
'@

$Xml = [xml]$Content
$Xml.project.version = '2.0.1'

个字符
对于您的脚本,它可能看起来像这样:

Get-ChildItem -Path . -Filter pom.xml | ForEach-Object {
  $Content = Get-Content $_.FullName -Raw
  $Xml= [xml]$Content
  $Xml.project.version = $Version
  $Xml.Save($_.FullName)
}

相关问题