Skip to content

Commit

Permalink
Remove default jar packaging when using ChangePackaging (#3708)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado authored Nov 19, 2023
1 parent e908b09 commit f19b05a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
return document;
}
document = document.withMarkers(document.getMarkers().withMarkers(ListUtils.map(document.getMarkers().getMarkers(), m -> {
if(m instanceof MavenResolutionResult) {
if (m instanceof MavenResolutionResult) {
return getResolutionResult().withPom(pom.withRequested(pom.getRequested().withPackaging(packaging)));
}
return m;
Expand All @@ -85,7 +85,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
public Xml visitTag(Xml.Tag tag, ExecutionContext context) {
Xml.Tag t = (Xml.Tag) super.visitTag(tag, context);
if (PROJECT_MATCHER.matches(getCursor())) {
if (packaging == null) {
if (packaging == null || "jar".equals(packaging)) {
t = filterTagChildren(t, it -> !"packaging".equals(it.getName()));
} else {
t = addOrUpdateChild(t, Xml.Tag.build("\n<packaging>" + packaging + "</packaging>"), getCursor().getParentOrThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,28 @@ void changePackaging() {
)
);
}

@Test
void changePackagingRemovingDefault() {
rewriteRun(
spec -> spec.recipe(new ChangePackaging("*", "*", "jar")),
pomXml(
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
</project>
""",
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
</project>
"""
)
);
}
}
5 changes: 5 additions & 0 deletions rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ public String getPrefix() {
public <P> Xml acceptXml(XmlVisitor<P> v, P p) {
return v.visitComment(this, p);
}

@Override
public String toString() {
return "<!--" + text + "-->";
}
}

@Value
Expand Down

0 comments on commit f19b05a

Please sign in to comment.