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>