or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

caching.mdcore-parsing.mddata-model.mdindex.mdrecipes.mdsearch-analysis.mdutilities.md
tile.json

tessl/maven-org-openrewrite--rewrite-maven

OpenRewrite Maven parsing and refactoring library that provides Maven POM file parsing, analysis, and automated refactoring capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.openrewrite/rewrite-maven@8.61.x

To install, run

npx @tessl/cli install tessl/maven-org-openrewrite--rewrite-maven@8.61.0

index.mddocs/

OpenRewrite Maven

OpenRewrite 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.

Package Information

  • Package Name: org.openrewrite:rewrite-maven
  • Package Type: Maven
  • Language: Java
  • Version: 8.61.3
  • License: Apache 2.0
  • Installation: Add to your Maven or Gradle build configuration

Maven Dependency

<dependency>
    <groupId>org.openrewrite</groupId>
    <artifactId>rewrite-maven</artifactId>
    <version>8.61.3</version>
</dependency>

Gradle Dependency

implementation 'org.openrewrite:rewrite-maven:8.61.3'

Core Imports

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;

Basic Usage

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());
        }
    }
}

Architecture

OpenRewrite Maven is built around several key components:

  • Parser System: MavenParser converts Maven POM XML files into OpenRewrite's AST with full dependency resolution
  • Data Model: Comprehensive tree model (org.openrewrite.maven.tree) representing all Maven POM concepts
  • Visitor Pattern: MavenVisitor and MavenIsoVisitor enable transformation of Maven POMs through tree traversal
  • Recipe System: 30+ built-in recipes for common Maven operations and refactorings
  • Search Capabilities: Query and analyze Maven projects and dependencies
  • Caching System: Pluggable caching for Maven artifacts and POM files to improve performance

Capabilities

Core Parsing and Visitor Functionality

Essential 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>;

Core Parsing and Visitors

Data Model and Tree Structures

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

Recipe Transformations

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;

Recipe Transformations

Search and Analysis

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;

Search and Analysis

Caching System

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;

Caching System

Utilities and Helpers

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;

Utilities and Helpers

Types

Core Configuration Types

// 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;

Dependency Scopes

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
}

Attribute System

// Base interface for Maven element attributes
public interface Attribute extends Serializable;

// Interface for elements that can have attributes
public interface Attributed {
    List<Attribute> getAttributes();
}