Skip to content

How to enable linebreak between license header and delimiter #2186

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

Closed
snuyanzin opened this issue Jun 26, 2024 · 8 comments
Closed

How to enable linebreak between license header and delimiter #2186

snuyanzin opened this issue Jun 26, 2024 · 8 comments

Comments

@snuyanzin
Copy link

snuyanzin commented Jun 26, 2024

we are using maven (3.8.6+)
and spotless maven plugin 2.33.0

The configuration looks like

<configuration>
    <java>
        <googleJavaFormat>
            <version>1.15.0</version>
            <style>AOSP</style>
        </googleJavaFormat>
...
        <licenseHeader>
            <content>${spotless.license.header}</content>
            <delimiter>${spotless.delimiter}</delimiter>
        </licenseHeader>
    </java>
</configuration>

Where ${spotless.delimiter} is just package
The problem is that
after applying it looks like

// LICENSE
package ...

and we want to have a line break between the license and package
like

// LICENSE

package ...

an attempt to insert extra line breaks into LICENSE doesn't help.
Is there a way to make it possible?

@sankalpbhatt
Copy link

Can you please share your code where you've defined spotless.license.header
Also, have you tried using custom FormatExtension? I think you should have a separate custom FormatExtesion to do the needful

@snuyanzin
Copy link
Author

snuyanzin commented Jun 30, 2024

@nedtwigg
Copy link
Member

nedtwigg commented Jul 4, 2024

I think it might be because the XML parsing is trimming "meaningless" trailing newlines. If you specify a license file instead of an inline constant, then it should respect the newline.

@nedtwigg nedtwigg closed this as completed Jul 4, 2024
@robross0606
Copy link

robross0606 commented Nov 8, 2024

Why was this closed? Using a license file is a workaround, not a solution. The default behavior here for license generation actually breaks a Checkstyle default code smell for "'package' should be separate from previous line".

@nedtwigg
Copy link
Member

nedtwigg commented Nov 8, 2024

To the best of my knowledge, this is a limitation of XML, there is no way to have significant trailing whitespace. So the workaround is the best we can do. Happy to merge a PR that fixes this some other way, but I am betting against such a PR being possible.

@robross0606
Copy link

robross0606 commented Nov 8, 2024

What about using CDATA and \n?
https://www.w3.org/TR/REC-xml/#sec-cdata-sect

@robross0606
Copy link

I would argue that the extra newline should be added by default. And/or perhaps adding a new option to include an extra newline?

<licenseHeader> <!-- specify either content or file, but not both -->
  <content>/* (C)$YEAR */</content>  <!-- or <file>${project.basedir}/license-header</file> -->
  <delimiter>#</delimiter>  <!-- content until first occurrence of the delimiter regex will be interpreted as header section -->
  <extraNewline>true</extraNewline> <!-- always include an extra newline between header section and delimiter -->
</licenseHeader>
``

@nedtwigg
Copy link
Member

nedtwigg commented Nov 8, 2024

I would argue that the extra newline should be added by default.

Perhaps, but we aren't going to make a breaking change for this.

What about using CDATA and \n?

Seems like that would work, good idea! That should probably be the real answer.

perhaps adding a new option to include an extra newline?

My #1 goal is to help people enforce whatever rules they want, my #2 rule is to maintain as few features as possible to make that possible. We have at least 1, possibly 2 (if CDATA works) solutions to this problem. They use the public API in exactly the way it is meant to be used, so I don't think they are "workarounds".

If you really want extraNewline and you can do it inside the Maven DSL without changing the LicenseHeaderStep itself, I would probably merge it. Adds 1 line to the docs, maybe 20 lines to the code? Doesn't seem like a good use of time to me, but sure. If it ends up being like 100 lines then no way, use the workarounds.

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