Coder Social home page Coder Social logo

acegi / xml-format-maven-plugin Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 13.0 1.29 MB

Advanced, automatic formatting of your XML files during Maven builds

Home Page: https://acegi.github.io/xml-format-maven-plugin/

License: Apache License 2.0

Java 100.00%
java xml-format xml maven maven-plugin

xml-format-maven-plugin's Introduction

Maven Build and Deployment Coverage Status Javadocs Maven Central License

XML Format Maven Plugin

Take a look at the Project Web Site for full information.

Snapshots

Snapshot releases are available in the OSS Sonatype Snapshots Repository.

Releasing

Use jdk 17 or lower to perform release in order to keep java compatibility at java 7. This is necessary as usage of java 21 to build requires we set to java 8.

xml-format-maven-plugin's People

Contributors

aaime avatar abrokenjester avatar benalexau avatar bvella avatar fishermans avatar hazendaz avatar mttjj avatar olim7t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xml-format-maven-plugin's Issues

xml-format goal fails to execute on maven versions prior to 3.3.9

My team is seeing this plugin fail on maven versions prior to 3.3.9. I've tried with 3.0.5 and 3.3.3 and both receive this error:
[ERROR] Failed to execute goal au.com.acegi:xml-format-maven-plugin:3.0.6:xml-format (default) on project [name redacted]: Unable to parse configuration of mojo au.com.acegi:xml-format-maven-plugin:3.0.6:xml-format for parameter includes: Cannot assign configuration entry 'includes' with value '**/src/test/resources/**/*.xml' of type java.lang.String to property of type java.lang.String[] -> [Help 1]

Here's how we have the plugin configured in our pom.

<plugin>
    <groupId>au.com.acegi</groupId>
    <artifactId>xml-format-maven-plugin</artifactId>
    <version>3.0.6</version>
    <executions>
        <execution>
            <goals>
                <goal>xml-format</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <indentSize>4</indentSize>
        <includes>**/src/test/resources/**/*.xml</includes>
    </configuration>
</plugin>

Maven versions from 3.3.9 onward seem to parse this correctly. Are there any other alternative ways of writing the includes tag? We've tried:

<includes>
    <include>**/src/test/resources/**/*.xml</include>
</includes>

We've also tried:

<includes>
    <includes>**/src/test/resources/**/*.xml</includes>
</includes>

But neither of these formats gave us the results we're looking for as it formatted all XML files in the project and not just the ones in the folder we would like to specify.

The official documentation states that only a minimum version of 2.0 is required for maven.

We have a large number of engineers and build machines that make requiring a minimum maven version of 3.3.9 infeasible at this time. However, we would still like to be able to use this plugin. Any guidance would be appreciated.

xml attributes

is there a possibility to split multiple attribute each on a new line?

Add "validate" goal

Would it be possible to add a goal which doesn't change the files, but fails the build if the "format" goal would have changed anything? This is mainly useful for CI, to make sure everyone has checked in formatted files.

Skip formatting if file size = 0

Otherwise we see things like:

ERROR] [xml-format] Error for /home/bpa/projects/acegi-standard-project/acegi-standard-maven-plugin/target/checkstyle-result.xml org.dom4j.DocumentException: Error on line 1 of document : Premature end of file. Nested exception: Premature end of file. at org.dom4j.io.SAXReader.read(SAXReader.java:466) at org.dom4j.io.SAXReader.read(SAXReader.java:329)

Board 'Renovate' to manage dependencies

@benalexau Can you go here https://github.com/marketplace/renovate and board this project onto Renovate? Its free and is a large step above dependabot in terms of what it provides. It will send a PR for onboarding which can simply be merged then that will create an issue tracking that will live forever that allows interactions with it as well as describing all dependencies we use (not just maven but github actions, future maven wrapper, etc).

JDK 7 Support

Hi Ben,
as a reminder, for the next release can you add the support for java 7?

Thank you!
Davide

Different formatting in linux vs. windows

Hi,

Love this plugin, however we have a very annoying problem:
The formatting behaves differently on Windows and on mac.

We use the plugin on our pom.xml files.

The first two lines of these files are:

<project ...>

Formatting under linux:

<project ...>

Formatting under Windows:

So in Windows, these two tags are put on one line, vs. on 2 lines under linux (preferred behaviour!).

Is this plugin thread safe for parallel building?

Thanks for your great effort to bring us this helpful plugin.

I'm building my projects in my CI pipelines with parallel building enabled. Adn maven just keeps whining that thisplugin is not marked as @threadsafe.

If the formatting procedures is thread safe, can we add the mark in mojo?

formatter corrupts XML file if file.encoding != UTF-8

using the example at https://github.com/jtnord/google-storage-plugin/tree/attempt-formatting-upgrade the formatter corrupts the pom.xml (on windows).

On windows the default file encoding is very rarely UTF-8.

If I specify explicitly set set MAVEN_OPTS=-Dfile.encoding=UTF-8 before running mvn validate things work as expected, however without that step the pom.xml is corrupted. as demonstrated by running mvn validate && mvn validate

which produces (on the second run)

[INFO] --- xml-format-maven-plugin:3.1.2:xml-format (validate) @ google-storage-plugin ---
[ERROR] [xml-format] Error for C:\workarea\source\github\jenkinsci\google-storage-plugin\pom.xml
org.dom4j.DocumentException: Invalid byte 3 of 3-byte UTF-8 sequence.
    at org.dom4j.io.SAXReader.read (SAXReader.java:513)
    at org.dom4j.io.SAXReader.read (SAXReader.java:392)
    at au.com.acegi.xmlformat.FormatUtil.format (FormatUtil.java:77)
    at au.com.acegi.xmlformat.FormatUtil.formatInPlace (FormatUtil.java:106)
    at au.com.acegi.xmlformat.XmlFormatPlugin.processFile (XmlFormatPlugin.java:40)
    at au.com.acegi.xmlformat.AbstractXmlPlugin.execute (AbstractXmlPlugin.java:208)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte (UTF8Reader.java:701)
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read (UTF8Reader.java:435)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load (XMLEntityScanner.java:1895)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar (XMLEntityScanner.java:1551)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next (XMLDocumentFragmentScannerImpl.java:2821)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next (XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next (XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument (XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse (XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse (XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse (XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse (AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse (SAXParserImpl.java:643)
    at org.dom4j.io.SAXReader.read (SAXReader.java:494)
    at org.dom4j.io.SAXReader.read (SAXReader.java:392)
    at au.com.acegi.xmlformat.FormatUtil.format (FormatUtil.java:77)
    at au.com.acegi.xmlformat.FormatUtil.formatInPlace (FormatUtil.java:106)
    at au.com.acegi.xmlformat.XmlFormatPlugin.processFile (XmlFormatPlugin.java:40)
    at au.com.acegi.xmlformat.AbstractXmlPlugin.execute (AbstractXmlPlugin.java:208)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

this should be reproducible on nix platforms by doing the inverse export MAVEN_OPTS=-Dfile.encoding=Cp1252 && mvn validate && mvn validate

Option for ignoring the XML declaration

Hi

Is there an option other than suppressDeclaration (which removes the xml tag from all files) which I can use to leave the tag untouched in all files? Use-case if Mule where custom types are defined in XML files but without xml declaration while flow files do have it. Without suppressDeclaration the added xml tag makes the custom types unrecognized in Studio and with suppressDeclaration all XML files have it removed. Not seen an issue due to this, but it would be better to just have the plugin skip the declaration handling.

Thanks,
Fritzi

Cannot Format Generated XML Documents in ${project.build.directory}

I was going to use xml-format-maven-plugin to format some XML generated via XSLT, but XmlFormatPlugin explicitly restricts operating on files in the project.build.directory:

final List<String> exclude = new ArrayList<>(asList(excludes));
if (baseDirectory.equals(targetDirectory.getParentFile())) {
exclude.add(targetDirectory.getName() + "/**");
}

I'd prefer to default to not modifying the source directory (which I prefer to remain immutable during builds) and completely prevent modifying files in the mutable target directory.

Preserve empty line (allow setting size but default 1)

A lot of times especially in pom files, I break things up into categories and separate by an empty line. This helps with reading and further changes with git. Currently this plugin will remove all these line seperations which isn't expected. Generally I like the cleanup but would like if it perserves 1 line break between characters and/or possibly configurable with default of 1.

Formatter modifies text between tags.

Given an xml tag with the text of a list if items separated by new lines, the formatter will replace the new lines with spaces. Formatting should not corrupt the original intent of the text fields.

Example

<chars>
  a
  b
  c
  d
</chars>

After formatting

<chars>a b c d</chars>

Further the field: trimText, default: true, description: Should we preserve whitespace or not in text nodes.
Suggests that the whitespace in the text nodes will not be modified by default.
When explicitly specifying this field as true the formatting performs the same as stated above
When explicitly specifying this field as false the formatting does not modify the text. But every time the formatter is run it will add a new line after every xml tag. IE every subsequent run of the formatter add as new line, run it once there is one new line between each tag, run it again there is two new lines between each tag, and so on.

Example

<field-a>blah</field-a>
<field-b>blah</field-b>
<chars>
  a
  b
  c
  d
</chars>

After formatting once

<field-a>blah</field-a>

<field-b>blah</field-b>

<chars>
  a
  b
  c
  d
</chars>

After formatting again

<field-a>blah</field-a>


<field-b>blah</field-b>


<chars>
  a
  b
  c
  d
</chars>

Please put maven artifacts in provided scope and release new version

Maven requires certain artifacts to be provided scope so that m2 repo is not bloated with downloads when maven is what controls that part. This plugin is currently violating this with maven-plugin-api being in compile scope. Please set that to 'provided' and if you could get a release soon. This is ultimately a major problem with maven 4.0 and is already lighting up dashboards on usage due to maven 3.9 making it flag this.

Do not access network while formatting

Stack trace if network offline:

[INFO] --- xml-format-maven-plugin:3.0.2:xml-format (xml-format) @ lmdbjava ---
[ERROR] [xml-format] Error for /home/bpa/projects/lmdbjava/src/main/config/checkstyle.xml
org.dom4j.DocumentException: www.puppycrawl.com Nested exception: www.puppycrawl.com
    at org.dom4j.io.SAXReader.read(SAXReader.java:468)
    at org.dom4j.io.SAXReader.read(SAXReader.java:329)
    at au.com.acegi.xmlformat.FormatUtil.format(FormatUtil.java:41)
    at au.com.acegi.xmlformat.FormatUtil.formatInPlace(FormatUtil.java:70)
    at au.com.acegi.xmlformat.XmlFormatPlugin.execute(XmlFormatPlugin.java:145)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)

Formatter formats quotes values

Hi, i just tried your formatter, everything seems to be work well except that values are modified. I guess values inside quotes should not be modified.

input

output

Here is my maven configuration

<plugin>
                <groupId>au.com.acegi</groupId>
                <artifactId>xml-format-maven-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <indentSize>4</indentSize>
                    <excludes>
                        <exclude>%regex[.*pom.xml]</exclude>
                    </excludes>
                </configuration>
            </plugin>

Can we format the xml schema declaration to multiple lines?

For example, there is a formatted XML file:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
....
</project>

Can xml-format-maven-plugin format it into this style:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
</project>

Thanks

Formatting differs in section with no blank lines whether or not keepBlankLines is used

The formatting output differs whether keepBlankLines and thus the BlankLinesWriter is used or not

Original unformatted xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <test>${project.build.directory}/reports/json/
    </test>
</project>

Format with keepBlankLines=false

Output:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <test>${project.build.directory}/reports/json/</test>
</project>

Format with keepBlankLines=true

Actual:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <test></test>
</project>

Expected:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <test>${project.build.directory}/reports/json/</test>
</project>

lineEnding setting is not respected for empty lines and CDATA, multiline text content is deleted

I have a pom.xml which is structed like this:

<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>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <xml-format-plugin.version>3.2.2</xml-format-plugin.version>

    <sonar.coverage.exclusions><![CDATA[
      src/main/java/A.java,
      src/main/java/B.java,
      src/main/java/C.java,
    ]]></sonar.coverage.exclusions>
    <sonar.exclusions><![CDATA[
      src/main/java/D.java,
      src/main/java/E.java,
    ]]></sonar.exclusions>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>au.com.acegi</groupId>
        <artifactId>xml-format-maven-plugin</artifactId>
        <version>3.2.2</version>
        <executions>
          <execution>
            <id>xml-format</id>
            <goals>
              <goal>xml-format</goal>
            </goals>
            <phase>process-sources</phase>
            <configuration>
              <indentSize>2</indentSize>
              <suppressDeclaration>true</suppressDeclaration>
              <keepBlankLines>true</keepBlankLines>
              <includes>pom.xml</includes>
              <lineEnding>CRLF</lineEnding>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Here's a screenshot of Notepad++ after the pom.xml was formatted:
image

I have several issues with the current behavior:

  1. The plugin inserts an empty line at the top
  2. The line breaks after </xml-format-plugin.version> are LFCRLF, so 1 Linux line break followed by 1 Windows line break. Both should be CRLF.
  3. The CDATA content will also have LFs instead of CRLFs.
  4. If I don't put the lines in the sonar exclusions into CDATA, the plugin will delete the text content.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.