1 vs code
也是剛剛接觸 vs code,但是一見鐘情。
最近在試著使用 nim language,推薦用 vs code,就試用了一下,然后一發不可收拾。于是有了一個大膽的想法,干脆全部轉到 vs code 上來吧,現在的情況是什么呢?前段用 webstorm/sublime,java 用 idea,python 用 pycharm, ruby 用 rubymine, c# 用 vs express,多麻煩啊。
vs code 其實就是的編輯器,是個馬甲,但是各種語言都可以實現對應的插件,包裝成一個 ide,這很好,很先進!最最最關鍵的是,開源,免費!
1.1 什么是 ide
集成開發環境(ide,integrated development environment )是用于提供程序開發環境的應用程序,一般包括代碼編輯器、編譯器、調試器和圖形用戶界面等工具。集成了代碼編寫功能、分析功能、編譯功能、調試功能等一體化的開發軟件服務套。
如微軟的visual studio系列,作為 c++/c# ide;
java 的 ide 如 eclipse 和 intellij idea。
1.2 vs code 是個編輯器
vs code 并不是一個 ide,它是個編輯器,是個有理想的編輯器,可以通過相應語言的插件,將其包裝成一個 ide。
vi 也是一個編輯器,很多程序員就是使用 vi 來做開發,構建用獨立的工具,比如 make,ant,maven,gradle 等等。ctags 用來對源代碼中的符號建索引。。。。
那么對于程序員來說,怎樣的編輯器來算是好用的呢?
- 打開文件,方便快捷,語法高亮,美觀!
- 編輯:增刪改查,豐富快捷
- 符號:符號定義查詢、跳轉,符號引用….
- 依賴管理:自動導入依賴包
- 分析:類結構,繼承關系…….
- 自動提示 …..
- 其他高級特性。。。
上述特性里面,有些是 vs code 可以做的,有些是必須由插件來完成的。比如符號和依賴管理等跟語言特征相關的,那就必須由相應語言的插件來完成,你不能在使用 vs code 時,因為代碼無法跳轉到definition,就罵 vs code 不智能。
2 java
盡管 idea 體驗也很不錯,但有時還是感覺太臃腫了,不夠流暢。
當然,必須承認 vs code 肯定無法匹敵 idea 所提供的完整特性,對于初學者來說,idea/eclipse 絕對是必經之路。然而,作為程序員,我們也必須清楚,設計是一種取舍,idea 提供的無微不至的保姆一般的圖形界面,終將會顯得友好但啰嗦,會有那么一天,你成熟了,長大了,就嫌她啰里啰嗦了。
2.1 java support extensions
https://code.visualstudio.com/docs/languages/java
按照官方文檔,老老實實的安裝好 java 相關的 extensions。
簡單來說:
vs code java ide =
1
2
3
|
編輯器:vs code 構建工具: maven/gradle 語言支持:eclipse ™ jdt language server |
2.2 language support for java(tm) by red hat
有些功能如:
- 代碼補全: code completion
- 自動導入: organize imports
- 代碼跳轉: code navigation
等等!很顯然,vs code 不會提供這些語言級別的特性,這也是為什么 jetbrains 有那么多產品的原因:
intellij idea - 一套智慧型的java整合開發工具,特別專注與強調程序師的開發撰寫效率提升
- phpstorm 7.0 發布,php 集成開發工具
- pycharm 3發布,智能python集成開發工具
- rubymine -rubymine 是一個為ruby 和rails開發者準備的ide,其帶有所有開發者必須的功能,并將之緊密集成于便捷的開發環境中。
- webstorm8.0 發布,智能html/css/js開發工具
vs code 通過 extension 來提供相應的 ide 特性,對于 java 來說,language support for java(tm) by red hat 這個 extension 就是干這個事情的。
provides java ™ language support via eclipse ™ jdt language server, which utilizes eclipse ™ jdt, m2eclipse and buildship.
2.3 什么是 jdt
jdt 叫做 eclipse java development tools
the jdt project provides the tool plug-ins that implement a java ide supporting the development of any java application,
再看看 jdt core 都提供了哪些 vs code 需要擴展的功能:
- a java model that provides api for navigating the java element tree. the java element tree defines a java centric view of a project. it surfaces elements like package fragments, compilation units, binary classes, types, methods, fields.
- a java document model providing api for manipulating a structured java source document.
- code assist and code select support.
- an indexed based search infrastructure that is used for searching, code assist, type hierarchy computation, and refactoring. the java search engine can accurately find precise matches either in sources or binaries.
- evaluation support either in a scrapbook page or a debugger context.
- source code formatter
需要注意的是,該 extension 使用了 eclipse ide 相關的實現。當生成一個新的 java 項目時,比如通過 mvn 來 generate 一個helloworld 項目:
mvn archetype:generate -darchetypegroupid=org.apache.maven.archetypes -darchetypeartifactid=maven-archetype-quickstart -darchetypeversion=1.3
然后用 vs code 打開項目目錄,會看到項目目錄中會隨之生成幾個文件和目錄:
1 .settings
1.1 org.eclipse.jdt.core.prefs
1
2
3
4
5
|
eclipse.preferences.version= 1 org.eclipse.jdt.core.compiler.codegen.targetplatform= 1.6 org.eclipse.jdt.core.compiler.compliance= 1.6 org.eclipse.jdt.core.compiler.problem.forbiddenreference=warning org.eclipse.jdt.core.compiler.source= 1.6 |
1.2 org.eclipse.m2e.core.prefs
1
2
3
4
|
activeprofiles= eclipse.preferences.version= 1 resolveworkspaceprojects= true version= 1 |
2 .project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version= "1.0" encoding= "utf-8" ?> <projectdescription> <name>spring-ldap-user-admin-sample</name> <comment></comment> <projects> </projects> <buildspec> <buildcommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildcommand> <buildcommand> <name>org.eclipse.m2e.core.maven2builder</name> <arguments> </arguments> </buildcommand> </buildspec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.m2e.core.maven2nature</nature> </natures> </projectdescription> |
3 .classpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?xml version= "1.0" encoding= "utf-8" ?> <classpath> <classpathentry kind= "src" output= "target/classes" path= "src/main/java" > <attributes> <attribute name= "optional" value= "true" /> <attribute name= "maven.pomderived" value= "true" /> </attributes> </classpathentry> <classpathentry excluding= "**" kind= "src" output= "target/classes" path= "src/main/resources" > <attributes> <attribute name= "maven.pomderived" value= "true" /> </attributes> </classpathentry> <classpathentry kind= "src" output= "target/test-classes" path= "src/test/java" > <attributes> <attribute name= "optional" value= "true" /> <attribute name= "maven.pomderived" value= "true" /> <attribute name= "test" value= "true" /> </attributes> </classpathentry> <classpathentry kind= "con" path= "org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/javase-1.6" > <attributes> <attribute name= "maven.pomderived" value= "true" /> </attributes> </classpathentry> <classpathentry kind= "con" path= "org.eclipse.m2e.maven2_classpath_container" > <attributes> <attribute name= "maven.pomderived" value= "true" /> </attributes> </classpathentry> <classpathentry kind= "output" path= "target/classes" /> </classpath> |
要注意: 這些文件都是 extension 自動生成的,如果目錄下沒有生成相應的文件,那么就會出現各種問題,jdt 相關的很多功能無法正常使用,比如符號跳轉,自動導入等。
如果用 idea 打開 java 項目,同樣會創建類似的文件,只不過結構和名稱不一樣而已。
2.4 java classpath is incomplete. only syntax errors will be reported
如果碰到該警告信息,說明 java 項目在打開過程中出問題了,缺少 .classpath .project 文件。有可能是以下原因,比如:
- jdt 相關的 extentsions 沒有安裝
- java 環境沒有按官方說明配置
- extension 配置不完整
這種情況下,符號跳轉,自動補全,導入等等功能,肯定無法正常使用。
但是使用 mvn 進行構建是沒有問題的,一定要清楚,mvn 是構建工具,只要源碼完整正確,有 pom.xml 文件,那么 maven 就能正常工作。
另外,發現當項目同時支持 maven 和 gradle 時,vs code 創建項目會失敗,導致 classpath 相關文件無法產生。這個時候將 build.gradle 刪掉,只留下 pom.xml 文件,再次打開項目文件夾,就可以了。
2.5 項目結構
如上圖,正常啟動的java項目,需要包含
- java projects
- maven projects
- java dependencies
其中 java projects 中包含 .classpath, .project, .settings
總結
總之,用 vs code 來作為 java ide 完全沒有問題,使用過程中難免會碰到些問題,多查閱,多思考,應該能解決。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/antony1776/article/details/80298326