How to make Maven checking the source end-of-line?
Asked Answered
K

3

4

I would like to enforce Unix style end-of-line (\n) in Java sources of our projects for consistency reasons. As many of us work under Windows, most IDEs are configured by default to use Windows style end-of-line (\r\n). We try to change that settings each time we configure a new workspace but for some reasons, some files are commited with Windows end-of-line.

Is there any Maven configuration or plugin which could help us enforcing the Unix style end-of-line or, at least, warning us of the files that are in Windows style?

I though about PMD or Checkstyle first but it doesn't appear to be that simple. I've also checked Maven Enforcer Plugin but I don't think it do that out-of-the box. I should be able to create my own rule but I'd like to make sure I don't reinvent the wheel :)

Kowal answered 21/2, 2013 at 13:47 Comment(1)
I don't think having maven modify the sources (if not for generated sources) would be a good idea. Your requirement would better fit to a pre-commit hook.Gentile
H
5

Use Checkstyle, we used it successfully in our project to enforce Unix line endings.

You can use a regular expression to do this ( a windows line ending is \r\n, but you probably know that).

Hoffer answered 21/2, 2013 at 14:8 Comment(7)
Pretty nice and simple, I like it :) The only drawback I can see is I will have one error for each line instead of one error by file but that's not big deal!Kowal
Just make a regexp that will only match the first windows newline.Hoffer
How do you make a regexp that only match the first Windows newline per file? I use the RegexpMultiline with format="\r\n" but each line ending with a Windows new line is detected as an error.Kowal
"\r\n.*" doesn't work (it still detects one error per line) but "(?s:\r\n.*)" seems to do the job! Thanks for the help.Kowal
How about add some details about how to config that on Checkstyle?Hermann
feel free to edit the answer to add more detail. I'm fine with hinting in the right direction. We use the RegexpMultiline module checkstyle.sourceforge.net/config_regexp.html#RegexpMultilineHoffer
Note that using "(?s:\r\n.*)" instead of "\r\n" also appears to help work around a StackOverflow bug in Checkstyle, see github.com/checkstyle/checkstyle/issues/48Charlotte
M
3

We use the following Checkstyle config to enforce UNIX line endings:

<module name="RegexpMultiline">
    <property name="format" value="\r\n"/>
    <property name="maximum" value="0"/>
    <property name="message" value="Do not use Windows line endings"/>
</module>
Midweek answered 29/8, 2013 at 14:27 Comment(1)
This with @florent-paillard's suggestion "(?s:\r\n.*)" as a format will do the trick. Even the maximum property is not needed as it is 0 by default.Clothing
F
2

I do not know how to check the new line character but I know how to fix it automatically (I believe this is even better.)

ANT has such task and maven can run ant tasks. Take a look here for details

Furbish answered 21/2, 2013 at 13:53 Comment(1)
Hi Alex, you're right, that would be even better to fix it automatically but the problem is that when Maven is executed, source files are often already committed and pushed to the central repository. Or did I miss something from your answer?Kowal

© 2022 - 2024 — McMap. All rights reserved.