本教學課程說明如何準備本機電腦以進行 Java 開發工作,包括開發在Google Cloud上執行的 Java 應用程式。請按照下列步驟安裝 Java 和相關工具。
目標
- 安裝 JDK (Java Development Kit)。
- 安裝自動化建置工具。
- 安裝 gcloud CLI。
- (選用) 安裝 IDE 或編輯器。
- (選用) 安裝 IDE Google Cloud 外掛程式。
- 安裝 Java 適用的 Cloud 用戶端程式庫。
- 設定驗證方法。
安裝 JDK (Java 開發套件)
您可以選擇任何 Java 發行版本,但請務必設定下列環境變數:
- JAVA_HOME:指向 JDK 安裝的基礎位置。
- PATH:包含
$JAVA_HOME/bin。
建議使用 Eclipse Temurin,這是搭配Google Cloud使用的 OpenJDK (Java 開發套件) 發行版本。Temurin 採用開放原始碼授權,並通過 Java SE TCK 認證,且經過測試,可確保生產品質的效能和安全性。
(建議) 安裝 Temurin
Temurin 的安裝方式會因作業系統而異。
- 二進位檔可供下載。
- 如果是 Docker 容器,請使用官方「eclipse-temurin」映像檔。
如果您使用 Compute Engine 開機映像檔,可以使用下列安裝指令碼。
CentOS/RHEL/Rocky
- 判斷 CentOS/RHEL/Rocky Linux 的主要版本:
eval "$(grep VERSION_ID /etc/os-release)" eval "$(grep ^ID= /etc/os-release)" OLD_IFS=$IFS IFS='.' read -ra split_version <<< "$VERSION_ID" IFS=$OLD_IFS MAJOR_VERSION=$split_version
- 建立 Adoptium 原始碼存放區檔案 `/etc/yum.repos.d/adoptium.repo`:
sudo tee /etc/yum.repos.d/adoptium.repo << EOM [Adoptium] name=Adoptium baseurl=https://packages.adoptium.net/artifactory/rpm/$ID/$MAJOR_VERSION/\$basearch enabled=1 gpgcheck=1 gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public EOM
- 更新套件清單:
sudo yum update -y
- 安裝 Temurin:
sudo yum install -y temurin-17-jdk
- 驗證安裝狀態:
java -version
Debian/Ubuntu
- 安裝公開存放區 GPG 金鑰。如果您使用 Ubuntu 16.4,請先透過
gpg --dearmor傳遞金鑰,再儲存至檔案。(例如:sudo wget ... | gpg --dearmor | sudo tee ...)sudo mkdir -p /etc/apt/keyrings sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/apt/keyrings/adoptium.asc
- 判斷 Linux 發行版名稱,並建立原始碼清單檔案
/etc/apt/sources.list.d/adoptium.list:eval "$(grep VERSION_CODENAME /etc/os-release)" sudo tee /etc/apt/sources.list.d/adoptium.list << EOM deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $VERSION_CODENAME main EOM
- 更新套件清單:
sudo apt update -y
- 安裝 Temurin:
sudo apt install -y temurin-17-jdk
- 驗證安裝狀態:
java -version
SLES
- 判斷 SLES 的主要版本:
eval "$(grep VERSION_ID /etc/os-release)" OLD_IFS=$IFS IFS='.' read -ra split_version <<< "$VERSION_ID" IFS=$OLD_IFS MAJOR_VERSION=$split_version
- 安裝公開存放區 GPG 金鑰:
sudo mkdir -p /etc/zypp/keyrings sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/zypp/keyrings/adoptium.asc sudo rpm --import /etc/zypp/keyrings/adoptium.asc
- 判斷 SLES 版本,並註冊 Adoptium 存放區:
sudo zypper ar -f "https://packages.adoptium.net/artifactory/rpm/sles/$MAJOR_VERSION/$(uname -m)" adoptium
- 更新套件清單:
sudo zypper update -y
- 安裝 Temurin:
sudo zypper install -y temurin-17-jdk
- 驗證安裝狀態:
java -version
Windows
如要安裝 Temurin,請以管理員身分執行 PowerShell 3.0 以上版本,並輸入下列指令。
- 下載 Temurin。以下操作說明中的
Invoke-WebRequest指令需要 PowerShell 3.0 以上版本。$JdkVersion = 17 $JdkUrl = "https://api.adoptium.net/v3/binary/latest/$JdkVersion/ga/windows/x64/jdk/hotspot/normal/eclipse?project=jdk" $JdkExtractionPath = "C:\temurin-$JdkVersion-jdk" $JdkDownload = "$JdkExtractionPath.zip" [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12' Invoke-WebRequest -Uri $JdkUrl -OutFile $JdkDownload Expand-Archive $JdkDownload -DestinationPath $JdkExtractionPath -Force
- 設定
JAVA_HOME和Path變數:pushd $JdkExtractionPath $JdkPath = (Get-ChildItem).FullName popd [System.Environment]::SetEnvironmentVariable('JAVA_HOME', $JdkPath, 'Machine') - (Optional) Refresh your session's `$env:Path` value. Otherwise, start a
new session:
$MachinePath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') $UserPath = [System.Environment]::GetEnvironmentVariable('Path', 'User') $env:Path = "$MachinePath;$UserPath" - 驗證安裝狀態:
java -version
安裝自動化建置工具
Apache Maven、Gradle 和 SBT 等套件管理選項可協助您在不同平台上,快速且一致地建構 Java 應用程式依附元件。
安裝 gcloud CLI
gcloud CLI 是一組適用於 Google Cloud的工具,包括 gcloud 和 bq。您可以從指令列執行這些指令,以存取 Compute Engine、Cloud Storage、BigQuery 和其他產品與服務。您可以利用這些工具進行互動操作,也可以使用自動化指令碼執行這些工具。
(選用) 安裝 IDE 或編輯器
用來開發 Java 應用程式的熱門編輯器包括但不限於下列項目 (排序不分先後):
- Visual Studio Code
- JetBrains 的 IntelliJ IDEA 和/或 Webstorm
- Eclipse Foundation 的 Eclipse
- GitHub 的 Atom
這些編輯器 (有些需安裝外掛程式) 提供各種功能,範圍從語法反白顯示、智慧感知、程式碼自動完成到全面整合的偵錯功能。
(選用) 安裝 IDE 外掛程式
如要存取編輯器中的實用功能,請查看下列外掛程式:
安裝 Java 適用的 Cloud 用戶端程式庫
您可以使用 Java 適用的 Cloud 用戶端程式庫整合多項服務,例如 Datastore 和 Cloud Storage。 Google Cloud您也可以為 BigQuery 等個別的 API 安裝套件,如同下列範例所示。
If you are using Maven, add
the following to your pom.xml file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code or IntelliJ, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
設定驗證方法
如要執行用戶端程式庫,您必須先設定驗證。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱「進行驗證以使用用戶端程式庫」一文。
使用用戶端程式庫
設定用戶端程式庫的端點
如果您使用的 API 支援區域端點,請使用端點設定要將要求傳送至哪個伺服器。舉例來說,您可以使用 Google.Cloud.Dataproc.V1 API 設定用戶端端點。如要進一步瞭解 Dataproc 的區域性端點,請參閱這篇文章。請務必在下列範例中,將 MY-PROJECT 替換為專案名稱,並將 us-central1 替換為適合您設定的區域:
ClusterControllerSettings settings =
ClusterControllerSettings.newBuilder()
.setEndpoint("us-central1-dataproc.googleapis.com:443")
.build();
try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settings)) {
String projectId = "MY-PROJECT";
String region = "us-central1";
Cluster cluster = Cluster.newBuilder().build();
}後續步驟
(選用) 使用 App Engine 適用的 Maven 或 Gradle 外掛程式
如果您是在 App Engine 標準或彈性環境中進行開發作業,則可以透過 Apache Maven 和 Gradle 建置工具均適用的外掛程式,使用其提供的便利功能來開發、測試及部署應用程式。
App Engine 標準環境
使用 App Engine 標準環境適用的 Maven App Engine 外掛程式或 Gradle 外掛程式。
App Engine 彈性環境
使用 App Engine 彈性環境適用的 Maven App Engine 外掛程式或 Gradle 外掛程式。