Skip to content

Sort maven properties (proposal with code example) #185

@rob-valor

Description

@rob-valor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions