Java

IDEA For Community

Products Git project IntelliJ IDEA 常用设置讲解 awesome-IntelliJ-IDEA

配置

  • 代码提示 Eclipse 的Alt + /是IEDA的: code completion 即 Ctrl + Shift + Space

Alt+/ 被 Cyclic Expand Word 占用,所以先修改Cyclic Expand Word的快捷键

  • 拼写检查 File | Settings | Editor | Inspections| Proofreading (全去掉)

Postfix Code

键入变量名.expr 可快速生成代码结构

可以在 Editor | General | Postfix Completion 查看更多

show Bytecode

View -> Show Bytecode 可显示编译后的字节码信息

注释模版

  • 新建Class 的注释模版

File | Settings | Editor | File and Code Templates Class 默认有一行 #parse("File Header.java"), 就是隔壁的 Includes File Header, SO 编辑它就行了

/**
 * @Description $Description
 * @Author yangfh 
 * @Date ${DATE} ${TIME}
**/

其 $Description 是一个参数, 新建的时候会有个弹窗要求输入

  • 方法的注释模板

实现elipase 的 /** + Enter

  1. File | Settings | Editor | Live Templates 添加一个模版组

  2. Add Live Temolates Abbreviation 设置为 * Expand with 设置为 Enter (注意)

  3. 模板及模板变量 Template text

*
 * @description: 
$params$
 * @return: $return$
 * @author: yangfh
 * @time: $date$ $time$
 */

Edit variables

params groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+=' * @param ' + params[i] + ((i < params.size() - 1) ? '\\n': '')}; return result", methodParameters())

return methodReturnType() date date() time time()

  1. 修改定义范围为java

Define Change

特殊注释 TODO

配置 File | Settings | Editor | TODO 过滤查看 View > Tool Windowns > TODO

Maven 配置

File settings Build, Execution, Deployment Maven

配置 Auto import class

https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html#automatically-add-import-statements

  1. Settings/Preferences dialog Ctrl+Alt+S, click Editor | General | Auto Import
  2. Select the Add unambiguous imports on the fly checkbox

配置 项目JDK

File Project Structre Platform Settings

配置 IDEA 运行的 JDK

Ctrl + shift + A 搜 boot Choose Boo Java Runtime for this IDE… Add custom Runtimne…

还可以通过将 IDEA_JDK 环境变量及其路径添加到所需的JDK主目录来覆盖用于IntelliJ IDEA的运行时;

配置全局生效

2020以后的版本: File New Project Settings Settings for New Projects

配置同步

idea 支持git 仓库配置痛同步, 但是git 没有免费的私有仓库..

用gitee替代…

参考官文

  1. Create a Git repository on any hosting service, such as GitHub.

  2. On the computer where the IntelliJ IDEA instance whose settings you want to share is installed, select File | Manage IDE Settings | Settings Repository from the main menu. Specify the URL of the repository you’ve created and click Overwrite Remote.

  3. On each computer where you want your settings to be applied, select File | Manage IDE Settings | Settings Repository from the main menu. Specify the URL of the repository you’ve created, and click Overwrite Local.

You can click Merge if you want the repository to keep a combination of the remote settings and your local settings. If any conflicts are detected, a dialog will be displayed where you can resolve these conflicts.

If you want to overwrite the remote settings with your local settings, click Overwrite Remote.

与 eclipse 快捷键的差异

功能描述Eclipse 快捷键IntelliJ IDEA 快捷键
 显示上下文菜单 (Source/Refactor)  Alt + Shift + S / L  Alt + Insert (生成) / Ctrl + Alt + T (重构)
 包裹代码 (Surround With, e.g., if/try)  Alt + Shift + Z  Ctrl + Alt + T 
 提取常量 (Extract Constant)  Alt + Shift + C  Ctrl + Alt + C 
 提取字段 (Extract Field)  Alt + Shift + F  Ctrl + Alt + F 
 内联变量/方法 (Inline)  Alt + Shift + I  Ctrl + Alt + N 
 改变方法签名 (Change Signature)  Alt + Shift + C (需选对) Ctrl + F6 
 实现接口/重写方法  Alt + Shift + S  Add Unimplemented… Ctrl + I (实现) / Ctrl + O (重写)
 查看参数信息 (Parameter Info)  Ctrl + Shift + Space (部分版本) Ctrl + P 
 快速文档 (Quick Documentation)  F2 (或悬停) Ctrl + Q 
 类型信息 (Type Info)  F2  Ctrl + Shift + P 
 展开/折叠代码块  Ctrl + Shift + + / -  Ctrl + + / - (数字键盘) 或 Ctrl + Shift + . / , 
 折叠所有方法  Ctrl + Shift + - (全部) Ctrl + Shift + - (全部) / Ctrl + . (当前)
查找类 (Open Type)Ctrl + Shift + TCtrl + N
查找文件 (Open Resource)Ctrl + Shift + RCtrl + Shift + N
查找符号 (方法/字段名)-Ctrl + Alt + Shift + N
全局搜索任意内容Ctrl + H (搜索对话框)Double Shift (连按两次 Shift)
在文件中查找文本Ctrl + FCtrl + F
在整个项目中查找文本Ctrl + H File SearchCtrl + Shift + F
替换 (全局)Ctrl + H File Search (Replace)Ctrl + Shift + R
查找用法 (Find Usages)Ctrl + Shift + GAlt + F7
查找用法 (弹出窗口)-Ctrl + Alt + F7
最近编辑的文件Ctrl + ECtrl + E
最近打开的工具窗口-Ctrl + Backtick (`)
跳转到指定行Ctrl + LCtrl + G
跳转到匹配的大括号Ctrl + Shift + PCtrl + Shift + M
查看方法层次结构 (Call Hierarchy)Ctrl + Alt + HCtrl + Alt + H
查看类型层次结构 (Type Hierarchy)F4Ctrl + H
超级定义 (Super Implementation)Ctrl + TCtrl + U

webapp项目支持

仅专业版以上支持

配置项目

添加 Facets

右键项目模块 Add frameworks Suppers Web application 会生成web目录, 包括 WEB-INF/, WEB-INF/web.xml

File Project Structure Facets 同样可以配置 Web Services

  • Deployment Desciptors 配置 > web.xml
  • Web Resource Directores 配置 Servlet app 资源目录

添加 Artifact 和 lib

File Project Structure Artifacts

添加 Web application Exploaded 它会把这个 <ouput root> 当作Servlet webapp的根目录

  • 添加本地源码项目编译的class, 选中项目右键: Put into WEB-INF/classes
  • 添加本地源码项目依赖的库, 选中项目的lib右键: Put into WEB-INF/lib
  • 添加本地源码项目web标记的资源: add copy of >Java EE Facet Resources
  • 添加本地项目资源: Directory Content > 添加上模块的 resources 目录

配置 Tomcat

在IntelliJ IDEA中配置Tomcat的步骤如下:

在IDEA右上角的项目运行列表中选择“Edit Configurations”。 在新打开的窗口点击“+”。 在列表中找到并点击“Tomcat Server”下的“Local”。 配置Tomcat路径,选择已安装的Tomcat路径,如果没有自动找到,可以手动添加。 配置Java环境,选择已安装的JDK。 在“服务器”页面进行其他配置,如名称和URL地址(通常使用默认值)。 点击“部署”,然后左上角的“+”,选择工件进行部署。 确认配置无误后,点击“确定”完成配置。

关于更新 Kotlin

IntelliJ IDEA and Android Studio suggest updating to a new release once it is out. When you accept the suggestion, it automatically updates the Kotlin plugin to the new version. You can check the Kotlin version in Tools | Kotlin | Configure Kotlin Plugin Updates.

踩坑指南

不索引文件?

点击 node_modules 文件夹 — 右键 — Mark Directory as — Excluded File — Settings — Editor — File Types,将 node_modules 添加

不识别项目结构?

File Project Structure > Modules > Import Module [选择类型Next]

热更新代码?

热启动: 修改代码后, 服务可自动重启, 通过console可看出, 这种做法并不好用! 热部署: 修改代码后, 服务不需要重启, 只替换相关的class即可生效;

不要用 spring-boot-devtools, 会重启项目 变成热启动. (可以配置禁用重启, 但是.. 不纯粹!)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>runtime</scope>
</dependency>

热部署

可手动 Ctrl + Shift + F9 重编译文件,然后弹窗询问替换class, 可实在是..

  1. 配置不弹窗询问: File | Settings | Build, Execution, Deployment | Debugger | HotSwap

  2. 配置idea自动编译 (仅在未运行或者debugger时) File | Settings | Build, Execution, Deployment | Compiler| Build project automatically 没有用需要 Ctrl + Shift + Alt + / | Registry.. | compiler.automake.allow.when.app.running (勾选)

最后 修改文件会自动编译了, 还是不会自动 reload class, 要手动

安装这个插件?

一些好用的插件

translation

TranslationPlugin

codota

Better code completions using AI for Java and JavaScript. Codota uses the most advanced Machine Learning models to boost productivity and save time for developers. Supports IntelliJ, Android Studio, WebStorm, and PhpStorm

codota codota - plugins.jetbrains

比较好用的是 Snippets 当我们第一次使用某个类, 某个函数不够熟悉时可以一些开源项目的代码块 供参考

踩坑指南

中文乱码

File | Settings | Editor | General | Console 设置为:UTF-8 Settings > Editor > File Encodings > Global Encodings & Project Encodings 设置为:UTF-8 Settings > Build, Execution, Deployment > Compile > Java Compiler > Additional command line parameters > 添加参数:-Dfile.encoding=UTF-8

Help > Etit Custom VM Options 添加一行-Dfile.encoding=UTF-8

Run 命令参数过长

Resolving Maven 卡死

大项目 Maven依赖过多, 会卡死在 Resolving Maven dependencies…

File | Settings | Build, Execution, Deployment | Build Tools | Maven | Runner 在VM Options 增加参数: -DarchetypeCatalog=local 优先本地找

File | Settings | Build, Execution, Deployment | Build Tools | Maven | Importing 在VM Options for Importing 增加参数: -Xms1024m -Xmx2048m