10

I want to ensure CRLF (Windows style) line separator in my project. I am checking a few alternatives, but not sure which one is the best for this.

Desired Behavior:

If a file is not CRLF, which IntelliJ IDEA shows at the bottom:

I want my maven profile, let's say mvn clean install -P tests to fail, saying something like "Invalid line separator, not CRLF"

Thanks a lot.

stack man
  • 2,303
  • 9
  • 34
  • 54

3 Answers3

19

I don't think it is possible to cause the Maven build to fail due to invalid line separators in your project's files unless someone has created a plugin to do that. However, you can configure a code inspection in Intellij IDEA to fail for that reason. This is how you could provoke such a failure:

  • Navigate to File -> Settings -> Editor -> Inspections -> Inconsistent Line Separators then check the box for Inconsistent Line Separators and select Error from the drop list for Severity:

IDEAsettings

  • Navigate to File -> Settings -> Editor -> Code Style and specify the default line separator by selecting Windows (\r\n) from the Line separator drop list (if not already set).
  • Invalidate the line separator setting for some open file in your project. For example: File -> Line Separators -> CR - Classic Mac (\r)
  • Run an inspection on your project (Analyze -> Inspect Code -> Whole Project) and you should now get an error:

IDEAinspection

JetBrains has an open bug ticket to force compilation failure based on inspection errors, so this approach is not exactly what you were asking for. But in the absence of any Maven-based solution it might be best you can do. See the Code Inspection documentation from JetBrains for more information.

One other possible approach is to look at TeamCity, another JetBrains tool for continuous integration. I haven't used it, but perhaps it allows you to configure failure when there are inspection errors (though from a quick look at their documentation I couldn't see how).


Update:

It looks like TeamCity might be worth a look after all. Its documentation on Build Failure Conditions states:

When using code examining tools in your build, like code coverage, duplicates finders, inspections and so on, your build generates various numeric metrics. For these metrics you can specify a threshold which, when exceeded, will fail a build.

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • thank you so much for your superb response buddy, I am going to take a look at it, but after a whole day of investigation yesterday, I am starting to suspect that my Jenkins changes CRLF files to LF! Any hint? Thank you so much – stack man Jan 31 '18 at 08:20
  • @stackman If you use Git I would think that is more likely than Jenkins to be relevant...but just a guess. – skomisa Jan 31 '18 at 08:53
  • Any hint about how can I force CRLF always on Git? – stack man Jan 31 '18 at 09:06
  • Ty you are the king – Eng Nov 24 '20 at 11:28
2

Previous answer stated:

I don't think it is possible to cause the Maven build to fail due to invalid line separators in your project's files unless someone has created a plugin to do that.

But there is plugins for that. You can have a checkstyle rule:

<module name="RegexpMultiline">
    <property name="format" value="(?<!\r)\n"/>
    <property name="maximum" value="0"/>
    <property name="message" value="Invalid line separator, not CRLF"/>
</module>

And then configure build to use checkstyle plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <failsOnError>true</failsOnError>
        <consoleOutput>true</consoleOutput>
        <includeTestSourceDirectory>true</includeTestSourceDirectory>
        <checkstyleRules>
            <module name="Checker">
                <module name="RegexpMultiline">
                    <property name="format" value="(?<!\r)\n"/>
                    <property name="maximum" value="0"/>
                    <property name="message" value="Invalid line separator, not CRLF"/>
                </module>
            </module>
        </checkstyleRules>
    </configuration>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>checkstyle</goal>
             </goals>
        </execution>
    </executions>
</plugin>

It binds to "verify" phase by default that should be already activated if you run install.

eis
  • 51,991
  • 13
  • 150
  • 199
  • This approach is much better than the one I gave in my answer since it is independent of the IDE being used. See also [How to make Maven checking the source end-of-line?](https://stackoverflow.com/q/15003871/2985643) which is not a duplicate, but relevant. – skomisa Oct 22 '21 at 15:49
1

I had the same issue on Linux. When I commit with IDEA then I get my line separators converted to LF. When I do commit with SmartGit then all stays CRLF.

All I did - command:

git config --global core.autocrlf false

And now everything is fine.

Mike Menko
  • 819
  • 8
  • 9
  • The question is unrelated to version control. What git settings you use is a bit beside the point here, even though that too affects the situation. – oligofren Mar 11 '21 at 11:40