[MPH-217] Ensure packaging is always shown in effective-pom output - #405
[MPH-217] Ensure packaging is always shown in effective-pom output#405elharo wants to merge 3 commits into
Conversation
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>.
| 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Pull request overview
This PR addresses MPH-217 / #332 by ensuring help:effective-pom always shows an explicit <packaging> element, even when the project relies on Maven’s default packaging (jar).
Changes:
- Post-processes the serialized effective POM output to add a
<packaging>element when it would otherwise be omitted. - Adds a new integration test project (
effective-pom-packaging) to verify<packaging>jar</packaging>appears when packaging is not specified in the input POM.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java | Adds logic intended to inject <packaging> into effective-pom output when omitted by serialization. |
| src/it/projects/effective-pom-packaging/verify.groovy | New IT assertion checking the effective-pom output includes <packaging>jar</packaging>. |
| src/it/projects/effective-pom-packaging/pom.xml | New IT project POM without a <packaging> element to exercise the default behavior. |
| src/it/projects/effective-pom-packaging/invoker.properties | Runs the effective-pom goal for the new IT project. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| // MavenXpp3Writer omits packaging when it is null or "jar" (the default). | ||
| // Ensure it is always present so users can see the effective packaging value. | ||
| if (pom.getPackaging() == null || "jar".equals(pom.getPackaging())) { | ||
| 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); | ||
| } | ||
| } |
| def LS = System.getProperty("line.separator") | ||
| assert buildLog.text.find( | ||
| '(?s)' + | ||
| ' <packaging>jar</packaging>') != null |
|
Agree with @pzygielo's diagnosis — won't re-litigate the Beyond the parent case already shown:
Gotcha: Test: the current |
Problem:
help:effective-pomdoes not show the<packaging>element when the packaging is not explicitly set in the POM (defaulting tojar).MavenXpp3Writerintentionally omits the<packaging>element when it isnullor 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