網頁

2020/9/12

MacOS Eclipse 匯入Tomcat原始碼為專案 import Tomcat source code as Eclipse project

在macOS的Eclipse匯入Tomcat原始碼為專案的步驟如下。

安裝Apache Ant


建置Tomcat原始碼需要Apache Ant,所以macOS要安裝Ant。

在終端機輸入ant -version確認系統是否已經有Ant。

$ ant -version

如果沒有安裝Ant則這邊使用Homebrew安裝,輸入brew install ant開始安裝。

$ brew install ant

安裝完輸入brew info ant查看Ant的安裝位置為/usr/local/Cellar/ant/1.10.8_2,此路徑待會設定Eclipse的[Classpath Variables]的ANT_HOME變數時會用到。

$ brew info ant
ant: stable 1.10.8, HEAD
Java build tool
https://ant.apache.org/
/usr/local/Cellar/ant/1.10.8_2 (1,667 files, 41.8MB) *
  Built from source on 2020-09-13 at 10:40:08
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/ant.rb
License: Apache-2.0
==> Dependencies
Required: openjdk ✔
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 10,903 (30 days), 27,996 (90 days), 136,973 (365 days)
install-on-request: 8,621 (30 days), 22,010 (90 days), 101,313 (365 days)
build-error: 0 (30 days)

範例安裝的Ant版本為1.10.8_2


再次輸入ant -version確認Ant已安裝。

$ ant -version
Apache Ant(TM) version 1.10.8 compiled on May 10 2020

下載Tomcat原始碼


到Tomcat的github https://github.com/apache/tomcat/ 把Tomcat原始碼clone下來到Eclipse的workspace目錄。

/.../workspace/$ git clone https://github.com/apache/tomcat/

原始碼抓下來後用git branch切換Tomcat版本,例如8.5.x

$ git branch 8.5.x

Eclipse設定


在Tomcat目錄下執行ant ide-eclispe下載Tomcat專案依賴的函式庫,預設下載到/Users/user/tomcat-build-libs/並建立Eclipse的專案設定檔。

/../workspace/tomcat$ ant ide-eclipse
Buildfile: /Users/user/Documents/workspace/tomcat/build.xml
...
...
ide-eclipse:
     [copy] Copying 1 file to /Users/user/Documents/workspace/tomcat
     [copy] Copying 1 file to /Users/user/Documents/workspace/tomcat
    [mkdir] Created dir: /Users/user/Documents/workspace/tomcat/.settings
     [copy] Copying 1 file to /Users/user/Documents/workspace/tomcat/.settings
     [echo] Eclipse project files created.
     [echo] Read the Building page on the Apache Tomcat documentation site for details on how to configure your Eclipse workspace.

BUILD SUCCESSFUL
Total time: 10 seconds

開啟Eclipse,在Preferences -> Java -> Build Path -> Classpath Variables新增兩個變數TOMCAT_LIBS_BASEANT_HOME

TOMCAT_LIBS_BASE為Tomcat專案依賴的外部函示庫,預設為/Users/user/tomcat-build-libs
ANT_HOME路徑為Homebrew安裝Ant的目錄,預設在/usr/local/Cellar/ant/1.10.8_2/libexec

設定好後在Eclipse功能選單點選File -> Import... -> General -> Existing Projects into Workspace



在[Select root directory]選擇Tomcat的目錄位置即可匯入為專案。

匯入後Eclipse [Markers]頁籤會出現Java Problems的錯誤,這不影響執行可以忽略。



在Eclipse的tomcat專案上按滑鼠右鍵 -> Run As -> Java Applications會出現很多有main方法的程式,選擇Bootstrap執行即可啟動專案的Tomcat。




沒有留言:

張貼留言