jgitver / jgitver-maven-plugin

maven core extension to automatically define versions using jgitver & git tags
https://jgitver.github.io/
Other
159 stars 42 forks source link

Sort maven properties (proposal with code example) #185

Open rob-valor opened 2 weeks ago

rob-valor commented 2 weeks ago

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.