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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request] Option to ignore root <project> tag formatting #85

Closed
PayBas opened this issue Jan 15, 2021 · 8 comments
Closed

[request] Option to ignore root <project> tag formatting #85

PayBas opened this issue Jan 15, 2021 · 8 comments
Assignees

Comments

@PayBas
Copy link

PayBas commented Jan 15, 2021

First of all: amazing plugin. Great job 馃憤

We have a rather large project with about 250 POMs and 100 developers.

While this plugin works very well, one thing that is preventing me from using it is the fact that there is no option to disable formatting of the root <project> tag.

We format our tag as:

<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">

When running the sortpom:sort goal, all 250 POMs are changed to:

<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">

Which is very annoying to have to revert.

We would love it if this behavior is disabled, or controllable through a configuration option.

My settings:

<plugin>
    <groupId>com.github.ekryd.sortpom</groupId>
    <artifactId>sortpom-maven-plugin</artifactId>
    <version>2.12.0</version>
    <configuration>
        <createBackupFile>false</createBackupFile>
        <keepBlankLines>true</keepBlankLines>
        <sortDependencies>scope,groupId,artifactId</sortDependencies>
        <nrOfIndentSpace>4</nrOfIndentSpace>
        <expandEmptyElements>false</expandEmptyElements>
        <spaceBeforeCloseEmptyElement>false</spaceBeforeCloseEmptyElement>
    </configuration>
</plugin>
@Ekryd
Copy link
Owner

Ekryd commented Jan 16, 2021

Hi,

Well, this is an annoying problem. My solution so far is to close my eyes whenever I see the first line (it is just basically configuration setup, right). What happens if you keep the long line?

It is hard to tackle since I read the whole pom into an xml framework, so ignoring the root element breaks the xml.
The xml framework does not give any formatting options for the namespace section.

I can have look inside the framework itself and see if I can override some protected method during output. No promises though.

KInd regards
/Bj枚rn

@Ekryd
Copy link
Owner

Ekryd commented Jan 16, 2021

I have looked into JDOMs class XMLOutputter
printNamespace(...), printElementNamespace(...) and printAdditionalNamespaces(...) are all private and cannot be patched.
There is no possibility to set the "xmlns=" entries to a new line.

However the printAttributes(...) method is only protected and the "xsi:schemaLocation" section is an xml attribute.
This could be modified.

So, a reasonable approach would be to prefix the xsi:schemaLocation with a line termination and double indentation characters. This would follow the configuration for the plugin. In your case this would be 8 space characters (your example contains 9 space characters).

One really hard problem remains (besides the coding). What would be the name of the new configuration parameter?
"prefixSchemaLocation", "schemaLocationOnSeparateRow", "lineTerminateProject", "lineTerminateBeforeSchemaLocation"???

@PayBas
Copy link
Author

PayBas commented Jan 17, 2021

Great effort in getting to the bottom of this behavior.

It seems like there are no ideal solutions here. While having this feature would benefit me personally, I'm not sure it is worth your time and effort. Unless you feel that this feature would be useful for others (or yourself).
If you decide to implement this, I think having the option as a string would be more flexible (accommodate more users) than having it as a boolean (break line or not). There's bound to be users who use tabs for indentation for instance.

If you decide to implement it as a string (allowing \n for instance), then prefixSchemaLocation would probably be confusing. People might get the impression that it prefixes the "value". Maybe xmlSchemaLocationAttrWhitespace (or something along those lines) might be clearer.

Whatever you decide, thanks again for investigating the issue. I was hoping that perhaps there would be a simple solution ;).

edit: whitespaceBeforeProjectSchemaLocationAttr might be more in line with spaceBeforeCloseEmptyElement naming.

@Ekryd Ekryd self-assigned this Jan 25, 2021
@create-issue-branch
Copy link

Ekryd pushed a commit that referenced this issue Jan 25, 2021
Ekryd pushed a commit that referenced this issue Feb 5, 2021
@Ekryd
Copy link
Owner

Ekryd commented Feb 5, 2021

Time for testing!

  • Clone the repository

  • Checkout the master branch

  • Build with mvn clean install

  • Use the plugin with mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.13.2-SNAPSHOT:sort and remember to use the new parameter -Dsort.indentSchemaLocation=true or false. The parameter name is not the best, basically it means that the schema location attribute will be set on a separata line and be indented with (2 * normal indentation + 1 space). Other parameter names just felt too much.

  • Feel free to also use it by plugin configuration and with different indentation

Let me know how it works for you

@Ekryd
Copy link
Owner

Ekryd commented Mar 18, 2021

New version released 2.14.0. Enjoy!

@Ekryd Ekryd closed this as completed Mar 18, 2021
@PayBas
Copy link
Author

PayBas commented Mar 18, 2021

Sorry I didn't get back to you for testing. Life got in the way. I'll be sure to check out the new version. Thank you for your quick action.

@Ekryd
Copy link
Owner

Ekryd commented Mar 19, 2021

No worries. Life happens (as it should) ;-)
Hope you like the new functionality

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

No branches or pull requests

2 participants