IntelliJ - Update Copyright Year
Asked Answered
F

2

7

I have a copyright profile defined in IntelliJ as Copyright ${today.year}, All Rights Reserved. This works great on new files, however on existing files I would like to update the existing year to a range or update the end-date of an existing range to ${today.year}.

IntelliJ supports using Velocity in copyright templates. Is there a way to leverage Velocity to do this? From what I can tell, Velocity allows for if-statements yet I am unsure how to get the previous years as variables.

Examples:

New document: Copyright 2018

Copyright 2017 to Copyright 2017-2018

Copyright 2015-2017 to Copyright 2015-2018

Reference:

IntelliJ supported variables

Velocity User Guide

Forbidden answered 9/1, 2018 at 16:54 Comment(3)
Just an idea, could you do this with structural search and replace?Historied
@Historied Potentially. Yet that would need to be triggered manually every time a file is modified. Using the built in template allows the copyright to be created/updated auto-magically.Forbidden
@Forbidden Did you find a solution?Nyctophobia
K
3

It is possible now. More information in the JetBrains documentation.


Add this to your Copyright matcher:

Copyright $originalComment.match("Copyright (\d+)", 1, "-")$today.year

This does exactly this:

New document: Copyright 2022

Copyright 2017 to Copyright 2017-2022

Copyright 2015-2022 to Copyright 2015-2022

Klemens answered 17/11, 2022 at 20:38 Comment(0)
E
1

I am unsure how to get the previous years as variables

As far as I can see, exactly this will be the breaker. If you could access the creation date of the file, it would be simple to compare "today" with the year of creation and make a copyright you want.

On the other hand, those copyrights are usually project-based, although they appear in each file. I never saw that files have different entries for the date in the copyright.

Therefore, if you want a simple solution, just use

Copyright 2017-${today.year}

If you happen to start a project 2018, just use 2018 and when the year changes, make the above adaption to your copyright and update all files. You need to do this only once with new projects and spending even 15 min fiddling around with the Velocity templates is more time than you need to make the adaption in the copyright.

Esemplastic answered 9/1, 2018 at 17:53 Comment(1)
That works well for small projects. However if you are modifying several multi-module projects, the overhead can quickly escalate. Would prefer to have a single template, sharable between teams, to maintain this.Forbidden

© 2022 - 2024 — McMap. All rights reserved.