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.00
# OpenRewrite Maven
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: org.openrewrite:rewrite-maven
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Version**: 8.61.3
10
- **License**: Apache 2.0
11
- **Installation**: Add to your Maven or Gradle build configuration
12
13
### Maven Dependency
14
15
```xml
16
<dependency>
17
<groupId>org.openrewrite</groupId>
18
<artifactId>rewrite-maven</artifactId>
19
<version>8.61.3</version>
20
</dependency>
21
```
22
23
### Gradle Dependency
24
25
```groovy
26
implementation 'org.openrewrite:rewrite-maven:8.61.3'
27
```
28
29
## Core Imports
30
31
```java
32
import org.openrewrite.maven.MavenParser;
33
import org.openrewrite.maven.MavenVisitor;
34
import org.openrewrite.maven.tree.MavenResolutionResult;
35
import org.openrewrite.maven.tree.Pom;
36
import org.openrewrite.maven.tree.Dependency;
37
import org.openrewrite.maven.tree.ResolvedDependency;
38
import org.openrewrite.ExecutionContext;
39
```
40
41
## Basic Usage
42
43
```java
44
import org.openrewrite.maven.MavenParser;
45
import org.openrewrite.maven.tree.MavenResolutionResult;
46
import org.openrewrite.maven.tree.ResolvedDependency;
47
import org.openrewrite.maven.tree.Scope;
48
import org.openrewrite.xml.tree.Xml;
49
import org.openrewrite.ExecutionContext;
50
import org.openrewrite.InMemoryExecutionContext;
51
import org.openrewrite.SourceFile;
52
import java.util.List;
53
import java.util.Map;
54
55
// Parse a Maven POM file
56
MavenParser parser = MavenParser.builder()
57
.activeProfiles("dev", "test")
58
.build();
59
60
ExecutionContext ctx = new InMemoryExecutionContext();
61
List<SourceFile> parsedFiles = parser.parse(ctx, pomXmlContent);
62
63
// Access Maven-specific information
64
Xml.Document pomDocument = (Xml.Document) parsedFiles.get(0);
65
MavenResolutionResult result = pomDocument.getMarkers()
66
.findFirst(MavenResolutionResult.class)
67
.orElse(null);
68
69
if (result != null) {
70
// Get dependencies by scope
71
Map<Scope, List<ResolvedDependency>> dependenciesByScope = result.getDependencies();
72
73
// Work with resolved dependencies
74
for (Map.Entry<Scope, List<ResolvedDependency>> entry : dependenciesByScope.entrySet()) {
75
Scope scope = entry.getKey();
76
List<ResolvedDependency> dependencies = entry.getValue();
77
78
System.out.println("Dependencies in " + scope + " scope:");
79
for (ResolvedDependency dep : dependencies) {
80
System.out.println(" " + dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion());
81
}
82
}
83
}
84
```
85
86
## Architecture
87
88
OpenRewrite Maven is built around several key components:
89
90
- **Parser System**: `MavenParser` converts Maven POM XML files into OpenRewrite's AST with full dependency resolution
91
- **Data Model**: Comprehensive tree model (`org.openrewrite.maven.tree`) representing all Maven POM concepts
92
- **Visitor Pattern**: `MavenVisitor` and `MavenIsoVisitor` enable transformation of Maven POMs through tree traversal
93
- **Recipe System**: 30+ built-in recipes for common Maven operations and refactorings
94
- **Search Capabilities**: Query and analyze Maven projects and dependencies
95
- **Caching System**: Pluggable caching for Maven artifacts and POM files to improve performance
96
97
## Capabilities
98
99
### Core Parsing and Visitor Functionality
100
101
Essential parsing capabilities for converting Maven POM files to OpenRewrite AST and visitor patterns for transformations.
102
103
```java { .api }
104
// Core parser with dependency resolution
105
public static MavenParser.Builder builder();
106
107
// Base visitor for Maven transformations
108
public abstract class MavenVisitor<P> extends XmlVisitor<P>;
109
110
// Identity-preserving visitor
111
public class MavenIsoVisitor<P> extends MavenVisitor<P>;
112
```
113
114
[Core Parsing and Visitors](./core-parsing.md)
115
116
### Data Model and Tree Structures
117
118
Complete data model representing Maven POM files, dependencies, plugins, and all Maven concepts.
119
120
```java { .api }
121
// Core POM representation
122
public class Pom implements Serializable;
123
124
// Maven resolution result containing all resolved information
125
public class MavenResolutionResult;
126
127
// Fully resolved POM with dependency resolution
128
public class ResolvedPom;
129
130
// Maven coordinate representations
131
public class GroupArtifact implements Serializable;
132
public class GroupArtifactVersion implements Serializable;
133
134
// Dependency representations
135
public class Dependency implements Serializable, Attributed;
136
public class ResolvedDependency implements Serializable;
137
```
138
139
[Data Model and Tree Structures](./data-model.md)
140
141
### Recipe Transformations
142
143
30+ built-in recipes for common Maven operations including dependency management, plugin configuration, and project structure changes.
144
145
```java { .api }
146
// Dependency management recipes
147
public class AddDependency extends Recipe;
148
public class RemoveDependency extends Recipe;
149
public class UpgradeDependencyVersion extends Recipe;
150
151
// Plugin management recipes
152
public class AddPlugin extends Recipe;
153
public class UpgradePluginVersion extends Recipe;
154
155
// Property management recipes
156
public class AddProperty extends Recipe;
157
public class ChangePropertyValue extends Recipe;
158
```
159
160
[Recipe Transformations](./recipes.md)
161
162
### Search and Analysis
163
164
Query and analyze Maven projects to find dependencies, plugins, and other Maven elements.
165
166
```java { .api }
167
// Search recipes for finding Maven elements
168
public class FindDependency extends Recipe;
169
public class FindPlugin extends Recipe;
170
public class DependencyInsight extends Recipe;
171
172
// Analysis of effective configurations
173
public class EffectiveDependencies extends Recipe;
174
public class EffectiveManagedDependencies extends Recipe;
175
```
176
177
[Search and Analysis](./search-analysis.md)
178
179
### Caching System
180
181
Pluggable caching interfaces and implementations for Maven artifacts and POM files to improve performance.
182
183
```java { .api }
184
// Core caching interfaces
185
public interface MavenArtifactCache;
186
public interface MavenPomCache;
187
188
// Cache implementations
189
public class LocalMavenArtifactCache implements MavenArtifactCache;
190
public class InMemoryMavenPomCache implements MavenPomCache;
191
public class RocksdbMavenPomCache implements MavenPomCache;
192
```
193
194
[Caching System](./caching.md)
195
196
### Utilities and Helpers
197
198
Utility classes for common Maven operations including Maven wrapper support and dependency visualization.
199
200
```java { .api }
201
// Maven wrapper utilities
202
public class MavenWrapper;
203
204
// Artifact downloading
205
public class MavenArtifactDownloader;
206
207
// Dependency visualization
208
public class PrintMavenAsDot extends Recipe;
209
```
210
211
[Utilities and Helpers](./utilities.md)
212
213
## Types
214
215
### Core Configuration Types
216
217
```java { .api }
218
// Maven settings representation
219
public class MavenSettings;
220
221
// Security settings for encrypted passwords
222
public class MavenSecuritySettings;
223
224
// Execution context for Maven operations
225
public class MavenExecutionContextView extends DelegatingExecutionContext;
226
227
// Maven-specific exceptions
228
public class MavenDownloadingException extends Exception;
229
public class MavenDownloadingExceptions extends Exception;
230
```
231
232
### Dependency Scopes
233
234
```java { .api }
235
public enum Scope {
236
Compile, // Default scope, available in all classpaths
237
Test, // Only available for test compilation and execution
238
Runtime, // Not required for compilation but needed for execution
239
Provided, // Expected to be provided by runtime environment
240
System, // Similar to provided but must be explicitly provided
241
Import, // Only supported on dependency of type 'pom' in dependencyManagement
242
None, // No scope specified
243
Invalid // Invalid or unrecognized scope
244
}
245
```
246
247
### Attribute System
248
249
```java { .api }
250
// Base interface for Maven element attributes
251
public interface Attribute extends Serializable;
252
253
// Interface for elements that can have attributes
254
public interface Attributed {
255
List<Attribute> getAttributes();
256
}
257
```