

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立專案建置檔案
<a name="setup-create-project-file"></a>

在您設定單一登入存取和開發環境之後，請使用您偏好的建置工具建立 Kotlin 專案。在建置檔案中，指定應用程式需要存取 AWS 服務 之 的相依性。

若要查看所有可能的 Maven 成品名稱清單，請參閱 [API 參考文件](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/index.html)。若要尋找最新版本的 SDK，請檢查 [ GitHub 上的最新版本](https://github.com/awslabs/aws-sdk-kotlin/releases/latest)。

下列範例建置檔案提供使用 7 開始編碼專案的必要元素 AWS 服務。

------
#### [ Gradle ]

 適用於 Kotlin 的 AWS SDK 發佈 [Gradle 版本目錄](https://docs.gradle.org/current/userguide/version_catalogs.html)和物料清單 (BOM)，可協助您探索相依性的名稱，並保持跨多個成品的版本編號同步。

請注意，版本目錄是 Gradle 第 8 版之前的預覽功能。根據您使用的 Gradle 版本，您可能需要透過[特徵預覽 API](https://docs.gradle.org/current/userguide/feature_lifecycle.html#feature_preview) 選擇加入。

**使用 Gradle 版本目錄**

1. 在您的 `settings.gradle.kts`檔案中，在 `versionCatalogs`區塊內新增 `dependencyResolutionManagement`區塊。

   下列範例檔案會設定 適用於 Kotlin 的 AWS SDK 版本目錄。您可以導覽至 {{X.Y.Z }}連結，以查看可用的最新版本。

   ```
   plugins {
       id("org.gradle.toolchains.foojay-resolver-convention") version "[https://plugins.gradle.org/plugin/org.gradle.toolchains.foojay-resolver-convention](https://plugins.gradle.org/plugin/org.gradle.toolchains.foojay-resolver-convention)"
   }
   rootProject.name = "your-project-name"
   
   dependencyResolutionManagement {
       repositories {
           mavenCentral()
       }
   
       versionCatalogs {
           create("awssdk") {
               from("aws.sdk.kotlin:version-catalog:[https://github.com/awslabs/aws-sdk-kotlin/releases/latest](https://github.com/awslabs/aws-sdk-kotlin/releases/latest)")
           }
       }
   }
   ```

1. `build.gradle.kts` 使用版本目錄提供的類型安全識別符，宣告 中的相依性。

   下列範例檔案會宣告七個 的相依性 AWS 服務。

   ```
   plugins {
       kotlin("jvm") version "{{[X.Y.Z](https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)}}"
       application
   }
   
   group = "org.example"
   version = "1.0-SNAPSHOT"
   
   repositories {
       mavenCentral()
   }
   
   dependencies {
       implementation(platform(awssdk.bom))
       implementation(platform("org.apache.logging.log4j:log4j-bom:[https://search.maven.org/#search|gav|1|g:org.apache.logging.log4j%20AND%20a:log4j-bom](https://search.maven.org/#search|gav|1|g:org.apache.logging.log4j%20AND%20a:log4j-bom)"))
   
       implementation(awssdk.services.s3)
       implementation(awssdk.services.dynamodb)
       implementation(awssdk.services.iam)
       implementation(awssdk.services.cloudwatch)
       implementation(awssdk.services.cognitoidentityprovider)
       implementation(awssdk.services.sns)
       implementation(awssdk.services.pinpoint)
       implementation("org.apache.logging.log4j:log4j-slf4j2-impl")
   
       // Test dependency.
       testImplementation(kotlin("test"))
   }
   
   tasks.test {
       useJUnitPlatform()
   }
   
   java {
       toolchain {
           languageVersion = JavaLanguageVersion.of({{X*}})
       }
   }
   
   application {
       mainClass = "org.example.AppKt"
   }
   ```

   \*Java 版本，例如 `17`或 `21`。

------
#### [ Maven ]

下列範例`pom.xml`檔案具有七個 的相依性 AWS 服務。您可以導覽至 {{X.Y.Z }}連結，以查看可用的最新版本。

```
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>setup</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <aws.sdk.kotlin.version>[https://github.com/awslabs/aws-sdk-kotlin/releases/latest](https://github.com/awslabs/aws-sdk-kotlin/releases/latest)</aws.sdk.kotlin.version>
        <kotlin.version>{{[X.Y.Z](https://kotlinlang.org/docs/releases.html#release-details)}}</kotlin.version>
        <log4j.version>[https://search.maven.org/#search|gav|1|g:org.apache.logging.log4j%20AND%20a:log4j-bom](https://search.maven.org/#search|gav|1|g:org.apache.logging.log4j%20AND%20a:log4j-bom)</log4j.version>
        <junit.jupiter.version>[https://search.maven.org/#search|gav|1|g:org.junit.jupiter%20AND%20a:junit-jupiter](https://search.maven.org/#search|gav|1|g:org.junit.jupiter%20AND%20a:junit-jupiter)</junit.jupiter.version>
        <jvm.version>{{X}}*</jvm.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>aws.sdk.kotlin</groupId>
                <artifactId>bom</artifactId>
                <version>${aws.sdk.kotlin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-bom</artifactId>
                <version>${log4j.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>s3-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>dynamodb-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>iam-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>cloudwatch-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>cognitoidentityprovider-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>sns-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>aws.sdk.kotlin</groupId>
            <artifactId>pinpoint-jvm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>${jvm.version}</jvmTarget>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
```

\*Java 版本，例如 `17`或 `21`。

------