只有在您想要建置 Connector/J 的自訂版本,或您有興趣協助我們測試新程式碼時,才需要從原始碼安裝 Connector/J。若只是想在您的系統上啟動並執行 MySQL Connector/J,請使用標準二進位發行版本安裝 Connector/J;如需指示,請參閱第 4.1 節「從二進位發行版本安裝 Connector/J」。
若要從原始碼安裝 MySQL Connector/J,請確保您的系統上具有下列軟體
建議使用下列軟體的最新版本來編譯 Connector/J;否則,某些功能可能無法使用。
Git 用戶端,如果您想要從我們的 GitHub 儲存庫檢查來源 (可從 https://git.dev.org.tw/downloads 取得)。
Apache Ant 1.10.6 或更新版本 (可從 https://ant.dev.org.tw/ 取得)。
JDK 1.8.x (可從 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 取得)。
-
下列協力廠商程式庫
-
JUnit 5.10.2 (請參閱JUnit 5 使用者指南中的安裝和下載資訊)。需要下列 JAR 檔案
junit-jupiter-api-5.10.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.junit.jupiter/junit-jupiter-api/5.10.2/jar 取得)。junit-jupiter-engine-5.10.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.junit.jupiter/junit-jupiter-engine/5.10.2/jar 取得)。junit-platform-commons-1.10.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.junit.platform/junit-platform-commons/1.10.2/jar 取得)。junit-platform-engine-1.10.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.junit.platform/junit-platform-engine/1.10.2/jar 取得)。junit-platform-launcher-1.10.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.junit.platform/junit-platform-launcher/1.10.2/jar 取得)。-
JUnit 5 依賴的這些額外 JAR 檔案
apiguardian-api-1.1.2.jar
(例如,可從 https://central.sonatype.com/artifact/org.apiguardian/apiguardian-api/1.1.2/jar 取得)。opentest4j-1.3.0.jar
(例如,可從 https://central.sonatype.com/artifact/org.opentest4j/opentest4j/1.3.0/jar 取得)。
Javassist 3.30.2 (
javassist-3.30.2-GA.jar
,例如,可從 https://central.sonatype.com/artifact/org.javassist/javassist/3.30.2-GA/bundle 取得)。Protocol Buffers Java API 4.26.1 (
protobuf-java-4.26.1.jar
,例如,可從 https://central.sonatype.com/artifact/com.google.protobuf/protobuf-java/4.26.1/bundle 取得)。C3P0 0.10.1 或更新版本 (
c3p0-0.10.1.jar
,例如,可從 https://central.sonatype.com/artifact/com.mchange/c3p0/0.10.1/jar 取得)。Simple Logging Facade API 2.0.13 或更新版本 (
slf4j-api-2.0.13.jar
,例如,可從 https://central.sonatype.com/artifact/org.slf4j/slf4j-api/2.0.13/jar 取得)。Java Hamcrest 2.2 或更新版本 (
hamcrest-2.2.jar
,例如,可從 https://central.sonatype.com/artifact/org.hamcrest/hamcrest/2.2/jar 取得)。Oracle Cloud Infrastructure SDK for Java (
oci-java-sdk-common-3.41.2.jar
,例如,可從 https://central.sonatype.com/artifact/com.oracle.oci.sdk/oci-java-sdk-common/3.41.2/jar 取得)。OpenTelemetry API (
opentelemetry-api-1.38.0.jar
,例如,可從 https://central.sonatype.com/artifact/io.opentelemetry/opentelemetry-api/1.38.0 取得)。OpenTelemetry Context (
opentelemetry-context-1.38.0.jar
,例如,可從 https://central.sonatype.com/artifact/io.opentelemetry/opentelemetry-context/1.38.0 取得)。Open Test Alliance for the JVM (
opentest4j-1.3.0.jar
,例如,可從 https://central.sonatype.com/artifact/org.opentest4j/opentest4j/1.3.0 取得)。
-
若要從原始碼建置 MySQL Connector/J,請遵循下列步驟
確保已安裝 JDK 1.8.x。
-
透過下列其中一種方式取得 Connector/J 的來源
從 Connector/J 下載頁面下載 Connector/J 的平台獨立發行版本封存檔 (以
.tar.gz
或.zip
格式)。將封存檔的內容解壓縮到名為 (例如)mysql-connector-j
的資料夾。-
從 Connector/J 下載頁面下載 Connector/J 的原始碼 RPM 套件並安裝。
-
從位於 GitHub 的 MySQL Connector/J 原始碼儲存庫檢查程式碼,網址為 https://github.com/mysql/mysql-connector-j。Connector/J 9.0 系列的最新版本位於
release/9.0
分支;使用下列命令檢查它$> git clone --branch release/9.0 https://github.com/mysql/mysql-connector-j.git
在目前目錄下,此命令會建立
mysql-connector-j
子目錄,其中包含您需要的程式碼。
將所有必要的協力廠商程式庫放在來源樹狀結構根目錄的
lib
資料夾 (也就是,如果您已遵循上述步驟,則放在mysql-connector-j/lib
中),或者將它們放在其他位置,稍後再將位置提供給 Ant (請參閱下方的步驟 5)。將目前的工作目錄變更為上述步驟 2 中建立的
mysql-connector-j
目錄。-
在目錄中,建立一個名為
build.properties
的檔案,以使用屬性com.mysql.cj.build.jdk
向 Ant 指示您的 JDK 1.8.x 安裝根目錄的位置,以及額外函式庫的位置(如果它們不在mysql-connector-j/lib
中),則使用屬性com.mysql.cj.extra.libs
。以下是一個設定這些屬性的範例檔案(將 “path_to_*
” 部分替換為適當的檔案路徑)com.mysql.cj.build.jdk=path_to_jdk_1.8 com.mysql.cj.extra.libs=path_to_folder_for_extra_libraries
或者,您也可以透過 Ant 的
-D
選項設定這些屬性的值。注意從 Connector/J 5.1 升級到 8.0 及更高版本時,許多用於建置 Connector/J 的 Ant 屬性已重新命名或移除;有關詳細資訊,請參閱第 4.4.1.4 節「建置屬性的變更」。
-
執行以下命令以編譯驅動程式,並為 Connector/J 建立
.jar
檔案$> ant build
這會在目前目錄中建立一個
build
目錄,所有建置輸出都會放置於其中。在build
目錄下會建立一個目錄,其名稱包含您正在建置的發行版本號碼。該目錄包含來源程式碼、已編譯的.class
檔案,以及用於部署的.jar
檔案。如需所有建置目標的資訊,包括建立完整封裝散發套件的目標,請執行以下命令
$> ant -projecthelp
依照設定類別路徑或為應用程式伺服器設定 Connector/J中提供的指示,安裝新建立的 JDBC 驅動程式
.jar
檔案,就像安裝從 MySQL 下載的二進位.jar
檔案一樣。