最新版本2021.3:https://www.jetbrains.com/help/idea/2021.3/enabling-version-control.html#associate_directory_with_VCS
基础配置
Maven
File → Settings → Editor → Build,Execution,Deployment → Compiler → Build Tools → Maven,配置本机的 Maven 安装目录和 settings 目录、repository目录
自动下载pom依赖
File → Settings → Build,Execution,Deployment → Build Tools → Maven → Importing,在 Automatically download 项 勾选 Sources、Documentation、Annotation。OK
自动导包
File → Settings → Editor → General → Auto Import,Add unambiguous imports on the fly:自动导入不明确的结构,Optimize imports on the fly:自动帮我们优化导入的包
在 pom 文件中添加依赖,右键 Maven → Reload project
自动编译
File → Settings → Editor → Build,Execution,Deployment → Compiler,勾选 Build project automatically 和 Compile independent modules in parallel
编码格式
File → Settings → Editor → File Encodings,设置三个地方:Global Encoding、Project Encoding、Properties Files并勾选 Transparent native-to-ascii conversion
JDK
File → Project Structure → SDKs,选择jdk,选择jdk安装目录即可
添加按钮到顶部
Appearance & Behavior → Main Toolbar,点击 Search Everywhere,点击 + → Add Action → Main Menu → File,选择 Preferences 和 Project Structure
文件修改后,目录颜色变化
File → Settings → Version Control → Confirmation,勾选 Highlight directors…
选择关键字后相当的关键字高亮
IDEA 2022:Preferences(直接配置到工具栏) → Editor → Color Schema → General → Code → Identifier under caret,右侧 Backgroud 选择背影颜色
大小写提示
IDEA 2021 File → Settings → Editor → General → Code completion,取消勾选 Match case。
多行显示
平时开发可能会开很多个类,默认情况下会堆积在一行显示,右侧通过箭头筛选的方式选择其他文件,开启该配置以后所有文件可以多行显示在头部方便查看。
editor->general->editor tabs->show tabs in one row 前面的勾去掉
显示行号和方法分割符
File → Settings → Editor → General → Appearance,勾选:Show line numbers 和 Show method separators
去除@Autowired注入对象带来的下划线提示
File → Settings → Editor → Inspections → Spring → Spring Core → Code,去掉 Non recommended field injections 勾选,Incorrect injection point autowiring in Spring bean components 级别调为Warning
去除注释@param红色报错
File → Other Settings → Default Settings,搜索框输入 javadoc,配置 Declaration has problems in Javadoc references 项 Serverity 为 No highlighting,only fix
复制代码取消灰色波浪线提示
*File → Settings(输入 inspections 搜索),取消选择 Duplicated Code
类注释模板
File → Other Settings → Default Settings → Editor → File and Code Templates,如下图所示:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
/**
* @Author Yincn
* @Date ${DATE} ${TIME}
*/
public class ${NAME} {
}
设置方法注释模板
File → Other Settings → Default Settings → Editor → Live Templates,先创建组,再创建 templates
类注释告警
idea中注释上出现Warning Tag解决方法:
File——>settings——>Editor——>inspections——>Java——>Javadoc——>Decleartion has Javadoc problems——>Serverity选择:No highlighting…
取消access can be private提示
File -> Settings -> Editor -> Inspections -> 列表选择Java -> Declaration redundancy -> Declaration access can be weaker后面的勾去掉,Apply,OK。
顺便把IDEA默认的设置也改了:
File -> Other Settings -> Default Settings -> Editor -> Inspections -> 列表选择Java -> Declaration redundancy -> Declaration access can be weaker后面的勾去掉,Apply,OK。
加载大文件时报错:The file size exceeds configured limit
Help -> Edit Custom Properties,添加 idea.max.intellisense.filesize=50000,重启
查看类的所有方法
方法一:Alt+7,方法二:Ctrl+F12,自定义 File Structure,Keymap里配置File Structure
Commit时,取消code analysis
File → Settings → Version Control → Commit,取消勾选:Analysis code 和 Check TODO(Show All) Configure
application.yml 显示spring图标
进入 Project Structure → Modules,选择当前项目(比如 vt-nontrade),如果没有Spring,右键 Add spring;如果有,点击 Spring,点右侧+号,指定到application.yml文件即可。
配置run dushboard
设置打开Run Dashboard (View-》Tool Windows-》Run Dashboard),如果是灰色,则在 Edit Configurations 里面配置 Spring Boot 启动文件,不能是 Application
通用代码模板
步骤:创建模板组 → 创建模板
创建模板组:File → Setting → Editor → Live template,点击右侧 + 号,选择 Template Group,输入模板组名,OK
创建模板:File → Setting → Editor → Live template,点击右侧 + 号,选择 Live Template,abbreviation(快捷键):根据自己习惯配置,Description(描述):随便写,Template text(模板代码):根据需要配置,Defined:选择 Java(最重要)。
案例:($start$ 表示光标位置)
将模板加入模板组:右键新创建的模板,在弹出框中点击Change context…,选择归属组(比如Java)。创建完成
try {
$start$
} catch (Exception e) {
log.error(e.getMessage());
}
logger 模板
创建模板:File → Setting → Editor → Live template,点击右侧 + 号,选择 Live Template
显示隐藏忽略文件/文件夹
Project → Show Options menu → 勾选/取消勾选:Show Excluded Files
JVM
-Xms1024m
-Xmx1024m
-XX:MetaspaceSize=64m
-XX:MaxMetaspaceSize=256m
-XX:MaxDirectMemorySize=256m
视图
菜单栏View → Appearance,勾选 Toolbar 、Navigation Bar、Tool Windows Bars
插件
maven helper
安装后打开 pom 文件,多了 dependency analyzer 选项卡
Mybatis Log Plugin(直接显示 sql 脚本)
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 Mybatis Log Plugin, → Install。
使用方法:项目日志级别调为 debug,Tools → Mybatis Log Plugin,底部会显示按钮,启动项目,查询数据库,会显示可直接执行的 sql 语句。
控制台日志过滤插件
Preferences → Plugins,搜索:grep-console,安装并重启。启动项目打开Console,点击 Open Grep Console setting 按钮,在Filters 栏目里配置关键字过滤,比如:
# 关键字过滤
# 过滤包含 QUEUE_DEFAULT_CHANNEL_XX 的日志,Filters 栏目里 default 列里面输入下面表达式(其它默认即可):
.*QUEUE_DEFAULT_CHANNEL_XX.*
# 不同日志级别设置不同背景颜色
在 Highlights 栏里设置
Free MyBatis plugin(mapper跳转)
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 Free MyBatis plugin, → Install。
jclasslib(字节码工具)
IDEA集成安装:Setting → plugins → Browse Repositories ,搜索 jclasslib → 选择安装好后重启IDEA即可
显示插件:view栏,选择 ”Show Bytecode with jclasslib” 即可显示出来了
Statistic(代码统计)
经查插件版本4.0以上适合IDEA2020
更换插件版本3.4适配本地IDEA2018后解决
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 Statisties,选择带有 REPORTING的, → Install;
下载安装:https://plugins.jetbrains.com/plugin/4509-statistic/versions/stable/82816,下载 Statistic-3.8.1.jar,Settings → Plugins → Install plugin from disk,选择文件 → 安装。重启IDEA后底部会显示按钮,点 refresh,即会出统计数据。
SequenceDiagram(序列图)
IDEA2020才能用
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 Sequence,选择 SequencePlugin, → Install。
使用方法:选中方法名(注意不要选类名),然后点击鼠标右键(显示可选项),选择Sequence Diagram
CodeGlance(代码地图)
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 CodeGlance, → Install。
Rainbow Brackets(彩虹扩号)
IDEA集成安装:Settings → Plugins → Browse Repositories,搜索 Rainbow Brackets, → Install。
区域代码高亮快捷键:ctrl + 鼠标右键,非选中部分暗淡效果快捷键:alt + 鼠标右键
快捷键
查看实现关系
鼠标放在接口名上或双击选中,Ctrl+T。或者 F4 调出右侧 Hierachy窗口。或者 Navigate → Type Hierarchy
查看继承关系
鼠标放在类名第一行末尾,Ctrl+Alt+U
try catch
在idea中添加try/catch的快捷键,使用 ctrl+alt+t,选中想被try/catch包围的语句,同时按下ctrl+alt+t
SVN相关
关联到SVN
- 配置SVN客户端命令
File → Settings → Version Controller → Subversion,在右侧的 Use command line client栏里设置 svn 客户端执行程序,勾选 Enable interactive mode - 开启SVN
VCS → Enable Version Controll Integration - 右键项目 → Subversion → Share Directory,选择要上传到的SVN地址目录下
- 右键项目 → Subversion → Commit Directory,有选择的进行文件上传,不必要的文件不要上传,通常只上传 pom.xml 和 src 目录
断开SVN连接
- File → Settings → Plugins,在右边搜索框输入“SVN”,搜索。选择“SVN disconnect”,安装此插件
- VCS → SVN disconnect,完成SVN版本管理移除
提交时一直 Commit Changes Dialog local changes refresh
File → Settings → Version Controller → Background,设置 VCS History cache settings 的值
Git相关
拉取代码
Mac操作:选择一个文件夹位置找开终端
git init
git clone GitLab-Url
# 会提示输入用户名:粘贴进去,gitlab的登录邮箱号
# 会提示输入密码:粘贴进去,gitlab的登录密码
配置忽略
IDEA忽略
IDEA忽略与Git忽略是两回事,仅限于在IDEA里不显示,配置方法:File → Settings → Editor → File Types → Ignore files and folders,点添加按钮 → 输入后回车 → Apply
Git忽略
File → Settings → Version Control → Ignored Files(也可以在IDEA工具底部点击快捷按钮,调出配置窗口)
删除仓库文件
以删除仓库target目录为例
- File → Settings → Build,Excution,Deployment → Compiler,取消勾选 Build project automatically
- 在IDEA里删除target目录
- commit、push
- 再恢复勾选 Build project automatically。
说明:由于勾选 Build project automatically后,target目录会自动生成,所以需要取消勾选。其它文件可不用执行这一步操作。
重置版本
- IDEA里面工程名右键 -> git -> show history,找到要回退的版本,选择 Reset Current Branch to Here…
- 终端里执行命令:git push -f
创建父子工程示例
创建父工程
-
IDEA菜单栏New → Project → Maven(JDK默认1.8) → Next
-
New Project界面:GroupId:com.demo,ArtifactId:microservice-demo,Next
-
Project name:microservice-demo-parent,Project location:D:\DevInstall\workspace-idea\microservice-demo,Next
-
提示 Directory does not exit(目录不存在,是否创建),点击 Ok
-
自动打开当前项目,右下角弹出 Maven projects need to be imported,点击 Enable-Auto-Import
-
在当前窗口打开项目,删除src文件夹
-
编辑pom文件,添加springboot和springcloud依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>microservice-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
</dependency>
</dependencies>
</project>
创建子工程
公共子工程
- 父工程上右键,New → Module,Next
- ArtifactId:microservice-demo-common,Next
- 配置 Module name:microservice-demo-common,配置 Content root 和 Module file location 都是 D:\DevInstall\workspace-idea\microservice-demo\microservice-demo-common,Finish
常见问题
IDEA项目没有maven
Idea创建maven项目后,右边没有maven工具栏以及右键没有maven菜单栏
在项目 pom.xml 文件中,点击右键,选择 Add as Maven Project,就可以看到maven选项
父模块消失
通常是改名后造成的,解决办法:找到父模块目录下. idea – modules.xml,发现modules 缺少了父模块,加入就好了,如果是改名造成的,直接将新名字替换旧名字即可。
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/core-common/core-common.iml" filepath="$PROJECT_DIR$/core-common/core-common.iml" />
<module fileurl="file://$PROJECT_DIR$/product-server/product-server.iml" filepath="$PROJECT_DIR$/product-server/product-server.iml" />
</modules>
</component>
</project>
右键新建选项没有Java class选项
把sec目录设为源码目录。首先打开Project Structure,选中要调整的模块,在Sources 栏里路径选择到 java级别,对应到右侧的 Source Folders即可
子模块生成out文件
打开Project Structure,设置 Project complier output:D:\DevInstall\workspace-idea\microservice-demo\microservice-demo-common\target 即可。
depenceicies 报红
先检查maven仓库是否有jar包,如果有重启idea,reimport
Command line is too long
打开项目的工作空间,进入.idea的文件夹,打开workspace.xml文件,找到name为“PropertiesComponent”,增加一行:
<property name="dynamic.classpath" value="true" />
更改后重启idea,在启动就可以了