Comments (9)
It seems feasible to override XMLWriter#writeElement
method in PatchedXMLWriter
and do special handling for the <project>
element possibly copy pasting the exact content from the original input.
from sortpom.
Sounds interesting, I'll have a look.
from sortpom.
Bad news, the SaxParser, which reads the file, does not hold any information about the original content. There is no convenient way to preserve linebreaks and whitespaces.
Second part is ordering:
<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/maven-v4_0_0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
is the overall namespace
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
is an actual attribute to the project element.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
is the namespace for that attribute.
If all three were attributes, then we could have kept track of reordering. Now, they are three separate things and I cannot see in which order they were written in the original XML. Basically same limitation as above.
from sortpom.
There is way to make indention before the project attribute: indentSchemaLocation
. It will always format the the project element 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 http://maven.apache.org/maven-v4_0_0.xsd">
I thought it would be better, but other tools usually ruin that order anyway: whenever a version needs to be bumped due to deployments or dependency updates
from sortpom.
Isn't it possible to get the content of the project element from the raw input (before XML parsing) and hardcode it later on when we are about to write the XML file?
This is somehow similar to the https://github.com/Ekryd/sortpom/wiki/IgnoringSections feature but specialized for the <project ...>
where "ignoringSections" cannot be applied.
from sortpom.
Yes, it is possible. But...
Ignoring sections is a hack in order to solve some concrete cases in the Maven setup, i.e. some content needed to be have custom comments because of some tool parsing the pom-file in order to generate something code-like. Ignoring sections also needs to take away full xml elements, so that the rest of the file is parseable.
Ignoring the start of the project element would not be possible, because the rest of the file cannot be parsed then. So that functionality cannot be used directly.
Another way is to sort the whole file, go back to the original file and do text replacements. This can be done, but I'm not too keen on doing it. I would solve an XML formatting problem, not a Maven formatting problem. It is out of scope for the plugins main responsibility. The code in the plugin only parses xml tags, not text formatting. Any new code that handles text directly would increase overall complexity, thus making it harder to maintain. Also, it would likely be harder to reuse the plugin for other XML parsing, if there are use-cases out there.
from sortpom.
I have discovered that there is a fairly easy way to indent the namespace as well. Then you could get a schema 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 http://maven.apache.org/maven-v4_0_0.xsd">
Indenting both namespace and attribute of the project element, but reordering of the content is still not available. Is this a possible solution?
from sortpom.
First of all thanks a lot for all the research and effort spend on this ticket. Much appreciated!
For my particular use-case, I don't care much about indentation inside the project
element (for previous requests this could be useful). The request is leave the things as they are inside the project element.
In fact for my use-case it seems that I could leave with the following workaround.
- Use the plugin to sort the pom according to the defined rules
- Restore the formatting changes in
project
element by editing the pom.xml file (manually or automatically via other plugins/scripts).
By properly configuring the verify goal subsequent calls to verify would leave the file intact so I think that's good enough for me at the moment.
I opened the ticketing that change to keep formatting would be easy and not too intrusive after the dom4j upgrade but it seems that is not so I am fine with closing this ticket.
from sortpom.
No worries, I appreciate that you took the time to formulate the issue.
A quick search for replacing content gave me https://github.com/floverfelt/find-and-replace-maven-plugin, maybe that could create an automatic solution? Although I would suggest opening an issue to read the replacement text from a file 😄 . (echo-maven-plugin functionality can be used for inspiration for that).
Best of luck!
from sortpom.
Related Issues (20)
- Autoformat settings.xml files HOT 2
- Minimal version of Java for this plugin ? HOT 9
- Is it normal that pom.xml in target repository is checked ? HOT 7
- Preserve comments formatting HOT 5
- Merge automatic pull requests automatically HOT 6
- Xml formatting HOT 12
- sorting doesn't work in >=v3.0.0 HOT 11
- no sort xmls with doctype HOT 4
- Add entry for the 3.2.1 release on the wiki page `Versions` HOT 2
- Tone down sortpom output HOT 6
- Properties configuration Suggestion HOT 7
- Setting to disable pom sorting? HOT 4
- LATEST using in pom is deprecated and will be removed from maven 4 HOT 8
- Handle plugin warnings HOT 1
- Dependency Dashboard
- Setup Maven 4 build pipe HOT 1
- Add configuration to suppress XML declaration HOT 7
- Add configuration for newline at the end of file HOT 6
- XML tag with `xml:space="preserve"` containing only spaces should not be collapsed HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sortpom.