javamaven-2checkstylecopyright-display

How do I ensure that copyright notices accompany all of my source files for a java-maven build?


Is there a standard way people enforce the inclusion of copyright notices in their java/maven builds? I realize that it shouldn't be necessary since the product itself is copy-written and if someone has my source I have much bigger problems, but I'm being asked to check and was wondering if checkstyle, PMD or something else handled this automatically.

Is there a tool which handles checking for copyright?


Solution

  • Yes, Checkstyle (and the maven-checkstyle-plugin) can do that, it can check that every source files do contain a license header. Put that header in a text file and use the headerLocation to point on it (it uses by default LICENSE.txt).

    Let's say you want to use checkstyle.license for your copyright notices. For a multi-modules build, the standard approach is to create a dedicated module to host Checkstyle resources (see Multimodule configuration):

    whizbang
    |-- pom.xml
    |-- build-tools
    |   |-- src
    |   |   `-- main
    |   |       `-- resources
    |   |           `-- whizbang
    |   |               |-- checkstyle.xml
    |   |               `-- checkstyle.license
    |   `-- pom.xml
    |-- core
    |-- gui
    |-- jmx
    `-- src
    

    Then, include the Checkstyle configuration in the top level pom.xml.

    <pluginManagement>
      <plugins>
        <!-- Apply checkstyle rules and fail the build in case of errors. The
             checkstyle config files are taken from the build-tools JAR module.-->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <!-- Lock down plugin version for build reproducibility -->
          <version>2.4</version>
          <dependencies>
            <dependency>
              <groupId>com.example.whizbang</groupId>
              <artifactId>build-tools</artifactId>
              <version>1.0</version>
            </dependency>
          </dependencies>
          <configuration>
            <consoleOutput>true</consoleOutput>
            <configLocation>whizbang/checkstyle.xml</configLocation>
            <headerLocation>whizbang/checkstyle.license</headerLocation>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    

    This setup will ensure that a copyright header is present in source files (and apply other Checkstyle rules but this is another story). Adapt it to suit your needs.