openrewrite Maven plugin configuration

1goal

1.1mvn rewrite:run

1.1.1Run the configured recipes and apply the changes locally.

1.2mvn rewrite:runNoFork

1.2.1运行run命令但不改变maven 的life cycle,因此更方便用于多个maven goal 一起运行

1.3mvn rewrite:dryRun

1.3.1不修改原文件,而是在target/rewrite目录下生成一个diff格式的 rewrite.patch 文件

1.4mvn rewrite:dryRunNoFork

1.4.1运行dryRun命令但不改变maven 的life cycle,因此更方便用于多个maven goal 一起运行

1.5mvn rewrite:discover

1.5.1列出所有recipe,注意style也是一种recipe

2-D 参数

2.1-D 参数比配置文件优先级高,即-D会替换配置文件内的配置

2.1.1-Drewrite.activeRecipes=org.example.R1, org.example.R2

2.1.2-Drewrite.activeStyles=org.example.Style

2.1.3-Drewrite.recipeArtifactCoordinates=org.example:your-recipe:1.0

指定recipe 对应的maven artifact

3Plugin configuration

3.1activeRecipes

3.2activeStyles

3.2.1代码风格

3.3checkstyleDetectionEnabled

3.3.1默认会侦测maven Checkstyle plugin 并使用对应style

xml 复制代码
<artifactId>maven-checkstyle-plugin</artifactId>
				<version>3.3.1</version>
				<executions>
					<execution>
						<id>verify-style</id>
						<phase>process-classes</phase>
						<goals>
							<goal>check</goal>
						</goals>
					</execution>
				</executions>

3.4configLocation

3.4.1rewrite.yml 文件所在目录

3.5dependencies

3.5.1依赖 artificial,一般用于非官方recipes

3.6failOnDryRunResults

3.6.1DryRun发现内容需要修改时候返回fail

3.6.2默认false,不返回

3.7exclusions

3.7.1排除内容

3.8plainTextMasks

3.8.1指出那些文件为plaintext格式

3.8.2默认内容

xml 复制代码
**/META-INF/services/**
**/META-INF/spring.factories
**/META-INF/spring/**
**/*.bash
**/*.bat
**/CODEOWNERS
**/*.config
**/Dockerfile
**/.gitattributes
**/.gitignore
**/.java-version
**/*.jsp
**/*.ksh
**/*.md
**/*.qute.java
**/.sdkmanrc
**/*.sh
**/*.sql
**/*.txt

3.9pomCacheDirectory

3.9.1pom.xml文件cache目录

3.9.2默认为 ~/.rewrite-cache

3.10pomCacheEnabled

3.10.1default is true

3.11sizeThresholdMb

3.11.1非java 资源被忽略分析的上限大小

3.11.2The default threshold is 10Mb.

3.12skipMavenParsing

3.12.1跳过pom文件分析,默认为false

3.13 exportDatatables

3.13.1 export the data tables generated by the active recipes.

3.13.2The default is false.

3.13.3The resulting files can be found in the directory <build directory>/reports/rewrite/datatables/<timestamp>.

files

  • org.openrewrite.table.RecipeRunStats.csv
  • org.openrewrite.table.SourcesFileResults.csv

注意在运行目录下,使用maven cli的时候要注意不一定在 修改的project 目录下

3.13.4 核心代码

java 复制代码
 protected List<Result> runRecipe(Recipe recipe, LargeSourceSet sourceSet, ExecutionContext ctx) {
        getLog().info("Running recipe(s)...");
        RecipeRun recipeRun = recipe.run(sourceSet, ctx);

        if (exportDatatables) {
            String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss-SSS"));
            Path datatableDirectoryPath = Paths.get("target", "rewrite", "datatables", timestamp);
            getLog().info(String.format("Printing available datatables to: %s", datatableDirectoryPath));
            recipeRun.exportDatatablesToCsv(datatableDirectoryPath, ctx);
        }

        return recipeRun.getChangeset().getAllResults().stream().filter(source -> {
            // Remove ASTs originating from generated files
            if (source.getBefore() != null) {
                return !source.getBefore().getMarkers().findFirst(Generated.class).isPresent();
            }
            return true
        }).collect(toList());
    }

  public void exportDatatablesToCsv(Path filePath, ExecutionContext ctx) {
        try {
            Files.createDirectories(filePath);
        } catch (IOException e) {
            ctx.getOnError().accept(e);
        }
        for (Map.Entry<DataTable<?>, List<?>> entry : dataTables.entrySet()) {
            DataTable<?> dataTable = entry.getKey();
            List<?> rows = entry.getValue();
            File csv = filePath.resolve(dataTable.getName() + ".csv").toFile();
            try (PrintWriter printWriter = new PrintWriter(new FileOutputStream(csv, false))) {
                exportCsv(ctx, dataTable, printWriter::println, rows);
            } catch (FileNotFoundException e) {
                ctx.getOnError().accept(e);
            }
        }
    }
相关推荐
一只会写代码的猫19 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
萤丰信息20 小时前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧20 小时前
Eclipse为方法添加注释
java·ide·eclipse
我叫张小白。21 小时前
Spring Boot拦截器详解:实现统一的JWT认证
java·spring boot·web·jwt·拦截器·interceptor
Gerardisite1 天前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
2501_941147421 天前
基于 Rust 与 Actix 构建高性能微服务与低延迟分布式系统实践分享
maven
闲人编程1 天前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
故渊ZY1 天前
Java 代理模式:从原理到实战的全方位解析
java·开发语言·架构
匿者 衍1 天前
POI读取 excel 嵌入式图片(支持wps 和 office)
java·excel
一个尚在学习的计算机小白1 天前
java集合
java·开发语言