Configuring IntelliJ IDEA Class and Method Comment Templates with Groovy Scripts
This guide explains how to set up IntelliJ IDEA class and method Javadoc templates using File and Code Templates and Live Templates, including the required abbreviation, description, template text, applicable contexts, and custom Groovy scripts for automatically generating @param and @return annotations.
First, open Settings → Editor → File and Code Templates , select the Class tab, and add the class comment template shown in the example, which includes author and date placeholders that IDEA resolves automatically.
Next, configure method comment templates via Settings → Editor → Live Templates . Create a new template group (e.g., userDefine ) and add a new Live Template with the abbreviation * , description, and template text. Ensure the Expand with field is set to the Enter key.
In the template text, include placeholders such as $date$ , $time$ , $param$ , and $return$ . Since IDEA does not recognize $param$ and $return$ by default, provide custom Groovy scripts to generate the appropriate Javadoc tags.
Groovy script for generating @param annotations:
groovyScript("def result = '';def params = \"${_1}\".replaceAll('[\\[|\\]|\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {if(params[i] != '')result+='* @param ' + params[i] + ((i < params.size() - 1) ? '\r\n ' : '')}; return result == '' ? null : '\r\n ' + result", methodParameters())Groovy script for generating @return annotation:
groovyScript("return \"${_1}\" == 'void' ? null : '\r\n * @return ' + \"${_1}\"", methodReturnType())After adding the scripts, click Edit variables to assign the appropriate expressions for each placeholder (e.g., built‑in functions for date and time , custom script for param ).
Set the applicable contexts to Java so the template applies to all Java files, then save the settings. New classes and methods will now automatically include the configured Javadoc comments.
The article concludes with a Q&A section addressing common questions about the required abbreviation * , the purpose of the empty * line, and why custom scripts are needed for $param$ and $return$ handling.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.