[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.
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