Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.
This repository was archived by the owner on May 14, 2025. It is now read-only.

ProjectMerger.mergeMavenPlugins produces exceptions because of xml declarations #217

@jacobdanner

Description

@jacobdanner

ConversionUtils returns xmlStrings with xml declarations because Xpp3Dom.toString() and JAXB.marshal return strings with xml declarations.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <skip>true</skip>
</configuration>

in ProjectMerger.mergeMavenPlugins we are passing these xmlStrings into openrewrite recipes with the xml declaration

String configuration = (plugin.getConfiguration() != null)  
       ? ConversionUtils.fromDomToString((Xpp3Dom) plugin.getConfiguration()) : null;
   // ...
   Recipe addPluginRecipe = new AddPlugin(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(),  
       configuration, dependencies, null, null);

and it's causing issue in the openrewrite recipe because the recipe is building the xml expecting string snippets

Xml.Tag pluginTag = Tag.build("<plugin>\n<groupId>" + AddPlugin.this.groupId + "</groupId>\n<artifactId>" + AddPlugin.this.artifactId + "</artifactId>\n" + (AddPlugin.this.version != null ? "<version>" + AddPlugin.this.version + "</version>\n" : "") + (AddPlugin.this.executions != null ? AddPlugin.this.executions.trim() + "\n" : "") + (AddPlugin.this.configuration != null ? AddPlugin.this.configuration.trim() + "\n" : "") + (AddPlugin.this.dependencies != null ? AddPlugin.this.dependencies.trim() + "\n" : "") + "</plugin>");

they result in invalid xml getting created, e.g. This is what the ProjectMerger code is trying to create

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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>
    <!-- ... -->
<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-deploy-plugin</artifactId>  
    <?xml version="1.0" encoding="UTF-8"?> <!-- < - this is invalid -->
    <configuration>       
	    <skip>true</skip>  
    </configuration>
</plugin>
<!-- ... -->

This showed up in the MavenModificationTests.addPluginDependency with the following exceptions

07:13:42.126 [Test worker] ERROR org.springframework.cli.merger.ProjectMerger -- error in javaParser execution
org.openrewrite.internal.RecipeRunException: java.lang.ClassCastException: Cannot cast org.openrewrite.tree.ParseError to org.openrewrite.xml.tree.Xml$Document

but the logic in that test doesn't catch or handle or check the output because it's missing some checks

In this part of the test, getPlugins only returns the plugins that existed pre-ProjectMerger

Model mergedModel = pomReader.readPom(mergedPomPath.toFile());  
for (Plugin plugin : mergedModel.getBuild().getPlugins()) {  
    if (plugin.getGroupId().equals("org.apache.maven.plugins")  
          && plugin.getArtifactId().equals("maven-deploy-plugin")) {  
       assertThat(ConversionUtils.fromDomToString((Xpp3Dom) plugin.getConfiguration()))  
          .contains("<skip>true</skip>");  
    }

ie, this maven-deploy-plugin validation is never validated because of the RecipeRunException

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions