spotless icon indicating copy to clipboard operation
spotless copied to clipboard

How to enable linebreak between license header and delimiter

Open snuyanzin opened this issue 1 year ago • 2 comments

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?

snuyanzin avatar Jun 26 '24 07:06 snuyanzin

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

sankalpbhatt avatar Jun 30 '24 09:06 sankalpbhatt

Here it is an example https://github.com/datafaker-net/datafaker/blob/ba65ca925835a1834010bae7c4c36e09ef1d94db/pom.xml#L242-L253 and the definition of spotless.license.header https://github.com/datafaker-net/datafaker/blob/ba65ca925835a1834010bae7c4c36e09ef1d94db/pom.xml#L58-L60

snuyanzin avatar Jun 30 '24 18:06 snuyanzin

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 avatar Jul 04 '24 00:07 nedtwigg

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

robross0606 avatar Nov 08 '24 14:11 robross0606

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.

nedtwigg avatar Nov 08 '24 14:11 nedtwigg

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

robross0606 avatar Nov 08 '24 14:11 robross0606

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

robross0606 avatar Nov 08 '24 15:11 robross0606

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.

nedtwigg avatar Nov 08 '24 16:11 nedtwigg