VSCode

从 Sublime 投入 VSCode 怀抱。
自己一些常用插件配置。
linux 下在终端打开文件
code /xxx/xxx/xxx.xxx
屏蔽某些文件或文件夹
{ // settings.json
"files.watcherExclude": {
"**/vendor/**": true
},
"files.exclude": {
"bin": true,
"pkg": true
}
}
添加自定义环境变量
{ // settings.json
"terminal.integrated.env.linux": { // or osx/windows
"MY_ENV": "xxx"
},
}
以上设置好了重启 vscode 在终端中 echo $MY_ENV
就会看到设置的数据了
工作空间及任务
有时一个工程需要多个部分配合,但是又不想开很多个窗口,这时可以使用工作空间将其整合到一起。但是这时又有个问题是 task(任务)怎么统一使用一个呢,比如统一编译什么的,目前解决方法是再建立一个文件夹到工作空间,里面写脚本实现,大致如下:
{ // tasks.json
"version": "2.0.0",
"linux": {
"command": "/xxx/xxx/xxx" // 实现脚本路径
},
"tasks": [
{
"label": "build", // task 标签
"type": "shell",
"args": [
"build" // 脚本参数
],
"isBackground": true
},
{
"label": "clean",
"type": "shell",
"args": [
"clean"
],
"isBackground": true
}
]
}
脚本中各个工程路径可以从 xxx.code-workspace
工作空间配置中获取:
function path()
{
echo `cat .vscode/workspace.code-workspace | grep $1 | awk '{print $2}' | sed 's/\"//g'`
}
MYPROJ=`path XXX` // 获得路径
tools
- Settings Sync:同步备份插件及用户设置到 github,方便恢复
- Chinese (Simplified) Language Pack for Visual Studio Code:对于英语渣来说还是很有必要的
- TODO Highlight:代码中
TODO
会被高亮 - vscode-icons:好看
git
一般直接使用自带的或者命令行,感觉还可以,挺方便的。
- GitLens:可以直接看到每行代码是什么时候谁写的(
公开处刑)
markdown
- Markdown All in One
- Markdown Preview Enhanced:预览结果
go
现在我的 golang 基本都是在 VSCode 上开发了。
- Go:需要下载一堆插件准备好 VPN 吧
在 linux 中交叉编译 wins
env GOOS=windows GOARCH=amd64 go get -u -v github.com/xxx/xxx
在 wins 中交叉编译 linux
CGO_ENABLED=0 // 坏境变量中配置
GOOS=linux
GOARCH=amd64
工程单独配置 settings.json
{
"go.goroot": "c:\\Go",
"go.gopath": "d:\\GitLib\\Golang;${workspaceRoot}", // 多个路径
"go.toolsEnvVars": {
"GO111MODULE": "on", // 设置 go 环境变量
"CGO_ENABLED": 0,
"GOOS": "linux",
"GOARCH": "amd64",
},
"files.exclude": {
"**/vendor/**": true
},
"files.watcherExclude": {
"**/vendor/**": true
}
}
C
还没有真的在这上编译 C 代码,不过之前都是使用 Sublime,这个应该更好用的吧。
- C/C++
java
这部分也没有怎么使用过,网上有详细的说明文档,只要打开一个 java 文件,会自动建议你安装一些插件。
- Language support for Java ™ for Visual Studio Code
- Java Extension Pack
- Java Dependency Viewer
- Debugger for Java
- Java Test Runner
- Maven for Java
新建一个工程:Ctrl+P
打开控制台,输入 >Crea
,选择 Java: Create Java Project
。
我尝试的时候例子怎么都无法运行,报「找不到或无法加载主类 App」,后来发现是因为路径问题,这个文件我在虚拟机共享文件夹下建的,路径包括
\\192.168.xx.xx\...
。
添加 lib:.classpath
中添加 <classpathentry kind="lib" path="xxx/xxx.jar"/>
只能一个包一个包添加
设置不同的版本:插件目前只能在 JDK11 以上才能运行,那么老的工程怎么使用呢?
在 settings.json
中设置如下:
"java.home": "/xxx/jdk-14",
"java.configuration.runtimes": [
{
"default": true,
"name": "JavaSE-1.7",
"path": "/xxx/jdk1.7.0_80/",
},
{
"name": "JavaSE-1.8",
"path": "/xxx/jdk1.8.0/",
},
],
工程中指定某个版本 JDK,在 .classpath
中添加 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.x"/>
在使用 1.7 时发现一个报错:
Access restriction: The type 'HttpHandler' is not API (restriction on required library '/opt/local/java/jdk1.7.0_80/jre/lib/rt.jar')
解决方法修改 .classpath
如下:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<accessrules>
<accessrule kind="accessible" pattern="com/sun/net/**"/>
</accessrules>
</classpathentry>
添加 Junit Lib:.classpath
中添加 <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
Junit 运行版本报错的话,需要修改
runtimes
配置default
版本与当前一致即可
maven
使用 maven 新建一个工程:
- 先设置工具路径
"maven.executable.path": "C:\\xxx\\mvn"
- 打开控制台输入
>maven
,选择 Generate from Maven Archetype,选择 maven-archetype-quickstart,根据提示输入就行,其中命名规范网上整理的很多1。
maven 添加自己的 package 方法:
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
</plugin>
...
</plugins>
</pluginManagement>
<!-- plugins section : plugins that are invoked when building the project -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>app</id> <!-- 多个包 ID -->
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>target/${project.artifactId}-app-${project.version}.jar</outputFile> <!-- 打包自定义名字 -->
<minimizeJar>true</minimizeJar> <!-- 只打使用的类,未测试过,在 1.7 上没法使用 -->
<filters>
<filter>
<artifact>${project.groupId}:${project.artifactId}</artifact> <!-- 过滤自己的代码 -->
<includes>
<include>com/wishlily/helloworld/App.**</include>
</includes>
</filter>
<filter>
<artifact>io.*:*</artifact> <!-- 打包其他库的选择器 -->
<excludes>
<exclude>**</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>helloworld.App</mainClass> <!-- jar main 入口 -->
</transformer>
</transformers>
</configuration>
</execution>
<!-- 还可以添加另一个 jar 包,同上 -->
</executions>
</plugin>
</plugins>
</build>
开始怎么在
pluginManagement
中添加maven-shade-plugin
插件都不能起作用2。
maven 添加本地库3:
<dependency>
<groupId>anything</groupId>
<artifactId>anything</artifactId>
<version>anything</version>
<scope>system</scope> <!-- 参数为 test 则表示只有测试代码使用 -->
<systemPath>${basedir}/lib/xxx.jar</systemPath> <!-- jar 所在路径,basedir 即 pom.xml 所在目录 -->
</dependency>
gradle
build.gradle
自定义 url
repositories {
maven { url "${nexusUrl}/repository/xxx/" }
}
nexusUrl
可以在 gradle.properties
中定义4:
nexusUrl=http://localhost:8081/nexus
nexusUsername=admin
nexusPassword=admin123
Access restriction: The type 'XXX' is not API (/xxx/jre/lib/xx.jar)
如果报类似以上错误,可以在 build.gradle
添加以下插件:
import org.gradle.plugins.ide.eclipse.model.AccessRule
apply plugin: 'eclipse'
eclipse {
classpath {
file {
whenMerged {
def jre = entries.find { it.path.contains 'org.eclipse.jdt.launching.JRE_CONTAINER' }
jre.accessRules.add(new AccessRule('0', 'ssl/**')) // 忽略部分路径
jre.accessRules.add(new AccessRule('0', 'com/sun/net/ssl/internal/ssl/**'))
}
}
}
}