Add Copyright to Code File

Recently, while working on an open-source project, I was informed just before going live that copyright attribution needed to be added to every program file. Due to time constraints, I gave up on finding an automated solution and opted for manual editing. Resolving the issue took a few minutes of manual effort.

To efficiently solve similar problems in the future, an efficient/automated method is needed. Manual editing is cumbersome and prone to errors, especially when dealing with hundreds of program files.

During the holidays, I researched the feasibility of performing this operation within the IDE and discovered a solution.

The following method uses JB company’s IDEs, such as WebStorm and IntelliJ IDEA.

If you want to add a copyright to existing files in a project uniformly, you can follow the steps below:

  1. Configure Copyright, Editor-Copyright-Copyright Profiles

  2. Configure Scope for Copyright, Editor-Copyright

    If the requirement is simple and all program files have the same Copyright, configure it as shown below:

  3. Apply the copyright configuration to all files

    • In the resource directory, select the root folder, right-click, and choose Update Copyright..., and the IDE will automatically add the copyright notice to all code files.
    • The IDE will automatically exclude files not under VCS, such as node_modules, and non-program files, such as JSON, will also be excluded automatically.

After the above operations, you will find that all program files have successfully added the copyright notice, so easy.

Sometimes, the copyright information needs to be updated.

  1. If you only want to update modified files, you can check Update Copyright when submitting, and this method will automatically update the copyright information of the modified files.
  2. If you’re going to update all files, simply select the folder in the resource directory, right-click Update Copyright, and you are done.

WebStorm and other IDEs do not support sharing copyright settings across projects. If needed, you have to copy the settings between projects manually.

Someone has already raised a related issue, and you can follow it if you are interested:

https://youtrack.jetbrains.com/issue/IDEA-243167?_ga=2.243406598.2075586931.1632032900-927496416.1626448071

At the end

After understanding this method, there will be no need for manual batch editing of copyright information in the future.

Docs