OpenRewrite Maven parsing and refactoring library that provides Maven POM file parsing, analysis, and automated refactoring capabilities
—
30+ built-in recipes for common Maven operations including dependency management, plugin configuration, and project structure changes.
All Maven recipes extend the OpenRewrite Recipe base class and can be applied to Maven projects.
/**
* Base class for all OpenRewrite recipes
*/
public abstract class Recipe {
/**
* Get the display name of this recipe
*/
public abstract String getDisplayName();
/**
* Get a description of what this recipe does
*/
public abstract String getDescription();
/**
* Apply this recipe to source files
*/
public abstract TreeVisitor<?, ExecutionContext> getVisitor();
}Add a new dependency to the project's dependencies section.
/**
* Recipe to add a new dependency to Maven POM
*/
public class AddDependency extends Recipe {
/**
* Create recipe to add dependency
* @param groupId Group ID of dependency to add
* @param artifactId Artifact ID of dependency to add
* @param version Version of dependency to add
* @param versionPattern Optional version pattern for dynamic versions
* @param scope Dependency scope (default: Compile)
* @param releasesOnly Whether to only consider release versions
* @param onlyIfUsing Only add if these classes/packages are used
* @param classifier Optional classifier
* @param optional Whether dependency is optional
* @param type Dependency type (default: jar)
* @param familyPattern Pattern to match against recipe family
* @param acceptTransitive Accept transitive dependencies
*/
public AddDependency(String groupId, String artifactId, String version,
@Nullable String versionPattern, @Nullable String scope,
@Nullable Boolean releasesOnly, @Nullable String onlyIfUsing,
@Nullable String classifier, @Nullable Boolean optional,
@Nullable String type, @Nullable String familyPattern,
@Nullable Boolean acceptTransitive);
}Usage Examples:
// Add JUnit dependency
AddDependency addJUnit = new AddDependency(
"org.junit.jupiter", "junit-jupiter", "5.8.2",
null, "test", null, null, null, null, null, null, null
);
// Add Spring Boot starter with version pattern
AddDependency addSpringBoot = new AddDependency(
"org.springframework.boot", "spring-boot-starter", null,
"2.7.x", "compile", true, null, null, null, null, null, null
);
// Apply recipe
Recipe recipe = addJUnit;
RecipeRun run = recipe.run(List.of(pomDocument), ctx);
List<Result> results = run.getResults();Remove a dependency from the project.
/**
* Recipe to remove a dependency from Maven POM
*/
public class RemoveDependency extends Recipe {
/**
* Create recipe to remove dependency
* @param groupId Group ID of dependency to remove
* @param artifactId Artifact ID of dependency to remove
*/
public RemoveDependency(String groupId, String artifactId);
}Upgrade a dependency to a newer version.
/**
* Recipe to upgrade dependency version
*/
public class UpgradeDependencyVersion extends Recipe {
/**
* Create recipe to upgrade dependency version
* @param groupId Group ID of dependency to upgrade
* @param artifactId Artifact ID of dependency to upgrade
* @param newVersion New version to upgrade to
* @param versionPattern Optional version pattern constraint
* @param overrideManagedVersion Whether to override managed version
*/
public UpgradeDependencyVersion(String groupId, String artifactId, String newVersion,
@Nullable String versionPattern,
@Nullable Boolean overrideManagedVersion);
}Change the scope of an existing dependency.
/**
* Recipe to change dependency scope
*/
public class ChangeDependencyScope extends Recipe {
/**
* Create recipe to change dependency scope
* @param groupId Group ID of dependency
* @param artifactId Artifact ID of dependency
* @param newScope New scope to apply
*/
public ChangeDependencyScope(String groupId, String artifactId, String newScope);
}Change the coordinates of a dependency (useful for artifact relocations).
/**
* Recipe to change dependency coordinates
*/
public class ChangeDependencyGroupIdAndArtifactId extends Recipe {
/**
* Create recipe to change dependency coordinates
* @param oldGroupId Current group ID
* @param oldArtifactId Current artifact ID
* @param newGroupId New group ID
* @param newArtifactId New artifact ID
* @param newVersion Optional new version
*/
public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId,
String newGroupId, String newArtifactId,
@Nullable String newVersion);
}Add a managed dependency to the dependencyManagement section.
/**
* Recipe to add managed dependency
*/
public class AddManagedDependency extends Recipe {
/**
* Create recipe to add managed dependency
* @param groupId Group ID of managed dependency
* @param artifactId Artifact ID of managed dependency
* @param version Version of managed dependency
* @param scope Optional scope for managed dependency
* @param type Optional type for managed dependency
* @param classifier Optional classifier for managed dependency
*/
public AddManagedDependency(String groupId, String artifactId, String version,
@Nullable String scope, @Nullable String type,
@Nullable String classifier);
}Remove a managed dependency from dependencyManagement.
/**
* Recipe to remove managed dependency
*/
public class RemoveManagedDependency extends Recipe {
/**
* Create recipe to remove managed dependency
* @param groupId Group ID of managed dependency to remove
* @param artifactId Artifact ID of managed dependency to remove
*/
public RemoveManagedDependency(String groupId, String artifactId);
}Add a Maven plugin to the build plugins section.
/**
* Recipe to add Maven plugin
*/
public class AddPlugin extends Recipe {
/**
* Create recipe to add Maven plugin
* @param groupId Plugin group ID (default: org.apache.maven.plugins)
* @param artifactId Plugin artifact ID
* @param version Plugin version
* @param configuration Optional plugin configuration
* @param dependencies Optional plugin dependencies
* @param executions Optional plugin executions
*/
public AddPlugin(String groupId, String artifactId, @Nullable String version,
@Nullable String configuration, @Nullable List<Dependency> dependencies,
@Nullable List<PluginExecution> executions);
}Upgrade a Maven plugin to a newer version.
/**
* Recipe to upgrade plugin version
*/
public class UpgradePluginVersion extends Recipe {
/**
* Create recipe to upgrade plugin version
* @param groupId Plugin group ID
* @param artifactId Plugin artifact ID
* @param newVersion New version to upgrade to
*/
public UpgradePluginVersion(String groupId, String artifactId, String newVersion);
}Modify plugin configuration.
/**
* Recipe to change plugin configuration
*/
public class ChangePluginConfiguration extends Recipe {
/**
* Create recipe to change plugin configuration
* @param groupId Plugin group ID
* @param artifactId Plugin artifact ID
* @param configuration New configuration XML
*/
public ChangePluginConfiguration(String groupId, String artifactId, String configuration);
}Modify plugin executions.
/**
* Recipe to change plugin executions
*/
public class ChangePluginExecutions extends Recipe {
/**
* Create recipe to change plugin executions
* @param groupId Plugin group ID
* @param artifactId Plugin artifact ID
* @param executions New executions configuration
*/
public ChangePluginExecutions(String groupId, String artifactId,
List<PluginExecution> executions);
}Add a property to the project properties section.
/**
* Recipe to add Maven property
*/
public class AddProperty extends Recipe {
/**
* Create recipe to add property
* @param key Property name
* @param value Property value
* @param preserveExisting Whether to preserve existing value if present
*/
public AddProperty(String key, String value, Boolean preserveExisting);
}Change the value of an existing property.
/**
* Recipe to change property value
*/
public class ChangePropertyValue extends Recipe {
/**
* Create recipe to change property value
* @param key Property name to change
* @param newValue New property value
* @param addIfMissing Whether to add property if it doesn't exist
*/
public ChangePropertyValue(String key, String newValue, Boolean addIfMissing);
}Remove a property from the project.
/**
* Recipe to remove Maven property
*/
public class RemoveProperty extends Recipe {
/**
* Create recipe to remove property
* @param key Property name to remove
*/
public RemoveProperty(String key);
}Remove properties that are redundant or have default values.
/**
* Recipe to remove redundant properties
*/
public class RemoveRedundantProperties extends Recipe {
// No constructor parameters - removes known redundant properties
}Add or change the parent POM reference.
/**
* Recipe to add parent POM
*/
public class AddParentPom extends Recipe {
/**
* Create recipe to add parent POM
* @param groupId Parent group ID
* @param artifactId Parent artifact ID
* @param version Parent version
* @param relativePath Optional relative path to parent
* @param versionPattern Optional version pattern
*/
public AddParentPom(String groupId, String artifactId, String version,
@Nullable String relativePath, @Nullable String versionPattern);
}Change the parent POM coordinates.
/**
* Recipe to change parent POM
*/
public class ChangeParentPom extends Recipe {
/**
* Create recipe to change parent POM
* @param oldGroupId Current parent group ID
* @param oldArtifactId Current parent artifact ID
* @param newGroupId New parent group ID
* @param newArtifactId New parent artifact ID
* @param newVersion New parent version
* @param versionPattern Optional version pattern
*/
public ChangeParentPom(String oldGroupId, String oldArtifactId,
String newGroupId, String newArtifactId, String newVersion,
@Nullable String versionPattern);
}Increment the project version following semantic versioning.
/**
* Recipe to increment project version
*/
public class IncrementProjectVersion extends Recipe {
/**
* Create recipe to increment version
* @param incrementType Type of increment (major, minor, patch)
*/
public IncrementProjectVersion(String incrementType);
}Add XML comments to dependencies for documentation.
/**
* Recipe to add comment to Maven dependency
*/
public class AddCommentToMavenDependency extends Recipe {
/**
* Create recipe to add dependency comment
* @param groupId Dependency group ID
* @param artifactId Dependency artifact ID
* @param comment Comment text to add
*/
public AddCommentToMavenDependency(String groupId, String artifactId, String comment);
}Remove duplicate dependency declarations.
/**
* Recipe to remove duplicate dependencies
*/
public class RemoveDuplicateDependencies extends Recipe {
// No constructor parameters - removes all duplicates automatically
}Add exclusions to dependencies.
/**
* Recipe to add dependency exclusion
*/
public class AddExclusion extends Recipe {
/**
* Create recipe to add exclusion
* @param groupId Dependency group ID to add exclusion to
* @param artifactId Dependency artifact ID to add exclusion to
* @param exclusionGroupId Group ID to exclude
* @param exclusionArtifactId Artifact ID to exclude
*/
public AddExclusion(String groupId, String artifactId,
String exclusionGroupId, String exclusionArtifactId);
}Remove exclusions from dependencies.
/**
* Recipe to remove dependency exclusion
*/
public class RemoveExclusion extends Recipe {
/**
* Create recipe to remove exclusion
* @param groupId Dependency group ID to remove exclusion from
* @param artifactId Dependency artifact ID to remove exclusion from
* @param exclusionGroupId Group ID exclusion to remove
* @param exclusionArtifactId Artifact ID exclusion to remove
*/
public RemoveExclusion(String groupId, String artifactId,
String exclusionGroupId, String exclusionArtifactId);
}Update Java version properties in Maven projects.
/**
* Recipe to update Java version properties
*/
public class UpdateMavenProjectPropertyJavaVersion extends Recipe {
/**
* Create recipe to update Java version
* @param version New Java version (e.g., "17", "11", "8")
*/
public UpdateMavenProjectPropertyJavaVersion(String version);
}Use Maven Compiler Plugin release configuration instead of source/target properties.
/**
* Recipe to use Maven Compiler Plugin release configuration
*/
public class UseMavenCompilerPluginReleaseConfiguration extends Recipe {
/**
* Create recipe to use release configuration
* @param releaseVersion Java release version (e.g., "17", "11")
*/
public UseMavenCompilerPluginReleaseConfiguration(String releaseVersion);
}Modernize obsolete POM configurations and deprecated elements.
/**
* Recipe to modernize obsolete POM configurations
*/
public class ModernizeObsoletePoms extends Recipe {
// No constructor parameters - automatically modernizes obsolete configurations
}Add Develocity (Gradle Enterprise) Maven extension.
/**
* Recipe to add Develocity Maven extension
*/
public class AddDevelocityMavenExtension extends Recipe {
/**
* Create recipe to add Develocity extension
* @param version Develocity extension version
* @param server Optional Develocity server URL
*/
public AddDevelocityMavenExtension(String version, @Nullable String server);
}Enable Develocity build cache in Maven projects.
/**
* Recipe to enable Develocity build cache
*/
public class EnableDevelocityBuildCache extends Recipe {
// No constructor parameters - enables build cache automatically
}Add dependencies to Maven plugins.
/**
* Recipe to add plugin dependency
*/
public class AddPluginDependency extends Recipe {
/**
* Create recipe to add plugin dependency
* @param pluginGroupId Plugin group ID
* @param pluginArtifactId Plugin artifact ID
* @param dependencyGroupId Dependency group ID to add
* @param dependencyArtifactId Dependency artifact ID to add
* @param dependencyVersion Dependency version
*/
public AddPluginDependency(String pluginGroupId, String pluginArtifactId,
String dependencyGroupId, String dependencyArtifactId,
String dependencyVersion);
}Remove dependencies from Maven plugins.
/**
* Recipe to remove plugin dependency
*/
public class RemovePluginDependency extends Recipe {
/**
* Create recipe to remove plugin dependency
* @param pluginGroupId Plugin group ID
* @param pluginArtifactId Plugin artifact ID
* @param dependencyGroupId Dependency group ID to remove
* @param dependencyArtifactId Dependency artifact ID to remove
*/
public RemovePluginDependency(String pluginGroupId, String pluginArtifactId,
String dependencyGroupId, String dependencyArtifactId);
}Usage Examples:
// Apply single recipe
AddDependency recipe = new AddDependency(
"org.junit.jupiter", "junit-jupiter", "5.8.2",
null, "test", null, null, null, null, null, null, null
);
ExecutionContext ctx = new InMemoryExecutionContext();
RecipeRun run = recipe.run(List.of(pomDocument), ctx);
List<Result> results = run.getResults();
for (Result result : results) {
if (result.getDiff() != null) {
System.out.println("Applied change:");
System.out.println(result.getDiff());
}
}
// Compose multiple recipes
List<Recipe> recipes = Arrays.asList(
new AddDependency("org.junit.jupiter", "junit-jupiter", "5.8.2",
null, "test", null, null, null, null, null, null, null),
new ChangePropertyValue("maven.compiler.source", "17", true),
new ChangePropertyValue("maven.compiler.target", "17", true),
new UpgradePluginVersion("org.apache.maven.plugins", "maven-compiler-plugin", "3.8.1")
);
// Apply all recipes
for (Recipe recipe : recipes) {
RecipeRun run = recipe.run(sourceFiles, ctx);
sourceFiles = run.getResults().stream()
.map(result -> result.getAfter())
.filter(Objects::nonNull)
.collect(toList());
}
// Use RecipeSpec for declarative configuration
RecipeSpec spec = new RecipeSpec();
spec.setName("com.example.UpdateProject");
spec.setDisplayName("Update Project Dependencies");
spec.setRecipeList(Arrays.asList(
"org.openrewrite.maven.AddDependency",
"org.openrewrite.maven.UpgradeDependencyVersion"
));
Recipe compositeRecipe = Recipe.fromSpec(spec);Install with Tessl CLI
npx tessl i tessl/maven-org-openrewrite--rewrite-maven