BlackEye
BlackEye

Reputation: 777

pom.xml namespace necessary?

Is there any need or usage for the namespace declarations in pom.xml's project tag?

<project xmlns="http://maven.apache.org/POM/4.0.0" ...

We have a tool that parses pom.xmls but it fails for pom.xmls containing a namespace declaration.

Upvotes: 5

Views: 4945

Answers (2)

Joe
Joe

Reputation: 31087

All documentation for Maven tells you to use the namespace, and implies that it is required. For example, the POM reference shows:

<project xmlns="http://maven.apache.org/POM/4.0.0"

However, the MavenXpp3Reader class that Maven uses to read the pom.xml files doesn't check the namespace at all. Any value (or even no value) won't prevent Maven from parsing the pom. (And even correct use of namespaces can cause Maven to fail to parse.)

However, just as you've got a tool that fails to parse valid poms, there are other tools out there which reimplement pom parsing and require the namespace declaration. Producing poms without could cause interoperability problems down the line.

In summary; you'll get away with it if you're building with Maven, but fixing the bug in your code would by far be preferable.

Upvotes: 3

khmarbaise
khmarbaise

Reputation: 97399

In general it's a good style to add namespace information to your xml file. So it is good style to add those information to your pom file which can be helpful in particular if you are in tools like Eclipse etc. Furthermore you can validate your xml file against the XSD which is given there as well.

Apart from that if your tool has problems with such namespaces it's not a good tool cause it should handle namespace definitions correctly.

If you like to read pom file it shouldn't be a problem to implement that which correctly handles namespaces etc.

The following lines are needed to read the pom file:

MavenXpp3Reader mavenReader = new MavenXpp3Reader();
Model pom = mavenReader.read(new FileReader(pomFile));

Upvotes: 3

Related Questions