Как удалить определенные элементы из pom.xml через командную строку или используя python / perl / sed / awk? - PullRequest
0 голосов
/ 08 января 2019

Я хочу удалить элементы, содержащие определенные значения, из файла pom.xml. Мне нужно автоматизировать это, чтобы я мог использовать это на Дженкинс. У меня есть следующий pom.xml, и мне нужно удалить все в <plugin>, в котором <artifactId> равно tycho-packaging-plugin.

Я пытался использовать регулярное выражение, и следующее регулярное выражение работало в онлайн-валидаторе, но когда я использовал его для всего файла pom.xml, он не удаляет этот фрагмент кода: https://regex101.com/r/734O5h/1/

<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>org.eclipse.lsp4e</groupId>
<artifactId>parent</artifactId>
<version>0.8.0-SNAPSHOT</version>
<packaging>pom</packaging>

<prerequisites>
    <maven>3.1.1</maven>
</prerequisites>

<properties>
    <tycho-version>1.3.0-SNAPSHOT</tycho-version>
    <tycho-extras-version>${tycho-version}</tycho-extras-version>
    <sonar.jacoco.reportPath>../target/jacoco.exec</sonar.jacoco.reportPath>
    <tycho.scmUrl>scm:git:https://git.eclipse.org/r/p/lsp4e/lsp4e.git</tycho.scmUrl>
    <ui.test.vmargs></ui.test.vmargs>
    <target-config>target-platform-latest</target-config>
</properties>

<modules>
    <module>target-platforms/target-platform-photon</module>
    <module>target-platforms/target-platform-latest</module>
    <module>org.eclipse.lsp4e</module>
    <module>org.eclipse.lsp4e.debug</module>
    <module>org.eclipse.lsp4e.tests.mock</module>
    <module>org.eclipse.lsp4e.test</module>
    <module>repository</module>
</modules>

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-maven-plugin</artifactId>
            <version>${tycho-version}</version>
            <extensions>true</extensions>
        </plugin>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>target-platform-configuration</artifactId>
            <version>${tycho-version}</version>
            <configuration>
                <includePackedArtifacts>true</includePackedArtifacts>
                <target>
                    <artifact>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${target-config}</artifactId>
                        <version>0.8.0-SNAPSHOT</version>
                    </artifact>
                </target>
                <environments>
                    <environment>
                        <os>win32</os>
                        <ws>win32</ws>
                        <arch>x86_64</arch>
                    </environment>
                    <environment>
                        <os>linux</os>
                        <ws>gtk</ws>
                        <arch>x86_64</arch>
                    </environment>
                    <environment>
                        <os>macosx</os>
                        <ws>cocoa</ws>
                        <arch>x86_64</arch>
                    </environment>
                </environments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-source-plugin</artifactId>
            <version>${tycho-version}</version>
            <executions>
                <execution>
                    <id>plugin-source</id>
                    <goals>
                        <goal>plugin-source</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-packaging-plugin</artifactId>
            <version>${tycho-version}</version>
            <configuration>
                <sourceReferences>
                    <generate>true</generate>
                </sourceReferences>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.tycho.extras</groupId>
                    <artifactId>tycho-sourceref-jgit</artifactId>
                    <version>${tycho-extras-version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.eclipse.tycho.extras</groupId>
            <artifactId>tycho-p2-extras-plugin</artifactId>
            <version>${tycho-extras-version}</version>
            <executions>
                <!-- Make sure we don't forget to bump version -->
                <execution>
                    <id>compare-version-with-baseline</id>
                    <goals>
                        <goal>compare-version-with-baselines</goal>
                    </goals>
                    <configuration>
                        <baselines>
                            <url>http://download.eclipse.org/lsp4e/releases/latest</url>
                        </baselines>
                        <comparator>zip</comparator>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <goals><goal>prepare-agent</goal></goals>
                </execution>
            </executions>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.reportPath}</destFile>
                <includes>
                    <include>org.eclipse.lsp4e*</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-packaging-plugin</artifactId>
                <version>${tycho-version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.eclipse.tycho.extras</groupId>
                        <artifactId>tycho-buildtimestamp-jgit</artifactId>
                        <version>${tycho-extras-version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <timestampProvider>jgit</timestampProvider>
                    <jgit.ignore>pom.xml</jgit.ignore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-p2-plugin</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <baselineRepositories>
                        <repository>
                            <url>http://download.eclipse.org/lsp4e/snapshots</url>
                        </repository>
                    </baselineRepositories>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>packAndSign</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.tycho.extras</groupId>
                    <artifactId>tycho-pack200a-plugin</artifactId>
                    <version>${tycho-extras-version}</version>
                    <executions>
                        <execution>
                            <id>pack200-normalize</id>
                            <phase>package</phase>
                            <goals>
                                <goal>normalize</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.cbi.maven.plugins</groupId>
                    <artifactId>eclipse-jarsigner-plugin</artifactId>
                    <version>1.1.3</version>
                    <executions>
                        <execution>
                            <id>sign</id>
                            <phase>package</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.tycho.extras</groupId>
                    <artifactId>tycho-pack200b-plugin</artifactId>
                    <version>${tycho-extras-version}</version>
                    <executions>
                        <execution>
                            <id>pack200-pack</id>
                            <phase>package</phase>
                            <goals>
                                <goal>pack</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.tycho</groupId>
                    <artifactId>tycho-p2-plugin</artifactId>
                    <version>${tycho-extras-version}</version>
                    <executions>
                        <execution>
                            <id>p2-metadata</id>
                            <phase>package</phase>
                            <goals>
                                <goal>p2-metadata</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <defaultP2Metadata>false</defaultP2Metadata>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <!-- Automatic profile for Mac-specific settings -->
    <profile>
        <id>macos</id>
        <activation>
            <os>
                <family>mac</family>
            </os>
        </activation>
        <properties>
            <ui.test.vmargs>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts -Djava.awt.headless=false</ui.test.vmargs>
        </properties>
    </profile>
</profiles>

<pluginRepositories>
    <pluginRepository>
        <id>cbi</id>
        <url>https://repo.eclipse.org/content/groups/cbi</url>
        <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>tycho-snapshots</id>
        <url>https://repo.eclipse.org/content/repositories/tycho-snapshots/</url>
    </pluginRepository>
</pluginRepositories>

<distributionManagement>
    <snapshotRepository>
        <id>jboss-snapshots-repository</id>
        <name>JBoss Snapshots Repository</name>
        <url>https://repository.jboss.org/nexus/content/repositories/snapshots/</url>
        <uniqueVersion>false</uniqueVersion>
    </snapshotRepository>
</distributionManagement>

Python-код для удаления

#!/usr/bin/python
import re
with open('lsp4e/pom.xml', 'r') as f:
  text = f.read()
  text = re.sub(r"\s+", "", text)
  text = re.sub("<plugin><groupId>org\.eclipse\.tycho<\/groupId><artifactId>tycho-packaging-plugin<\/artifactId><version>\${tycho-version}<\/version><configuration><sourceReferences><generate>true<\/generate><\/sourceReferences><\/configuration><dependencies><dependency><groupId>org\.eclipse\.tycho\.extras<\/groupId><artifactId>tycho-sourceref-jgit<\/artifactId><version>\${tycho-extras-version}<\/version><\/dependency><\/dependencies><\/plugin><plugin>", "", text)
  print(text)
  with open('lsp4e/pom.xml', 'w') as f:
        f.write(text)

Я ожидал, что <plugin> с artifactId tycho-packaging-plugin будет удален, но это не так.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...