Skip to content

Edit the .idea XML files with the JDK's DOM instead of Groovy's XmlParser #262

Closed

Description

@abashev

What happens

After #261 the Gradle plugin has no Groovy sources, but XmlUtils and ConfigureJavaFormatterXml still read and write .idea/open-java-format.xml, .idea/workspace.xml and .idea/externalDependencies.xml with Groovy's XmlParser, XmlNodePrinter and groovy.util.Node. The plugin does not declare Groovy: the classes come with gradleApi() at compile time and, at run time, from the Groovy that the user's Gradle bundles, so what is there is up to Gradle. That broke once: Groovy 4, in Gradle 9, dropped groovy.util.XmlParser, and the plugin failed with NoClassDefFoundError: groovy/util/XmlParser (palantir#1536).

Proposal

Parse with javax.xml.parsers.DocumentBuilder and edit the DOM, and drop the groovy.* imports from XmlUtils, ConfigureJavaFormatterXml and ConfigureJavaFormatterXmlTest. Write the files in UTF-8, which IntelliJ uses, instead of Charset.defaultCharset().

Why on its own

The text of the files changes. They are the developer's own files, which IntelliJ rewrites too, and ConfigureJavaFormatterXmlTest compares their text exactly. Tried on an externalDependencies.xml that lists one plugin, adding a second (JDK 21, Groovy 4.0.33 from Gradle 9.9.1):

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ExternalDependencies">
    <plugin min-version="5.0" id="CheckStyle-IDEA" />
  </component>
</project>

XmlNodePrinter with setPreserveWhitespace(true), as XmlUtils writes today:

<project version="4">
  <component name="ExternalDependencies">
    <plugin min-version="5.0" id="CheckStyle-IDEA"/>
    <plugin id="open-java-format"/>
  </component>
</project>

The JDK's Transformer with INDENT and an indent amount of 2 (the blank-looking lines hold spaces):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project version="4">
    
  <component name="ExternalDependencies">
        
    <plugin id="CheckStyle-IDEA" min-version="5.0"/>
      
    <plugin id="open-java-format"/>
  </component>
  
</project>
  • Today the plugin already drops IntelliJ's XML declaration and the space IntelliJ writes before />. The Transformer writes a declaration of its own, with standalone="no".
  • The Transformer indents on top of the whitespace text the parser kept. Either strip whitespace-only text before writing, or print the tree with a small printer of our own.
  • The DOM writes attributes in alphabetical order (id before min-version); XmlNodePrinter keeps the order of the file.

The aim worth a test: a file the plugin has nothing to change in comes out byte for byte the same, which is not the case today.

Activity

  1. added
    enhancement
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions