Skip to content

[#384] Do not modify the project model in effective-pom - #414

Open
Neil-Tomar wants to merge 2 commits into
apache:masterfrom
Neil-Tomar:fix/384-effective-pom-mutation
Open

[#384] Do not modify the project model in effective-pom#414
Neil-Tomar wants to merge 2 commits into
apache:masterfrom
Neil-Tomar:fix/384-effective-pom-mutation

Conversation

@Neil-Tomar

Copy link
Copy Markdown

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its verify).

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@Neil-Tomar

Neil-Tomar commented Aug 9, 2026

Copy link
Copy Markdown
Author

Fixes #384

help:effective-pom only displays the POM, so it should not change anything. But cleanModel() sorted the properties on the project's own model, so the goal modified the project it was reading.

I clone the model first, so only the copy that gets written is sorted.

The test checks that the project's properties are still the same object, with the same contents, after the goal runs. It fails without the clone.

@Neil-Tomar

Copy link
Copy Markdown
Author

Also, I have a question the template says it's not needed for changes of ~20 lines, but this PR adds a ~120-line test class. Should I file an ICLA before this can be merged?

@Neil-Tomar
Neil-Tomar force-pushed the fix/384-effective-pom-mutation branch from 5bf7f4b to 27e1cfa Compare August 9, 2026 08:24
*/
private void writeEffectivePom(MavenProject project, XMLWriter writer) throws MojoExecutionException {
Model pom = project.getModel();
Model pom = project.getModel().clone();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

how sure are we that clone works here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hey @elharo, sorry for late reply.

So I think .clone() works here because in the original version the pom in cleanModel(pom) has a reference to the original Model, so cleaning/sorting the properties there overwrites the original ones. That's the issue in #384. Since you mentioned the goal should be read only, I pass a clone() of the Model to cleanModel() instead, so only the copy gets changed.

I am still new to this codebase, so if there is a better way I am happy to change it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My question is more does this return a Cloneable object? What's the contract on Model?

@MojoParameter(name = "output", value = "${outputPath}")
void testExecuteDoesNotModifyProjectModel(EffectivePomMojo mojo) throws Exception {
// snapshot of the contents before the mojo runs, to detect in-place modification
expectedProperties = new Properties();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe this should be a local variable?

private MavenSession mavenSession;

@Mock
private Log log;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can probably use a real Log

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I removed the log entirely in new commit because it's not used.


@BeforeEach
void setup() throws IOException {
originalProperties = new Properties();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the field can be initialized directly

originalProperties.setProperty("b.property", "b-value");
originalProperties.setProperty("a.property", "a-value");

model = new Model();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the field can be initialized directly

*/
private void writeEffectivePom(MavenProject project, XMLWriter writer) throws MojoExecutionException {
Model pom = project.getModel();
Model pom = project.getModel().clone();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My question is more does this return a Cloneable object? What's the contract on Model?

/**
* The effective-pom goal only displays the model, so it must not modify the project it reads from.
*
* @throws Exception in case of errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not really needed on test

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants