磨刀不误砍柴工,在写代码之前还有许多事情要做。
-
下载JDK,虽然是个人的代码,但也不能给Oracle留下任何小把柄。选择jdk1.8.0u191及之前的版本就好了。以目前Spring Boot 2.x的版本来说,还是尽量避免踩JDK8之后的坑;
-
安装IntelliJ IDEA,目前版本是2019.1,理论上够用就行了。
1.1. 配置Maven
下载IDEA后应该先将Maven路径指向已安装Maven的目录,虽然不配置也可以用。
在File >> Settings >> Build, Execution, Deployment >> Build Tools中找到“Maven”并修改它的
- “Maven home directory”
- “User settings file”
- “Local repository”
通常修改“Maven home directory”之后,“User settings file”会自动指向$MavenHome$/conf/settings.xml,“Local repository”也会随之变化。
settings.xml参考:
<settings>
<!-- 配置自己的local repository -->
<localRepository>D:\maven-repository</localRepository>
<mirrors>
<!-- 使用阿里的镜像仓库 -->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<!-- 锁定Maven项目的编译版本 -->
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
阿里云的Maven仓库见:https://maven.aliyun.com/mvn/view
1.2. IDEA插件安装
阿里出台代码规范已经有很久,而且日常也会遵循他们的编码规范,所以第一步就安装个小插件吧。
- 阿里P3C插件 — https://github.com/alibaba/p3c
在File >> Settings >> Plugins中搜索“alibaba”,找到“Alibaba Java Coding Guidelines”安装。
- Lombok懒人工具
在File >> Settings >> Plugins中搜索“lombok”,找到“Lombok”安装。当然还需要在对应的工程中添加它的依赖。
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version> <!-- now 1.18.6 -->
<scope>provided</scope>
</dependency>
1.3. 添加注释模板和版权信息
1.3.1. 添加File Header
在File >> Settings >> Editor >> File and Code Templates中找到标签页“includes”,并编辑“File Header”
/*
* Copyright [${YEAR}] [${USER}]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
/**
* TODO
*
* @author ${USER} Email: 邮箱地址
* @date ${YEAR}-${MONTH}-${DAY}
* @since 1.0.0
*/
简单解释下内容:
- 中间分割是输出包名的脚本代码,其意为如果包名存在且不为空,则输出该包名;
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
-
上方注释为Copyright信息,这里用到了Apache License Version 2.0,主要也是为了免责和开源精神;
-
下方注释为类文件注释信息,其中:
- TODO应改为类的描述(如作用等);
- @author说明类作者;
-
@date描述了类创建日期(格式为年-月-日,需要时间可自己加
${TIME}
) - @since说明创建时代码的版本、。
再回到“includes”左边的标签“Files”,将Class、Interface、Enum、AnnotationType、package-info中的包名输出脚本去掉。
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
对,还是这一段,因为已经引入了刚才修改的File Header#parse("File Header.java")
,那就不能再让它创建两个相同的package。
1.3.2. 添加Live Templates快捷键
1.3.1.1. 方法注释模板
快捷键:先在编辑器中输入“/*”,然后按下“tab”键(Default)
在File >> Settings >> Editor >> Live Templates的右侧栏,点击“+”创建一个“Template Group”,命名为“Custom”,然后选中“Custom”继续点击“+”创建“Live Template”:
-
设置abbreviation为“*”;
-
添加Template Text内容如下:
**
* TODO$param$$returns$
* @date $date$ $time$
* @author $user$
* @since 1.0.0
*/
注:此处注释开头不带斜杠“/”
- 点击“Edit Variables”输入对应参数的“Expression”:
- $param$对应
groovyScript("def params=\"${_1}\"; def result='\\n *'; params=params.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {if(params[i] == '') continue; result+='\\n * @param ' + params[i] + ' param' + (i + 1)}; return result", methodParameters())
脚本描述为:
由于methodParameters()方法输出的字符串形式为[a, b, c],因此需要替换掉无用的中括号即(.replaceAll('[\\[|\\]|\\s]', ''))并分割为数组;
但即使方法没有参数,在数组中都会有一个空的元素存在,因此foreach中会判断每次循环的元素是否为空即(params[i] == ''),不为空才会将其设置到新行中并输出为(“@param paramName paramIndex”);
- $returns$对应
groovyScript("def result=\"${_1}\"; if(result == 'void') return ''; result=result.replace('<', '<').replace('>', '>'); return '\\n * @return ' + result;", methodReturnType())
脚本描述为:
如果返回类型为“void”(一般不会为空字符串),就返回为空字符串;
当返回类型包含泛型时,而泛型附带的“<>”会影响JavaDoc的观感,故将这些符号替换为转义后的'<>';
最后再换行输出为(“@return returnType”)
- $date$、$time$、$user$则对应
# 格式化的日期
date("yyyy-MM-dd")
# 格式化的时间
time("HH:mm")
# 当前系统登录用户,可在VMOptions中设置-Duser.name=Xxx,也可写死或设置默认值
user()
![](https://img.haomeiwen.com/i17478770/11d9b217505accd2.png)
- 设置“Define”选中“Java”复选框。
![](https://img.haomeiwen.com/i17478770/4e4f693ce8c7667e.png)
简单解释下内容:
-
设置快捷键为“*”而不是“/*”,是因为“/”是个转义符,但在实际使用中仍需要在方法上方输入“/*”,而后按下 “tab”才能取到方法的参数列表和返回类型。在右下角Options >> Expand With中可设置组合快捷键为“空格”、“回车”或其它。
-
$param$、$returns$对应的是表达式和方法,需要在“Edit variables”中为其赋值;
-
使用模板的范围需要在“Define”中定义,可全选。
1.3.1.2. 类注释模板
快捷键:先在编辑器中输入“/**”,然后按下“tab”键(Default)
与方法注释模板相同,继续创建“Live Template”:
-
设置abbreviation为“**”;
-
添加Template Text内容如下:
**
* TODO
*
* @author $user$ Email: 选填,邮箱也可设置为其它
* @version 1.0.0
* @date $date$
*/
注:此处注释开头不带斜杠“/”,当然也可以配置成带“/”
$date$、$user$对应
# 格式化的日期
date("yyyy-MM-dd")
# 当前系统登录用户,可在VMOptions中设置-Duser.name=Xxx,也可写死或设置默认值
user()
1.3.1.3. Copyright注释模板
快捷键:先在编辑器中输入“/***”,然后按下“tab”键(Default)
与方法注释模板相同,继续创建“Live Template”:
-
设置abbreviation为“**”;
-
添加Template Text内容如下:
*
* Copyright [$year$] [$user$]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
这里同样$year$、$user$对应
# 格式化的日期,只要年份
date("yyyy")
# 当前系统登录用户,可在VMOptions中设置-Duser.name=Xxx,也可写死或设置默认值
user()
“类注释模板”和“Copyright注释模板”不再细述,简单列举下Live Templates和File Header的区别:
Live Templates | File Header |
---|---|
在编写代码的过程中使用组合快捷键生成注释 | 在创建文件时自动根据Header格式生成对应格式的文件 |
使用表达式、脚本、方法注入可能会产生变动的属性值 | 使用环境变量、虚拟机参数、已配置文件生成对应的属性值 |
1.3.1.4. File >> Settings >> Editor >> Copyright
这里也可以配置Copyright,但好像不能动态加入年份和用户,因此不推荐使用。
IDEA配置文件:https://github.com/df-zhang/my-spring-cloud/blob/master/settings.zip
网友评论