'Preserve comments after modifying pom file

I am trying to modify the pom file using the apache maven model (https://maven.apache.org/ref/3.5.0/apidocs/org/apache/maven/model/package-summary.html) and I am unable to preserve comments.

I tried xmlstarlet cmd tool which preserves comments but it's slow for my case as I have 100s of pom files to modify.

This is a sample pom file which I am experimenting,

<?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>com.test</groupId>
  <artifactId>test-java-dependencies</artifactId>
  <version>1.2.2</version>
  <packaging>pom</packaging>

  <properties>

        <!--################################################-->
        <!-- Define and set:                                -->
        <!--  * general project settings                    -->
        <!--  * plugin versions and settings                -->
        <!--  * dependency versions and settings            -->
        <!--################################################-->

    <version.jackson>2.9.9</version.jackson>
  </properties>
  <dependencyManagement>
    <dependencies>
      <!-- Jackson all related libraries imported -->
      <dependency>
        <groupId>com.fasterxml.jackson</groupId>
        <artifactId>jackson-bom</artifactId>
        <version>${version.jackson}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

This is the java code,

    File pomFile = new File(".....\\pom.xml");
    Model model = getMavenModel(pomFile);
    model.setVersion("1.2.3")

    File pomFileNew = new File("...\\pom-new.xml");
    writeMavenModel(model, pomFile1);

  static Model getMavenModel(File file) {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    reader.setAddDefaultEntities(true);
    try {
      return reader.read(new FileReader(file));
    } catch (IOException | XmlPullParserException e) {
      e.printStackTrace();
    }
    return null;
  }

  static void writeMavenModel(Model model, File file) {
    MavenXpp3Writer writer = new MavenXpp3Writer();
    try (FileWriter w = new FileWriter(file)) {
      writer.write(w, model);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

I am not just working on setting a version but also have a few more modifications.

Does the apache maven model support preserving comments? I searched for some time and I could not confirm. Are there any libraries in Java which can do this?



Solution 1:[1]

preserving comments currently is not included in the org.apache.maven.model.Model.

There are two two old requests to maven model which were not fixed: https://issues.apache.org/jira/browse/MNG-4 (comment+formatting) and https://issues.apache.org/jira/browse/MNG-1117 (formatting)

For flatten-maven-plugin I created a helper class which grabs all comments before the MavenXpp3Reader and adds all comments for the nodes which are still on the same position.

Usage is

commentsOfOriginalPomFile = KeepCommentsInPom.create(getLog(), originalPomFile);

and after using MavenXpp3Writer:

xmlString = anOriginalCommentsPath.restoreOriginalComments(bufferOfMavenXpp3Writer.toString(), pom.getModelEncoding());

This is currently on branch https://github.com/c-a-services/flatten-maven-plugin/blob/keep-comments/src/main/java/org/codehaus/mojo/flatten/KeepCommentsInPom.java but hopefully merged to https://github.com/mojohaus/flatten-maven-plugin/tree/master/src/main/java/org/codehaus/mojo/flatten with https://github.com/mojohaus/flatten-maven-plugin/pull/270

You can use it until core model preserves the comments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2198875