Skip to content

Conversation

gnodet
Copy link
Contributor

@gnodet gnodet commented May 28, 2025

Description

This PR fixes MNG-8746 by implementing property insertion order preservation in Maven's WrapperProperties class. The issue was that properties accessed through model.getProperties() were not maintaining their insertion order, leading to inconsistent behavior.

Problem

The WrapperProperties class in the Maven compat layer was not preserving the insertion order of properties. When properties were added through the Properties API and then accessed via methods like keySet() or entrySet(), they would be returned in an arbitrary order (often alphabetical) rather than the order they were inserted.

Solution

Implemented a comprehensive order preservation mechanism:

Key Changes:

  1. Enhanced WrapperProperties Template (src/mdo/java/WrapperProperties.java):

    • Added internal OrderedProperties class that maintains insertion order
    • Implemented caching mechanism to preserve order across read/write operations
    • Modified all read operations to use the ordered cache
    • Simplified write operations to work directly with the ordered properties
  2. OrderedProperties Implementation:

    • Uses ArrayList<Object> keyOrder to track key insertion order
    • Overrides put(), setProperty(), and remove() methods to maintain order
    • Custom KeySet and EntrySet implementations that iterate in insertion order
    • Removed unnecessary synchronized wrappers that interfered with ordering
  3. Architecture Simplification:

    • WrapperProperties maintains its own ordered state independently
    • Only syncs content (not order) with the v4 API model when needed
    • Eliminated complex cache invalidation logic that was causing order loss

Testing

Added comprehensive tests:

  • PropertiesOrderTest: Tests v4 API model order preservation
  • WrapperPropertiesOrderTest: Tests compat layer order preservation
  • Covers various scenarios including property modification and initial properties

Verification

The implementation ensures that:

Properties props = model.getProperties();
props.setProperty("key3", "value3");
props.setProperty("key1", "value1");
props.setProperty("key2", "value2");

// keySet() now returns ["key3", "key1", "key2"] in insertion order
// instead of ["key1", "key2", "key3"] in alphabetical order

Impact

  • ✅ Preserves property insertion order for consistent behavior
  • ✅ Maintains backward compatibility
  • ✅ No breaking changes to existing APIs
  • ✅ Improves reproducible builds

Fixes: https://issues.apache.org/jira/browse/MNG-8746


Pull Request opened by Augment Code with guidance from the PR author

This commit implements order preservation for properties in the Maven compat
layer's WrapperProperties class, ensuring that properties maintain their
insertion order when accessed through the Properties API.

Key changes:

1. Enhanced WrapperProperties template (src/mdo/java/WrapperProperties.java):
   - Added internal OrderedProperties class that maintains insertion order
   - Implemented caching mechanism to preserve order across read/write operations
   - Modified all read operations to use the ordered cache
   - Simplified write operations to work directly with the ordered properties

2. OrderedProperties implementation:
   - Uses ArrayList to track key insertion order
   - Overrides put/setProperty/remove to maintain order
   - Custom KeySet and EntrySet implementations that iterate in insertion order
   - Removed unnecessary synchronized wrappers that interfered with ordering

3. Updated model-v3.vm template:
   - Ensured LinkedHashMap is used when converting properties to v4 API
   - This preserves order when properties are synced between compat and v4 layers

4. Added comprehensive tests:
   - PropertiesOrderTest: Tests v4 API model order preservation
   - WrapperPropertiesOrderTest: Tests compat layer order preservation
   - Covers various scenarios including modification and initial properties

The implementation ensures that properties accessed through model.getProperties()
maintain their insertion order, which is important for consistent behavior
and reproducible builds.

Fixes: MNG-8746
Copy link
Contributor

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use PostConstruct ?

@gnodet gnodet added this to the 4.0.0-rc-4 milestone Jun 3, 2025
@gnodet gnodet merged commit 9d307f0 into master Jun 4, 2025
25 checks passed
@gnodet gnodet deleted the fix-MNG-8746 branch June 4, 2025 09:10
@gnodet gnodet added the bug Something isn't working label Jun 4, 2025
@github-actions github-actions bot added maintenance and removed bug Something isn't working labels Jun 4, 2025
Pankraz76 added a commit to Pankraz76/maven that referenced this pull request Jun 4, 2025
…ache#2404)

* Fix MNG-8746: Preserve property insertion order in WrapperProperties

This commit implements order preservation for properties in the Maven compat
layer's WrapperProperties class, ensuring that properties maintain their
insertion order when accessed through the Properties API.

Key changes:

1. Enhanced WrapperProperties template (src/mdo/java/WrapperProperties.java):
   - Added internal OrderedProperties class that maintains insertion order
   - Implemented caching mechanism to preserve order across read/write operations
   - Modified all read operations to use the ordered cache
   - Simplified write operations to work directly with the ordered properties

2. OrderedProperties implementation:
   - Uses ArrayList to track key insertion order
   - Overrides put/setProperty/remove to maintain order
   - Custom KeySet and EntrySet implementations that iterate in insertion order
   - Removed unnecessary synchronized wrappers that interfered with ordering

3. Updated model-v3.vm template:
   - Ensured LinkedHashMap is used when converting properties to v4 API
   - This preserves order when properties are synced between compat and v4 layers

4. Added comprehensive tests:
   - PropertiesOrderTest: Tests v4 API model order preservation
   - WrapperPropertiesOrderTest: Tests compat layer order preservation
   - Covers various scenarios including modification and initial properties

The implementation ensures that properties accessed through model.getProperties()
maintain their insertion order, which is important for consistent behavior
and reproducible builds.

Fixes: MNG-8746

* Update src/mdo/java/WrapperProperties.java

Co-authored-by: Pankraz76 <[email protected]>

* Update src/mdo/java/WrapperProperties.java

Co-authored-by: Pankraz76 <[email protected]>

---------

Co-authored-by: Pankraz76 <[email protected]>
@gnodet gnodet added bug Something isn't working and removed maintenance labels Jun 7, 2025
@jira-importer
Copy link

Resolve #9550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants