0

I am trying to automate a maven project with a list of plugins. I can get to the groupID, artifactID, and version and what the text reads but want to get rid of the {http://maven.apache.org/POM/4.0.0} that comes before the tag. I am not sure exactly what it is but I have outputted the child in root and that was displayed so that's why I used it in the if statements.

import xml.etree.ElementTree as ET

tree = ET.parse('pom.xml')
root = tree.getroot()

for child in root:
    if child.tag == '{http://maven.apache.org/POM/4.0.0}build':
        for Gchild in child:
            if Gchild.tag == '{http://maven.apache.org/POM/4.0.0}plugins':
                for achild in Gchild:
                    if achild.tag == '{http://maven.apache.org/POM/4.0.0}plugin':
                        for bchild in achild:
                            print (bchild.tag, bchild.text)

Output:

{http://maven.apache.org/POM/4.0.0}groupId org.apache.maven.plugins
{http://maven.apache.org/POM/4.0.0}artifactId maven-antrun-plugin
{http://maven.apache.org/POM/4.0.0}version 3.0.0
{http://maven.apache.org/POM/4.0.0}groupId org.apache.maven.plugins
{http://maven.apache.org/POM/4.0.0}artifactId maven-assembly-plugin
{http://maven.apache.org/POM/4.0.0}version 3.3.0

expected output:

groupId org.apache.maven.plugins
artifactId maven-antrun-plugin
version 3.0.0
groupId org.apache.maven.plugins
artifactId maven-assembly-plugin
version 3.3.0

the groupID, artifactID, and version will be used to check a list of plugins and see if there are any updated versions.

here is the POM.xml file.

<?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>merged</groupId>
  <artifactId>all-deps</artifactId>
  <version>1.0</version>
  <name>All dependencies merged</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.0.0</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version>
      </plugin>
    </plugins>
  </build>
</project>

joe
  • 1
  • 1
  • Why not use https://www.mojohaus.org/versions-maven-plugin/display-plugin-updates-mojo.html ? – J Fabian Meier Aug 03 '21 at 15:26
  • In my case all versions have to be approved. I don't want them updated unless approved. The idea I have is to have a list of plugins and translate that list to generate a POM.xml file. If any thing is added or changed on the list it would automatically go in and update the POM.xml to reflect the desired list. I need multiple POM.xml files (ex. Quarkus, generic). – joe Aug 03 '21 at 15:54
  • As already mentioned by @JFabianMeier you can check for updates with versions-maven-plugins. Create a branch and update the version..check via your builds and then make a pull request / review and merge it to your main branch... Apart from that that sounds like a job for a corporate parent which defines all the needed plugins... SNAPSHOT version checked on different projects and after release recheck and merge it than to the appropriate main branches... – khmarbaise Aug 03 '21 at 16:00
  • any ideas to answer the question i posted? – joe Aug 03 '21 at 19:14
  • The part enclosed in curly braces is the namespace of the element (see https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces). If you want to remove it, see https://stackoverflow.com/q/45660063/407651. – mzjn Aug 04 '21 at 10:56

0 Answers0