【发布时间】:2016-06-08 12:40:57
【问题描述】:
我正在尝试使用脚本编辑我的 pom.xml 文件。它涉及在我期望存在的插件模块之后插入一个插件模块。
我的简化 pom 看起来像这样:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>very</groupId>
<artifactId>secret</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Something</name>
<properties>
...
</properties>
<modules>
<module>...</module>
</modules>
<prerequisites>
...
</prerequisites>
<profiles>
<profile>
...
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
...
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
...
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
...
</plugin>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<executions>
<execution>
<id>Generate JRebel configuration</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<relativePath>${relativeRoot}</relativePath>
<rootPath>$${webapp.jrebel.root}</rootPath>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
...
</plugin>
</plugins>
</reporting>
</project>
我想使用脚本在 zeroturnaround 之后添加另一个插件。所以基本上我正在寻找这种模式:
<rootPath>$${webapp.jrebel.root}</rootPath>
</configuration>
</plugin>
并且想在这个模式之后插入一些东西。所以输出应该是
<rootPath>$${webapp.jrebel.root}</rootPath>
</configuration>
</plugin>
Something new here
sed 不起作用,因为输入是一行一行的。所以这个
sed '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/a Something new here' pom.xml
打印出来
<rootPath>$${webapp.jrebel.root}</rootPath>
Something new here
</configuration>
</plugin>
我试过了
sed -i -e '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/ {
N; /\n<\/configuration>/ {
N; /\n<\/plugin>/ {
s/<\/plugin>/<\/plugin>hello/
}
}
}' pom.xml
但这无济于事。
如何进行模式匹配?我愿意使用 sed 或 awk。
【问题讨论】:
-
股票建议:不要使用
sed等面向行的工具操作 XML 数据。请改用xmlstarlet或xsltproc之类的东西。 -
好点 - 我现在正在研究 xmllint
-
xmllint 不用于编辑。看起来我需要按照您的建议使用 xmlstarlet @MichaelVehrs