本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
迁移工具(预览版)
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
-
导航到你的 Maven 项目的根目录
打开终端(命令行)窗口,然后导航到基于 Maven 的应用程序的根目录。
-
运行插件的
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 绩
-
导航到你的 Maven 项目的根目录
打开终端(命令行)窗口,然后导航到基于 Gradle 的应用程序的根目录。
-
创建 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() } } } }
-
运行
rewrite
命令与 Maven 插件一样,你可以在
dryRun
或run
模式下运行 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 支持。
该工具目前不支持以下功能。以下列表中的链接可将您带到迁移信息,以帮助您手动迁移代码。
-
S3 客户端(Amazons3Client),该工具目前支持的方法和方法
putObject
getObject
-
S3 传输管理器 (TransferManager)
-
S3 URI 解析 (Amazon URI S3)
-
DynamoDB 对象映射 (D) ynamoDBMapper
-
EC2元数据实用程序 (EC2MetadataUtils)
-
服务员 () AmazonDynamo DBWaiters
-
IAM策略生成器(策略)
-
CloudFront 预签名 (CloudFrontUrlSigner, CloudFrontCookieSigner)
-
S3 事件通知 (S3EventNotification)