Posted in

用 OpenRewrite 消除繁琐_AI阅读总结 — 包阅AI

包阅导读总结

1.

关键词:OpenRewrite、refactoring、development、developer experience、automation

2.

总结:文章介绍了 OpenRewrite 这一用于 Java 代码重构的工具,可快速可靠地进行耗时的代码更改,作者以将应用从 JUnit 4 迁移到 JUnit 5 为例,展示了其使用方法、效果和优势。

3.

主要内容:

– 介绍 OpenRewrite 可消除工作中的繁琐

– 作者曾因繁琐推迟将应用测试从 JUnit 4 升级到 JUnit 5,后用 OpenRewrite 几分钟完成

– 阐述 OpenRewrite 是什么

– 用于 Java 代码重构,运行食谱转换源代码,有丰富的食谱集合

– 说明运行 OpenRewrite 的方式

– 作者倾向使用 Gradle 初始化脚本,无需修改项目

– 给出用于 JUnit 4 到 JUnit 5 迁移的脚本示例

– 展示运行迁移食谱的命令及结果

– 命令包含指定运行任务、食谱和调整内存等

– 食谱对代码做了多项更改,估计为作者节省 55 分钟工作时间

– 提供将项目从 JUnit 4 迁移到 JUnit 5 的便捷脚本

思维导图:

文章地址:http://microservices.io//post/architecture/2024/08/06/eliminating-tedium-with-openrewrite.html

文章来源:microservices.io

作者:Chris Richardson

发布时间:2024/8/6 8:03

语言:英文

总字数:450字

预计阅读时间:2分钟

评分:91分

标签:OpenRewrite,Java 重构,JUnit 迁移,Gradle,软件开发效率


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

Eliminating tedium with OpenRewrite

refactoring development developer experience automation


Public workshops: Sept 4-6th & 23rd-25th – Architecting for fast, sustainable flow – enabling DevOps and Team Topologies thru architecture. Learn more and enroll.


I find some tasks so tedious that I tend to avoid them.For example, I’ve recently been working on a sample application for a new, soon to be released Eventuate saga framework (more on that later).I’d created the application by copying another sample that was still using JUnit 4.But I’d been putting off updating the tests to JUnit 5.That is, until today, when I got it done in a few minutes using OpenRewrite.

What is OpenRewrite?

OpenRewrite is a tool for refactoring Java code.It runs recipes that transform your source code.By running a single command, you can quickly and reliably make what would otherwise be very time-consuming changes to your code base.

There is a comprehensive collection of recipes available.I’d previously used it to migrate an application to Spring Boot 3.There’s also a recipe for migrating a Spring Boot application from JUnit 4 to JUnit 5.

Running OpenRewrite

There are a few different ways to run OpenRewrite.Many examples modify your pom.xml or build.gradle files.But since you typically run a recipe once, my preferred approach is to use a Gradle init script.This allows me to run OpenRewrite from the command line without modifying my project.

The Gradle init script for running OpenRewrite

Here’s the Gradle init script (copied from the OpenRewrite docs) that I used to run the JUnit 4 to JUnit 5 migration recipe:

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("org.openrewrite.recipe:rewrite-spring:latest.release")    }    afterEvaluate {        if (repositories.isEmpty()) {            repositories {                mavenCentral()            }        }    }}

The script declares org.openrewrite.recipe:rewrite-spring, which contains the needed recipe, as a dependency.

Running the recipe

Here’s the shell command that I used to run OpenRewrite:

 ./gradlew rewriteRun --init-script <path to initscript> \    -Drewrite.activeRecipe=org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration \    -Dorg.gradle.jvmargs=-Xmx8G

This command runs the rewriteRun task, which is defined by the init script.The rewrite.activeRecipe property specifies the recipe to run.I needed to increase the heap size to prevent OpenRewrite from running out of memory.

The results

The recipe made numerous changes to the code base including:

  • Updating the build.gradle dependencies
  • Fixing the imports
  • Removing @RunWith(SpringJUnit4ClassRunner.class) annotations
  • Replacing @RunWith(MockitoJUnitRunner.class) with @ExtendWith(MockitoExtension.class)
  • Replacing @Before with @BeforeEach

The only thing I needed to do was change some testing related plugins to use useJUnitPlatform().

Pretty awesome!OpenRewrite estimated that it saved me 55 minutes of work.

The code

Here’s a handy shell script that migrates your project from JUnit 4 to JUnit 5.

Need help with accelerating software delivery?

I’m available to help your organization improve agility and competitiveness through better software architecture: training workshops, architecture reviews, etc.

Learn more about how I can help