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 :)

Samuel EUSTACHI
  • 3,116
  • 19
  • 24
Florent Paillard
  • 549
  • 1
  • 6
  • 20
  • 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. – guido Feb 21 '13 at 14:04

3 Answers3

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

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • 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! – Florent Paillard Feb 21 '13 at 14:53
  • Just make a regexp that will only match the first windows newline. – kutschkem Feb 21 '13 at 15:08
  • 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. – Florent Paillard Feb 22 '13 at 15:44
  • 2
    "\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. – Florent Paillard Feb 26 '13 at 16:11
  • How about add some details about how to config that on Checkstyle? – rafa.ferreira Jun 19 '13 at 13:14
  • feel free to edit the answer to add more detail. I'm fine with hinting in the right direction. We use the RegexpMultiline module http://checkstyle.sourceforge.net/config_regexp.html#RegexpMultiline – kutschkem Jun 19 '13 at 14:13
  • Note that using "(?s:\r\n.*)" instead of "\r\n" also appears to help work around a StackOverflow bug in Checkstyle, see https://github.com/checkstyle/checkstyle/issues/48 – vorburger Jun 18 '15 at 13:55
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>
Andrew Gaul
  • 2,296
  • 1
  • 12
  • 19
mwuertinger
  • 217
  • 3
  • 13
  • 2
    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. – Kuitsi Jan 29 '14 at 19:37
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

Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 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? – Florent Paillard Feb 21 '13 at 14:51