OpenRewrite: how to visit sourceFiles with a custom extension in a JAVA Project?

737 Views Asked by At

I am creating a recipe to modify a final JAVA project according to certain conditions.

The use case would be:

  • IF there is a file called "file.test" in the project.
  • THEN I add a maven property to the project's pom.xml

It seems simple, but I can't get any Visitor to handle a file with *.test extension.

For example I have tried the following:

static class MarkerRecipe extends Recipe {

    @Override
    public String getDisplayName() {
      return "arker Recipe";
    }

    @Override
    protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
      return new HasSourcePath<>("**/*.test");
    }

    @Override
    protected PlainTextVisitor<ExecutionContext> getVisitor() {
      return new PlainTextVisitor<ExecutionContext>() {
        @Override
        public @Nullable PlainText postVisit(final PlainText tree, final ExecutionContext executionContext) {
          if (tree.getSourcePath().toString().endsWith("file.test")) {
            // Add maven Property
            MarkerRecipe.this.doNext(new ChangePropertyValue("maven-property", "to fill!", true));
          }
          return super.postVisit(tree, executionContext);
        }
      };
    }
  }

I have also tried using the org.openrewrite.FindSourceFiles Recipe as follows by adding it to my princpipal recipe and debugging to see if it detects *.test file types:

    this.doNext(new FindSourceFiles("*.test"));

I have created .test files both inside src/main/java, src/main/resources, src/main/files, but nothing works...

any idea?

I'm using open-rewrite 7.22.0

1

There are 1 best solutions below

1
On

Have a look at the new Quark which was introduced in rewrite 7.23.0. Now the build plugins will add a Quark with a Path and Markers to the source set for each file not supported by any other parser.