Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve empty line (allow setting size but default 1) #11

Closed
hazendaz opened this issue May 2, 2017 · 11 comments
Closed

Preserve empty line (allow setting size but default 1) #11

hazendaz opened this issue May 2, 2017 · 11 comments

Comments

@hazendaz
Copy link
Collaborator

hazendaz commented May 2, 2017

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.

@benalexau
Copy link
Member

xml-format-maven-plugin is simply a wrapper over the Dom4J formatter. You can see Dom4J's configuration options in OutputFormat.java. Unfortunately there isn't a configuration option available to preserve newlines.

If you'd like newline preservation support, I'd encourage you to explore this with the Dom4J project. Alternately if you knew of a more feature-rich backend that xml-format-maven-plugin could adopt I would be happy to take a look.

I will close this ticket here because there is nothing we can easily add to the xml-format-maven-plugin project to support this capability.

@hazendaz
Copy link
Collaborator Author

@benalexau Thank you for explaining how this works. I'll look into this further. I was planning to use this over a super pom setup at work but a number of factors such as this prevented such usage. I haven't had a lot of time to dive deeper into your library but suspect I eventually will get there. Again than you for the explanation on how that works.

@allan-shoup
Copy link

I recently came across this plugin and it looks promising, but like @hazendaz, I find the output too dense with the empty lines removed. I went ahead and logged an issue with dom4j. Quickly looking for alternative formatting options, I came across these StackOverflow questions: How to pretty print XML from Java?, Pretty print XML in java 8 (which you've probably already seen). Do you think any of the alternatives mentioned would work, such as java.xml.Transformer or the xml-formatter-core library?

@aaime
Copy link
Contributor

aaime commented Mar 13, 2021

Also stumbled into the whileline preservation issue. In the end, explored the sortpom plugin for formatting the pom files, which wroked beautifully. But still have lots of XML files in the sources that I'd like to pretty print.

That led me to wonder, how does sortpom preserve white lines? It's using jdom to parse the files, instead of dom4j, it seems:
https://github.com/Ekryd/sortpom/blob/master/sorter/src/main/java/sortpom/XmlProcessor.java

@allan-shoup
Copy link

@aaime, for what it's worth, we use Spotless for XML formatting (using the Eclipse WTP formatter) and it is working well.

@aaime
Copy link
Contributor

aaime commented Mar 20, 2021

@allan-shoup thanks, just tried it, it seems to fail on a parallel build (which we use a lot, give the large number of modules in the project, over 100). See last comment at: diffplug/spotless#492

@hazendaz
Copy link
Collaborator Author

The sortpom will preserve whitespace. Using their approve maven configuration then some minor tweaks to it gets almost the best possible setup. Its good enough we put this into our CI pipelines to address pom formatting on 100s of projects. We also ended up not using this plugin as formatter-maven-plugin which I'm part of the group got donated code that addresses significant amount of additional formatting covering xml nicely.

@aaime
Copy link
Contributor

aaime commented Mar 22, 2021

Hi @hazendaz I've tried out the formatter-maven-plugin, it also fails under a parallel build, as I've reported here: revelc/formatter-maven-plugin#433
I'm having the impression that anything based on Eclipse project may not work in a parallel build, as it's designed for a desktop tool, which just one user triggering reformats at a time.

I've also found that adding empty line preservation in this plugin is actually quite easy, a matter of subclassing the XML writer.
I have a draft of the approach, as well as a suggested plan on how to move from idea to final contribution, in this draft PR.

@hazendaz
Copy link
Collaborator Author

I run parallel builds all the time with no issues at formatter-maven-plugin. I think the problem is changing defaults so its picking up more than the independent src folders space from each module which crosses over. I believe you may have shown an example where that wasn't always the case and still had issues. We are still looking at that issue over there as well.

@hazendaz
Copy link
Collaborator Author

btw - xml formatting is not eclipse driven. That is coming from a separate project xml-formatter under same overall project and is borrowed code from eclipse formatter but under the hood is pure sax. Maybe its an issue in there and more closely related to same here.

@aaime
Copy link
Contributor

aaime commented Mar 22, 2021

Not sure why we are discussing the issue of the other project here. Can we resume at revelc/formatter-maven-plugin#433 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants