OpenRewrite Maven parsing and refactoring library that provides Maven POM file parsing, analysis, and automated refactoring capabilities
npx @tessl/cli install tessl/maven-org-openrewrite--rewrite-maven@8.61.0OpenRewrite Maven is a comprehensive Java library that provides Maven POM file parsing, dependency resolution, and automated refactoring capabilities. It enables developers to programmatically read, understand, and transform Maven project files and their dependencies, offering recipes for common Maven operations like adding/removing dependencies, updating versions, changing plugin configurations, and modernizing project structures.
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<version>8.61.3</version>
</dependency>implementation 'org.openrewrite:rewrite-maven:8.61.3'import org.openrewrite.maven.MavenParser;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.Dependency;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.ExecutionContext;import org.openrewrite.maven.MavenParser;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import java.util.List;
import java.util.Map;
// Parse a Maven POM file
MavenParser parser = MavenParser.builder()
.activeProfiles("dev", "test")
.build();
ExecutionContext ctx = new InMemoryExecutionContext();
List<SourceFile> parsedFiles = parser.parse(ctx, pomXmlContent);
// Access Maven-specific information
Xml.Document pomDocument = (Xml.Document) parsedFiles.get(0);
MavenResolutionResult result = pomDocument.getMarkers()
.findFirst(MavenResolutionResult.class)
.orElse(null);
if (result != null) {
// Get dependencies by scope
Map<Scope, List<ResolvedDependency>> dependenciesByScope = result.getDependencies();
// Work with resolved dependencies
for (Map.Entry<Scope, List<ResolvedDependency>> entry : dependenciesByScope.entrySet()) {
Scope scope = entry.getKey();
List<ResolvedDependency> dependencies = entry.getValue();
System.out.println("Dependencies in " + scope + " scope:");
for (ResolvedDependency dep : dependencies) {
System.out.println(" " + dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion());
}
}
}OpenRewrite Maven is built around several key components:
MavenParser converts Maven POM XML files into OpenRewrite's AST with full dependency resolutionorg.openrewrite.maven.tree) representing all Maven POM conceptsMavenVisitor and MavenIsoVisitor enable transformation of Maven POMs through tree traversalEssential parsing capabilities for converting Maven POM files to OpenRewrite AST and visitor patterns for transformations.
// Core parser with dependency resolution
public static MavenParser.Builder builder();
// Base visitor for Maven transformations
public abstract class MavenVisitor<P> extends XmlVisitor<P>;
// Identity-preserving visitor
public class MavenIsoVisitor<P> extends MavenVisitor<P>;Complete data model representing Maven POM files, dependencies, plugins, and all Maven concepts.
// Core POM representation
public class Pom implements Serializable;
// Maven resolution result containing all resolved information
public class MavenResolutionResult;
// Fully resolved POM with dependency resolution
public class ResolvedPom;
// Maven coordinate representations
public class GroupArtifact implements Serializable;
public class GroupArtifactVersion implements Serializable;
// Dependency representations
public class Dependency implements Serializable, Attributed;
public class ResolvedDependency implements Serializable;Data Model and Tree Structures
30+ built-in recipes for common Maven operations including dependency management, plugin configuration, and project structure changes.
// Dependency management recipes
public class AddDependency extends Recipe;
public class RemoveDependency extends Recipe;
public class UpgradeDependencyVersion extends Recipe;
// Plugin management recipes
public class AddPlugin extends Recipe;
public class UpgradePluginVersion extends Recipe;
// Property management recipes
public class AddProperty extends Recipe;
public class ChangePropertyValue extends Recipe;Query and analyze Maven projects to find dependencies, plugins, and other Maven elements.
// Search recipes for finding Maven elements
public class FindDependency extends Recipe;
public class FindPlugin extends Recipe;
public class DependencyInsight extends Recipe;
// Analysis of effective configurations
public class EffectiveDependencies extends Recipe;
public class EffectiveManagedDependencies extends Recipe;Pluggable caching interfaces and implementations for Maven artifacts and POM files to improve performance.
// Core caching interfaces
public interface MavenArtifactCache;
public interface MavenPomCache;
// Cache implementations
public class LocalMavenArtifactCache implements MavenArtifactCache;
public class InMemoryMavenPomCache implements MavenPomCache;
public class RocksdbMavenPomCache implements MavenPomCache;Utility classes for common Maven operations including Maven wrapper support and dependency visualization.
// Maven wrapper utilities
public class MavenWrapper;
// Artifact downloading
public class MavenArtifactDownloader;
// Dependency visualization
public class PrintMavenAsDot extends Recipe;// Maven settings representation
public class MavenSettings;
// Security settings for encrypted passwords
public class MavenSecuritySettings;
// Execution context for Maven operations
public class MavenExecutionContextView extends DelegatingExecutionContext;
// Maven-specific exceptions
public class MavenDownloadingException extends Exception;
public class MavenDownloadingExceptions extends Exception;public enum Scope {
Compile, // Default scope, available in all classpaths
Test, // Only available for test compilation and execution
Runtime, // Not required for compilation but needed for execution
Provided, // Expected to be provided by runtime environment
System, // Similar to provided but must be explicitly provided
Import, // Only supported on dependency of type 'pom' in dependencyManagement
None, // No scope specified
Invalid // Invalid or unrecognized scope
}// Base interface for Maven element attributes
public interface Attribute extends Serializable;
// Interface for elements that can have attributes
public interface Attributed {
List<Attribute> getAttributes();
}