如何用 VS Code 打開並啟動tstt這個專案 - 其二
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1264
問題原因
BaseAction.java 與 MessageAction.java 等檔案引用了 Servlet API、JTA、Log4j 以及 Seasar2 等外部庫,但目前專案的 classpath 中沒有這些 jar,VS Code 就無法解析 javax.servlet、javax.transaction 等類別。
直接用 javac 編譯也會出現大量 “package … does not exist” 的錯誤,說明缺少依賴庫,而不是程式碼本身有語法錯誤。
解決步驟(以手動加入 jar 為例)
準備 JDK
確認已安裝 JDK 8(專案 README 也建議使用 JDK 7/8)。
下載所需庫(可以從 Maven Central 下載對應 jar):
-
javax.servlet-api(Servlet 3.0 對應的 jar,可在 Tomcat 的 lib/ 目錄找到)
-
javax.annotation-api
-
javax.transaction-api
-
log4j-1.2.x
-
Seasar2/S2Struts 相關:s2-framework, s2-extension, s2-tiger, s2-struts 等
把 jar 放進專案並加入 classpath
-
在專案根目錄新建 lib 資料夾,把上述 jar 都放進去。
-
在 VS Code「Java Projects」視窗中,展開專案 → Referenced Libraries → Add → 選擇 lib 內的 jar。
重新編譯/重啟 VS Code
加入 jar 之後,VS Code 會重新索引;若還有紅線,嘗試執行一次 Ctrl+Shift+B 或重啟 VS Code。
若你熟悉 Maven/Gradle,也可以建立 pom.xml/build.gradle,把以上依賴寫進去,讓工具自動下載管理。
完成上述設定後,VS Code 中的 4K+ 錯誤應能消失,專案就能正常編譯並部署到 Tomcat。
下面示範如何把目前的專案改成 Maven 專案並用 Tomcat 部署。步驟都以 JDK 8、Maven 3.6+ 為前提。
1. 安裝並確認 Maven
下載並安裝 Maven。
mvn -v
確認系統能執行 Maven。
2. 在專案根目錄建立 pom.xml
專案結構已符合 Maven 標準 (src/main/java
、src/main/webapp
等),只差 pom.xml。
可在專案根目錄新增如下範例檔案(依需求再補充其他 jar):
<!-- pom.xml -->
<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>jp.tokyo.metro</groupId>
<artifactId>tstt</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Servlet / JTA / Annotation -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<!-- Seasar2 / S2Struts -->
<dependency>
<groupId>org.seasar.framework</groupId>
<artifactId>s2-framework</artifactId>
<version>2.4.45</version>
</dependency>
<dependency>
<groupId>org.seasar.framework</groupId>
<artifactId>s2-extension</artifactId>
<version>2.4.45</version>
</dependency>
<dependency>
<groupId>org.seasar.framework</groupId>
<artifactId>s2-tiger</artifactId>
<version>2.4.45</version>
</dependency>
<dependency>
<groupId>org.seasar.framework</groupId>
<artifactId>sa-struts</artifactId>
<version>1.0.4-sp9</version>
</dependency>
<!-- Commons 系列 -->
<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.7.0</version></dependency>
<dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.1</version></dependency>
<dependency><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId><version>1.6</version></dependency>
<dependency><groupId>commons-el</groupId><artifactId>commons-el</artifactId><version>1.0</version></dependency>
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2</version></dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>
<dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency>
<dependency><groupId>commons-validator</groupId><artifactId>commons-validator</artifactId><version>1.2.0</version></dependency>
<!-- 其餘 jar(例如 geronimo-*、jsonic、poi、opencsv 等)照上述格式再加入即可 -->
</dependencies>
<build>
<plugins>
<!-- 建 war 檔 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- 直接用 Maven 啟動 Tomcat(可選) -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/tstt</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
提示
WEB-INF/lib
中有些 jar(如 ojdbc6.jar
, iTextAsian.jar
)不在 Maven Central,需要手動安裝到本機倉庫,例如:
mvn install:install-file -Dfile=src/main/webapp/WEB-INF/lib/ojdbc6.jar \
-DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar
之後再以 <dependency>
方式引用即可。
其他 jar 可以在 search.maven.org 找到 groupId / artifactId / version。
3. 在 VS Code 匯入 Maven 專案
-
安裝擴充套件 Maven for Java 或 Extension Pack for Java
-
開啟專案根目錄;VS Code 會識別 pom.xml 並建立 Maven 專案
-
左側 Maven 視窗可看到專案與各命令
4. 編譯與建置
mvn clean package
完成後會產生 target/tstt-1.0-SNAPSHOT.war
。
5. 部署到 Tomcat
方法一:複製 WAR 檔
-
將上一步的
target/*.war
複製到apache-tomcat-7.0.99/webapps/
-
啟動
apache-tomcat-7.0.99/bin/startup.sh
(Windows 用startup.bat
)
方法二:使用 Maven 外掛
mvn tomcat7:run
Maven 會啟動內嵌 Tomcat,網址同樣是 http://localhost:8080/tstt
6. 常見問題
-
無法解析某個類:缺少對應
<dependency>
,檢查 jar 名稱再加入 -
Jar 不在 Maven Central:使用
mvn install:install-file
手動放入本機倉庫 -
VS Code 仍報錯:嘗試重新整理 Maven 專案(側邊欄 Maven 視窗點「Refresh」),或關閉重開 VS Code
依以上步驟,把所有需要的 jar 轉為 Maven 依賴後,就能方便地使用 Maven 建置並部署到 Tomcat 了。祝順利完成!
This article was last edited at