Header
Since Checkstyle 6.9
Description
headerFile
specifies a file that contains
the required header. Alternatively, the header specification can be
set directly in the header
property
without the need for an external file.
Notes
In default configuration, if header is not specified,
the default value of header is set to null
and the check does not rise any violations.
Properties
name | description | type | default value | since |
---|---|---|---|---|
charset | Specify the character encoding to use when reading the headerFile. | String | the charset property of the parent <a href="https://checkstyle.org/config.html#Checker">Checker</a> module |
5.0 |
fileExtensions | Specify the file extensions of the files to process. | String[] | all files |
6.9 |
header | Specify the required header specified inline. Individual header lines must be separated by the string "\n" (even on platforms with a different line separator). |
String | null |
5.0 |
headerFile | Specify the name of the file containing the required header. | URI | null |
3.2 |
ignoreLines | Specifies the line numbers to ignore when matching lines in a content of headerFile. | int[] | {} |
3.2 |
Examples
To configure the check such that no violations arise. Default values of properties are used.
<module name="Checker">
<module name="Header"/>
</module>
Example1:
package com.puppycrawl.tools.checkstyle.checks.header.header;
// OK, as by default there is not specific header defined to validate it presence
public class Example1 { }
To configure the check to use header file "java.header"
and
ignore lines 2
, 3
, and 4
and only process Java
files. 'ignoreLines' property is very useful for supporting headers that contain
copyright dates or other content that slightly different in some files:
<module name="Checker">
<module name="Header">
<property name="headerFile" value="${config.folder}/java.header"/>
<property name="ignoreLines" value="2, 3, 4"/>
<property name="fileExtensions" value="java"/>
</module>
</module>
content of "java.header" file:
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 20XX-20XX the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
Example2:
package com.puppycrawl.tools.checkstyle.checks.header.header;
/* violation on first line 'Line does not match expected header line of' */
// because headerFile is bigger then target java file
public class Example2 { }
Without the need for an external header file:
<module name="Checker">
<module name="Header">
<property name="header"
value="// Copyright (C) 2004 MyCompany\n// All rights reserved"/>
</module>
</module>
Example3:
package com.puppycrawl.tools.checkstyle.checks.header.header;
/* violation on first line 'Line does not match expected header' */
public class Example3 { }
In case target file is less in size than header file:
<module name="Checker">
<module name="Header">
<property name="headerFile" value="${config.folder}/java.header"/>
<property name="ignoreLines" value="2, 3, 4"/>
<property name="fileExtensions" value="java"/>
</module>
</module>
Example:
package com.puppycrawl.tools.checkstyle.checks.header.header;
/* violation on first line 'Missing a header - not enough lines in file' */
// because content defined in 'headerFile' is bigger then target java file
public class Example4 { }
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.header