如何在IntelliJ IDEA中设置类型的Javadoc模板?(已会Eclipse操作)
Hey there! Since you already know your way around setting up Javadoc templates in Eclipse, getting this sorted in IntelliJ IDEA is totally straightforward. Let me walk you through the steps to configure class/type-level Javadoc templates—both for auto-generating when you create new types and for quick insertion in existing code:
1. Auto-generate Javadoc when creating new classes/interfaces
This sets up IntelliJ to automatically add a Javadoc comment whenever you create a new class, interface, enum, or other type:
- Open the Settings window: Use the shortcut
Ctrl+Alt+S(Windows/Linux) orCmd+,(Mac), or go toFile > Settings(Windows/Linux) /IntelliJ IDEA > Settings(Mac). - Navigate to Editor > File and Code Templates.
- Switch to the Files tab. Here you'll see templates for all file types, including
Class,Interface,Enum, etc. - Select the type you want to configure (e.g.,
Class). In the right-hand editor, you'll see the default template structure. - Insert your custom Javadoc block right above the class definition. For example:
You can click the/** * ${description} * @author ${USER} * @since ${DATE} */Insert Variablebutton on the right to browse and add more built-in variables (like project name, package, etc.). - Hit
ApplythenOKto save your changes. Next time you create a new class, your custom Javadoc will be added automatically.
2. Quick Javadoc insertion for existing types (via /** shortcut)
If you want to generate a Javadoc comment for an existing class/interface quickly (by typing /** and pressing Enter), here's how to customize that template:
- Open Settings again and go to Editor > Live Templates.
- Expand the
javagroup in the left list, then select thedoctemplate (this is the one triggered by/**). - Click the
Edit variablesbutton on the right. - You can tweak the template text and set default values for variables. For example, modify the template to:
For class-level comments, you might want to remove/** * $description$ * @author $user$ * @since $date$ $param$ $return$ */$param$and$return$since they're more relevant for methods. Use theExpressiondropdown for each variable to set dynamic values (e.g.,user()for your username,date()for the current date). - Make sure the
Applicable insection includesJava: class,Java: interface, and other types you care about—this ensures the template triggers when you're working with those types. - Save your settings, and now typing
/**above any class/interface will generate your custom Javadoc.
A quick pro tip: If you want a consistent header across all files, you can edit the File Header.java template under the Includes tab in File and Code Templates, but for type-specific Javadoc, sticking to the Files tab templates is the way to go.
内容的提问来源于stack exchange,提问作者lapots

