迁移工具(预览版) - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

迁移工具(预览版)

Amazon SDK for Java 提供了一种迁移工具,可帮助自动将 Java 1.x 代码迁移到 2.x。SDK该工具使用 OpenRewrite(一种开源源代码重构工具)来执行迁移。

您现在可以将该工具用作预览版。该工具支持除了 Amazons3Client 之外的所有SDK服务客户端,以及高级客户端,APIs例如TransferManager和 D。ynamoDBMapper该工具还有一些限制,将在本主题的末尾列出。

使用迁移工具

迁移 Maven 项目

按照以下说明使用 Maven 插件SDK工具迁移基于 Java 1.x 的 M OpenRewriteav en 项目。

  1. 导航到你的 Maven 项目的根目录

    打开终端(命令行)窗口,然后导航到基于 Maven 的应用程序的根目录。

  2. 运行插件的rewrite-maven-plugin命令

    您可以从两种模式(Maven 目标)中进行选择:dryRun和。run

    dryRun 模式

    在该dryRun模式下,插件会在控制台输出中生成差异日志,并在target/rewrite文件夹rewrite.patch中生成名为的补丁文件。此模式允许您预览将要进行的更改,因为不会对源代码文件进行任何更改。

    以下示例说明如何在dryRun模式下调用插件。

    mvn org.openrewrite.maven:rewrite-maven-plugin:dryRun \ -Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:<sdkversion>*-PREVIEW \ -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

    *替换 <sdkversion> 有 2.x SDK 版本。访问 Maven Central 查看最新版本。

    dryRun模式下的控制台输出应类似于以下输出。

    [WARNING] These recipes would make changes to project/src/test/resources/maven/before/pom.xml: [WARNING] software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2 [WARNING] software.amazon.awssdk.v2migration.UpgradeSdkDependencies [WARNING] org.openrewrite.java.dependencies.AddDependency: {groupId=software.amazon.awssdk, artifactId=apache-client, version=2.27.0, onlyIfUsing=com.amazonaws.ClientConfiguration} [WARNING] org.openrewrite.java.dependencies.AddDependency: {groupId=software.amazon.awssdk, artifactId=netty-nio-client, version=2.27.0, onlyIfUsing=com.amazonaws.ClientConfiguration} [WARNING] org.openrewrite.java.dependencies.ChangeDependency: {oldGroupId=com.amazonaws, oldArtifactId=aws-java-sdk-bom, newGroupId=software.amazon.awssdk, newArtifactId=bom, newVersion=2.27.0} [WARNING] org.openrewrite.java.dependencies.ChangeDependency: {oldGroupId=com.amazonaws, oldArtifactId=aws-java-sdk-s3, newGroupId=software.amazon.awssdk, newArtifactId=s3, newVersion=2.27.0} [WARNING] org.openrewrite.java.dependencies.ChangeDependency: {oldGroupId=com.amazonaws, oldArtifactId=aws-java-sdk-sqs, newGroupId=software.amazon.awssdk, newArtifactId=sqs, newVersion=2.27.0} [WARNING] These recipes would make changes to project/src/test/resources/maven/before/src/main/java/foo/bar/Application.java: [WARNING] software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2 [WARNING] software.amazon.awssdk.v2migration.S3GetObjectConstructorToFluent [WARNING] software.amazon.awssdk.v2migration.ConstructorToFluent [WARNING] software.amazon.awssdk.v2migration.S3StreamingResponseToV2 [WARNING] software.amazon.awssdk.v2migration.ChangeSdkType [WARNING] software.amazon.awssdk.v2migration.ChangeSdkCoreTypes [WARNING] software.amazon.awssdk.v2migration.ChangeExceptionTypes [WARNING] org.openrewrite.java.ChangeType: {oldFullyQualifiedTypeName=com.amazonaws.AmazonClientException, newFullyQualifiedTypeName=software.amazon.awssdk.core.exception.SdkException} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getRequestId(), newMethodName=requestId} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getErrorCode(), newMethodName=awsErrorDetails().errorCode} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getServiceName(), newMethodName=awsErrorDetails().serviceName} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getErrorMessage(), newMethodName=awsErrorDetails().errorMessage} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getRawResponse(), newMethodName=awsErrorDetails().rawResponse().asByteArray} [WARNING] org.openrewrite.java.ChangeMethodName: {methodPattern=com.amazonaws.AmazonServiceException getRawResponseContent(), newMethodName=awsErrorDetails().rawResponse().asUtf8String} [WARNING] org.openrewrite.java.ChangeType: {oldFullyQualifiedTypeName=com.amazonaws.AmazonServiceException, newFullyQualifiedTypeName=software.amazon.awssdk.awscore.exception.AwsServiceException} [WARNING] software.amazon.awssdk.v2migration.NewClassToBuilderPattern [WARNING] software.amazon.awssdk.v2migration.NewClassToBuilder [WARNING] software.amazon.awssdk.v2migration.V1SetterToV2 [WARNING] software.amazon.awssdk.v2migration.V1GetterToV2 ... [WARNING] software.amazon.awssdk.v2migration.V1BuilderVariationsToV2Builder [WARNING] software.amazon.awssdk.v2migration.NewClassToBuilderPattern [WARNING] software.amazon.awssdk.v2migration.NewClassToBuilder [WARNING] software.amazon.awssdk.v2migration.V1SetterToV2 [WARNING] software.amazon.awssdk.v2migration.HttpSettingsToHttpClient [WARNING] software.amazon.awssdk.v2migration.WrapSdkClientBuilderRegionStr [WARNING] Patch file available: [WARNING] project/src/test/resources/maven/before/target/rewrite/rewrite.patch [WARNING] Estimate time saved: 20m [WARNING] Run 'mvn rewrite:run' to apply the recipes.

    run 模式

    当你在run模式下运行插件时,它会修改磁盘上的源代码以应用更改。在运行命令之前,请确保您有源代码的备份。

    以下示例说明如何在run模式下调用插件。

    mvn org.openrewrite.maven:rewrite-maven-plugin:run \ -Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:<sdkversion>*-PREVIEW \ -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

    *替换 <sdkversion> 有 2.x SDK 版本。访问 Maven Central 查看最新版本。

    运行命令后,编译应用程序并运行测试以验证更改。

迁移 Gradle 项目

按照以下说明使用 Gradle 插件工具迁移基SDK于 Java 1.x 成OpenRewrite 绩的项目。

  1. 导航到你的 Maven 项目的根目录

    打开终端(命令行)窗口,然后导航到基于 Gradle 的应用程序的根目录。

  2. 创建 Gradle 初始化脚本

    在目录中创建包含以下内容的init.gradle文件。

    initscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:latest.release") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("software.amazon.awssdk:v2-migration:latest.release") } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } }
  3. 运行rewrite命令

    与 Maven 插件一样,你可以在dryRunrun模式下运行 Gradle 插件。

    dryRun 模式

    以下示例说明如何在dryRun模式下调用插件。

    gradle rewriteDryRun --init-script init.gradle \ -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

    run 模式

    以下示例说明如何在run模式下调用插件。

    gradle rewriteRun --init-script init.gradle \ -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

目前的局限性

当前的预览版并不支持中的所有功能SDK。不久将添加对其他功能的 Support 支持。

该工具目前不支持以下功能。以下列表中的链接可将您带到迁移信息,以帮助您手动迁移代码。