jgitver-maven-plugin icon indicating copy to clipboard operation
jgitver-maven-plugin copied to clipboard

Sort maven properties (proposal with code example)

Open rob-valor opened this issue 1 year ago • 0 comments

When setting jgitver.resolve-project-version to true the properties in the pom file are rewritten but these are stored in a java.util.Properties which in turn is a java.util.Hashtable<Object,Object>. So when these properties are serialized to a new pom.xml file, the order of the properties is mixed up.

This can easily be fixed by using a custom Properties class which returns the key set as a TreeSet:

package fr.brouillard.oss.jgitver;

import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

class SortedProperties extends Properties {
  public SortedProperties(Properties properties) {
    properties.forEach((key, value) -> setProperty(key.toString(), value.toString()));
  }

  @Override
  public Set<Object> keySet() {
    return new TreeSet<>(super.keySet());
  }
}

Use this class as properties for the model that is rewritten as the new pom.xml file. Insert at JGitverUtils:219:

   model.setProperties(new SortedProperties(model.getProperties()));

That way, the properties are nicely sorted instead of being mixed up.

I'm not able to build the project myself because I get a [ERROR] java.lang.ClassNotFoundException: org.apache.maven.surefire.junit4.JUnit4Provider when building with ./mvnw package. I installed it locally by building without tests and verified the result in a project.

rob-valor avatar Aug 29 '24 14:08 rob-valor