LicenseComment
Since checkstyle-extension 1.0
Description
Checks whether or not a Java source file has a valid leading license comment.
A leading license comment is valid if (in order):
- It contains the name of the file if and only if
includeFilename
is set to true. - It contains a valid copyright notice if and only if
includeCopyright
is set to true. SettingrequiredCopyrightYear
andrequiredCopyrightHolder
imply this. - There is an empty line before the remainder of the license text if and only if
includeEmptyLineBeforeLicenseText
is set. - The remainder of the license text is valid according to what's been set with either
predefinedLicenseText
orcustomLicenseText
.
Properties
name | description | type | default value | since |
---|---|---|---|---|
includeFilename | True if the license comment must include the filename, or false if it's not allowed. | boolean | false | 1.0 |
includeCopyright | True if the license comment must include a copyright, or false if it's not allowed. | boolean | false | 1.0 |
requiredCopyrightYear | The required copyright year, or current . If set to a non-blank value, will require a copyright notice. |
string | - | 1.0 |
requiredCopyrightHolder | The required copyright holder. If set to a non-blank value, will require a copyright notice. | string | - | 1.0 |
includeEmptyLineBeforeLicenseText | True if the license comment must include an empty line, or false if it's not allowed. | boolean | false | 1.0 |
predefinedLicenseText | The expected predefined license. | see below | - | 1.0 |
customLicenseText | The expected license text. | string | - | 1.0 |
The license text must be defined using either predefinedLicenseText
or customLicenseText
. The available pre-defined license texts:
- Apache-2.0: the Apache License 2.0
- BSD-2-Clause: the 2-Clause BSD License / FreeBSD License / Simplified BSD License
- EPL-1.0: the Eclipse Public License 1.0
- EPL-2.0: the Eclipse Public License 2.0
- GPL-2.0: the GNU General Public License version 2
- GPL-3.0: the GNU General Public License version 3
- LGPL-2.0: the GNU Library General Public License version 2
- LGPL-2.1: the GNU Lesser General Public License version 2.1
- MIT: the MIT License
- MPL-2.0: the Mozilla Public License 2.0
Examples
To require a simple MPL-2.0 license:
<module name="LicenseComment">
<property name="predefinedLicenseText" value="MPL-2.0"/>
</module>
Example:
/**
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
class Valid {
}
To require a filename, copyright notice and empty line before the license text:
<module name="LicenseComment">
<property name="includeFilename" value="true"/>
<property name="includeCopyright" value="true"/>
<property name="includeEmptyLineBeforeLicenseText" value="true"/>
<property name="predefinedLicenseText" value="MPL-2.0"/>
</module>
Example:
/**
* Valid.java
* Copyright 2023 John Doe
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
class Valid {
}
/**
* OtherFile.java // Violation: Invalid filename in license. Expected 'Invalid.java'.
* // Violation: Missing copyright in license.
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
class Invalid {
}
To use a custom license text:
<module name="LicenseComment">
<property name="customLicenseText" value="
This Source Code Form is subject to the terms of the Mozilla
Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at https://mozilla.org/MPL/2.0/."/>
</module>
Note that the license text needs to be set using the value
attribute. This means that characters like "
need to be replaced with the equivalent XML entity like "
, and any line breaks need to be explicitly specified as
. To keep the text somewhat readable, any common leading whitespace will be discarded, as well as any blank first and last line. Therefore, the above example is the same as using predefined license text MPL-2.0
.
Violation Messages
- licenseComment.noLicense
- licenseComment.disallowedFilename
- licenseComment.missingFilename
- licenseComment.invalidFilename
- licenseComment.disallowedCopyright
- licenseComment.missingCopyright
- licenseComment.invalidCopyright
- licenseComment.invalidCopyrightYearRange
- licenseComment.invalidCopyrightYear
- licenseComment.invalidCopyrightHolder
- licenseComment.disallowedEmptyLine
- licenseComment.missingEmptyLine
- licenseComment.licenseTextMismatch
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.github.robtimus.checkstyle.checks