Skip to content

[MPH-217] Ensure packaging is always shown in effective-pom output - #405

Draft
elharo wants to merge 3 commits into
masterfrom
fix-mph-217
Draft

[MPH-217] Ensure packaging is always shown in effective-pom output#405
elharo wants to merge 3 commits into
masterfrom
fix-mph-217

Conversation

@elharo

@elharo elharo commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Problem: help:effective-pom does not show the <packaging> element when the packaging is not explicitly set in the POM (defaulting to jar). MavenXpp3Writer intentionally omits the <packaging> element when it is null or equals "jar" (the default), so users cannot see the effective packaging value.

Fix: After serialization, detect if the <packaging> element is missing and insert it into the XML output if so. This ensures the effective packaging value is always visible.

Test: Added an integration test (effective-pom-packaging) with a POM that has no <packaging> element, verifying the output contains <packaging>jar</packaging>.

Fixes #332

MavenXpp3Writer omits the packaging element when it is null or equals
the default value 'jar'. This leads help:effective-pom to not show
the packaging at all when it is not explicitly set in the POM.

Fix: after serialization, insert the packaging element into the XML
output if missing. This ensures users can always see the effective
packaging value (e.g. jar, pom, war) in the effective POM output.

Includes an integration test with a POM that has no packaging element,
verifying the output contains <packaging>jar</packaging>.
@elharo elharo added the bug Something isn't working label Aug 7, 2026
@elharo
elharo marked this pull request as draft August 7, 2026 12:36
@elharo
elharo marked this pull request as ready for review August 7, 2026 15:13
@elharo
elharo requested a review from ascheman August 7, 2026 15:13
String packaging = pom.getPackaging() != null ? pom.getPackaging() : project.getPackaging();
String packagingTag = " <packaging>" + packaging + "</packaging>";
if (!effectivePom.contains(packagingTag)) {
effectivePom = effectivePom.replace("</version>" + LS, "</version>" + LS + packagingTag + LS);

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.

If given project has parent, like

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>0.1-SNAPSHOT</version>
  </parent>
  <groupId>...</groupId>
  <artifactId>..</artifactId>
  <version>0.1-SNAPSHOT</version>
  ...

which </version> will be replaced in l. 206?

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.

I updated IT presented here with:

diff --git a/src/it/projects/effective-pom-packaging/pom.xml b/src/it/projects/effective-pom-packaging/pom.xml
index 6622e25..a97aed6 100644
--- a/src/it/projects/effective-pom-packaging/pom.xml
+++ b/src/it/projects/effective-pom-packaging/pom.xml
@@ -21,7 +21,11 @@ under the License.
 
 <project>
   <modelVersion>4.0.0</modelVersion>
-
+<parent>
+<groupId>org.apache.maven</groupId>
+<artifactId>maven-parent</artifactId>
+<version>49</version>
+</parent>
   <groupId>org.apache.maven.its.help</groupId>
   <artifactId>mph-217</artifactId>
   <version>1.0-SNAPSHOT</version>

And here is test result: SUCCESS.

But here is the goal result:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-parent</artifactId>
    <version>49</version>
    <packaging>jar</packaging>
  </parent>
  <groupId>org.apache.maven.its.help</groupId>
  <artifactId>mph-217</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

This is of course non-sense for parent.

And, perhaps according to String.replace javadoc (Replaces each substring of this string that matches the literal target sequence), every </version>, including plugins' - has explicit <packaging>jar</packaging> companion now.

I don't think it's correct...

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.

To clarify above - even without any modification to this PR - manual inspection of target/it/effective-pom-packaging/build.log shows that too much is done to the effective pom.

Perhaps src/it/projects/effective-pom-packaging/verify.groovy could be fixed to check that only expected changes are there (i.e. no unexpected changes are).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point and a good reminder of why regexes are dangerous when processing XML. There are other failure modes here too. I might take a whirl at fixing this with some real XML.

@elharo
elharo marked this pull request as draft August 9, 2026 10:58
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.

[MPH-217] help:effective-pom does not show the value for a POM's "packaging"

2 participants