OpenRewrite Maven parsing and refactoring library that provides Maven POM file parsing, analysis, and automated refactoring capabilities
—
Query and analyze Maven projects to find dependencies, plugins, and other Maven elements.
Recipe to find specific dependencies in Maven projects.
/**
* Recipe to find dependencies matching specified criteria
*/
public class FindDependency extends Recipe {
/**
* Create recipe to find dependency
* @param groupId Group ID to search for (supports wildcards)
* @param artifactId Artifact ID to search for (supports wildcards)
* @param scope Optional scope filter
* @param onlyDirect Only find direct dependencies (not transitive)
*/
public FindDependency(String groupId, String artifactId,
@Nullable String scope, @Nullable Boolean onlyDirect);
}Usage Examples:
// Find all Spring dependencies
FindDependency findSpring = new FindDependency("org.springframework", "*", null, null);
// Find only test dependencies
FindDependency findTestDeps = new FindDependency("*", "*", "test", null);
// Find direct JUnit dependencies only
FindDependency findJUnit = new FindDependency("org.junit.jupiter", "*", null, true);
// Apply and get results
RecipeRun run = findSpring.run(List.of(pomDocument), ctx);
List<FindDependency.Found> findings = run.getDataTable(FindDependency.Found.class);
for (FindDependency.Found found : findings) {
System.out.println("Found dependency: " + found.getGroupId() + ":" + found.getArtifactId());
System.out.println(" Version: " + found.getVersion());
System.out.println(" Scope: " + found.getScope());
System.out.println(" File: " + found.getSourcePath());
}Find managed dependencies in dependencyManagement sections.
/**
* Recipe to find managed dependencies
*/
public class FindManagedDependency extends Recipe {
/**
* Create recipe to find managed dependency
* @param groupId Group ID to search for (supports wildcards)
* @param artifactId Artifact ID to search for (supports wildcards)
*/
public FindManagedDependency(String groupId, String artifactId);
}Recipe to find Maven plugins in projects.
/**
* Recipe to find Maven plugins
*/
public class FindPlugin extends Recipe {
/**
* Create recipe to find plugin
* @param groupId Plugin group ID to search for (supports wildcards)
* @param artifactId Plugin artifact ID to search for (supports wildcards)
*/
public FindPlugin(String groupId, String artifactId);
}Usage Examples:
// Find all Maven plugins
FindPlugin findAllPlugins = new FindPlugin("*", "*");
// Find compiler plugin specifically
FindPlugin findCompiler = new FindPlugin("org.apache.maven.plugins", "maven-compiler-plugin");
// Get results
RecipeRun run = findCompiler.run(List.of(pomDocument), ctx);
List<FindPlugin.Found> findings = run.getDataTable(FindPlugin.Found.class);
for (FindPlugin.Found found : findings) {
System.out.println("Found plugin: " + found.getGroupId() + ":" + found.getArtifactId());
System.out.println(" Version: " + found.getVersion());
System.out.println(" Configuration: " + found.getConfiguration());
}Recipe to identify Maven projects and collect metadata.
/**
* Recipe to find and analyze Maven projects
*/
public class FindMavenProject extends Recipe {
// No constructor parameters - finds all Maven projects automatically
}Usage Examples:
FindMavenProject findProjects = new FindMavenProject();
RecipeRun run = findProjects.run(sourceFiles, ctx);
List<FindMavenProject.MavenProjectData> projects = run.getDataTable(FindMavenProject.MavenProjectData.class);
for (FindMavenProject.MavenProjectData project : projects) {
System.out.println("Maven Project: " + project.getGroupId() + ":" + project.getArtifactId());
System.out.println(" Version: " + project.getVersion());
System.out.println(" Packaging: " + project.getPackaging());
System.out.println(" Dependencies: " + project.getDependencyCount());
System.out.println(" Plugins: " + project.getPluginCount());
}Provides detailed analysis of dependency usage and resolution.
/**
* Recipe providing dependency analysis and insights
*/
public class DependencyInsight extends Recipe {
/**
* Create recipe for dependency insight
* @param groupIdPattern Group ID pattern to analyze
* @param artifactIdPattern Artifact ID pattern to analyze
* @param scope Optional scope filter
* @param onlyDirect Analyze only direct dependencies
*/
public DependencyInsight(String groupIdPattern, String artifactIdPattern,
@Nullable String scope, @Nullable Boolean onlyDirect);
}Usage Examples:
// Analyze Spring Boot dependencies
DependencyInsight springBootInsight = new DependencyInsight(
"org.springframework.boot", "*", null, false
);
RecipeRun run = springBootInsight.run(List.of(pomDocument), ctx);
List<DependencyInsight.DependencyReport> reports = run.getDataTable(DependencyInsight.DependencyReport.class);
for (DependencyInsight.DependencyReport report : reports) {
System.out.println("Dependency: " + report.getGroupId() + ":" + report.getArtifactId());
System.out.println(" Requested Version: " + report.getRequestedVersion());
System.out.println(" Resolved Version: " + report.getResolvedVersion());
System.out.println(" Scope: " + report.getScope());
System.out.println(" Direct: " + report.isDirect());
System.out.println(" Depth: " + report.getDepth());
if (!report.getConflicts().isEmpty()) {
System.out.println(" Version Conflicts:");
for (String conflict : report.getConflicts()) {
System.out.println(" - " + conflict);
}
}
}Recipe to show effective (resolved) dependencies including transitives.
/**
* Recipe to analyze effective dependencies
*/
public class EffectiveDependencies extends Recipe {
/**
* Create recipe for effective dependencies analysis
* @param scope Optional scope filter
* @param includeTransitive Include transitive dependencies
*/
public EffectiveDependencies(@Nullable String scope, @Nullable Boolean includeTransitive);
}Recipe to show effective managed dependencies from all sources.
/**
* Recipe to analyze effective managed dependencies
*/
public class EffectiveManagedDependencies extends Recipe {
// No constructor parameters - shows all effective managed dependencies
}Usage Examples:
// Show all effective dependencies
EffectiveDependencies effectiveDeps = new EffectiveDependencies(null, true);
RecipeRun run = effectiveDeps.run(List.of(pomDocument), ctx);
List<EffectiveDependencies.Effective> effective = run.getDataTable(EffectiveDependencies.Effective.class);
for (EffectiveDependencies.Effective dep : effective) {
System.out.println("Effective dependency: " + dep.getGroupId() + ":" + dep.getArtifactId());
System.out.println(" Version: " + dep.getVersion());
System.out.println(" Scope: " + dep.getScope());
System.out.println(" Source: " + dep.getSource()); // Direct, Parent, BOM, etc.
System.out.println(" Depth: " + dep.getDepth());
}
// Show effective managed dependencies
EffectiveManagedDependencies effectiveManaged = new EffectiveManagedDependencies();
RecipeRun managedRun = effectiveManaged.run(List.of(pomDocument), ctx);
List<EffectiveManagedDependencies.Managed> managed = managedRun.getDataTable(EffectiveManagedDependencies.Managed.class);
for (EffectiveManagedDependencies.Managed dep : managed) {
System.out.println("Managed dependency: " + dep.getGroupId() + ":" + dep.getArtifactId());
System.out.println(" Version: " + dep.getVersion());
System.out.println(" Source: " + dep.getSource()); // Parent, BOM, Current
}Recipe to find projects that do not include specific dependencies.
/**
* Recipe to find projects missing specific dependencies
*/
public class DoesNotIncludeDependency extends Recipe {
/**
* Create recipe to find missing dependencies
* @param groupId Group ID that should be present
* @param artifactId Artifact ID that should be present
* @param onlyDirect Only check direct dependencies
*/
public DoesNotIncludeDependency(String groupId, String artifactId, @Nullable Boolean onlyDirect);
}Recipe to check if specific modules have dependencies.
/**
* Recipe to check if modules have specific dependencies
*/
public class ModuleHasDependency extends Recipe {
/**
* Create recipe to check module dependencies
* @param groupId Dependency group ID to search for
* @param artifactId Dependency artifact ID to search for
* @param scope Optional scope filter
*/
public ModuleHasDependency(String groupId, String artifactId, @Nullable String scope);
}Recipe to check if specific modules have plugins.
/**
* Recipe to check if modules have specific plugins
*/
public class ModuleHasPlugin extends Recipe {
/**
* Create recipe to check module plugins
* @param groupId Plugin group ID to search for
* @param artifactId Plugin artifact ID to search for
*/
public ModuleHasPlugin(String groupId, String artifactId);
}Recipe to find and analyze Maven settings files.
/**
* Recipe to find Maven settings files
*/
public class FindMavenSettings extends Recipe {
// No constructor parameters - finds all Maven settings automatically
}Recipe to find SCM (Source Control Management) information in POMs.
/**
* Recipe to find SCM information
*/
public class FindScm extends Recipe {
/**
* Create recipe to find SCM information
* @param connectionPattern Optional pattern to match SCM connections
*/
public FindScm(@Nullable String connectionPattern);
}Recipe to show effective Maven repositories from all sources.
/**
* Recipe to analyze effective Maven repositories
*/
public class EffectiveMavenRepositories extends Recipe {
// No constructor parameters - shows all effective repositories
}Find and analyze Maven repositories configured in projects.
/**
* Recipe to find Maven repositories
*/
public class FindMavenRepository extends Recipe {
/**
* Create recipe to find repositories
* @param id Optional repository ID to search for
* @param url Optional repository URL pattern to search for
*/
public FindMavenRepository(@Nullable String id, @Nullable String url);
}Find active Maven profiles in projects.
/**
* Recipe to find active Maven profiles
*/
public class FindActiveProfiles extends Recipe {
/**
* Create recipe to find active profiles
* @param profileId Optional specific profile ID to search for
*/
public FindActiveProfiles(@Nullable String profileId);
}Find Maven properties and their usage.
/**
* Recipe to find Maven properties
*/
public class FindProperties extends Recipe {
/**
* Create recipe to find properties
* @param propertyPattern Property name pattern to search for
* @param includeInherited Include properties from parent POMs
*/
public FindProperties(String propertyPattern, @Nullable Boolean includeInherited);
}Usage Examples:
// Find all Java version related properties
FindProperties findJavaProps = new FindProperties("*java*", true);
RecipeRun run = findJavaProps.run(List.of(pomDocument), ctx);
List<FindProperties.PropertyData> properties = run.getDataTable(FindProperties.PropertyData.class);
for (FindProperties.PropertyData prop : properties) {
System.out.println("Property: " + prop.getKey() + " = " + prop.getValue());
System.out.println(" Source: " + prop.getSource());
System.out.println(" Usage Count: " + prop.getUsageCount());
}Find known security vulnerabilities in dependencies.
/**
* Recipe to find security vulnerabilities in dependencies
*/
public class FindSecurityVulnerabilities extends Recipe {
/**
* Create recipe to find vulnerabilities
* @param scope Optional scope filter for vulnerability scanning
* @param overrideTransitive Override transitive dependency versions for fixes
*/
public FindSecurityVulnerabilities(@Nullable String scope, @Nullable Boolean overrideTransitive);
}Find and analyze licenses of dependencies.
/**
* Recipe to find dependency licenses
*/
public class FindLicenses extends Recipe {
/**
* Create recipe to find licenses
* @param includeTransitive Include transitive dependency licenses
* @param scope Optional scope filter
*/
public FindLicenses(@Nullable Boolean includeTransitive, @Nullable String scope);
}// Base class for search results
public abstract class SearchResult {
public abstract String getSourcePath();
public abstract int getLine();
public abstract int getColumn();
}
// Dependency search result
public static class DependencyFound extends SearchResult {
public String getGroupId();
public String getArtifactId();
public String getVersion();
public String getScope();
public boolean isDirect();
public List<String> getTransitivePath();
}
// Plugin search result
public static class PluginFound extends SearchResult {
public String getGroupId();
public String getArtifactId();
public String getVersion();
public String getConfiguration();
public List<String> getExecutions();
}
// Property search result
public static class PropertyFound extends SearchResult {
public String getKey();
public String getValue();
public String getSource(); // "current", "parent", "profile"
public int getUsageCount();
}Complex Search Examples:
// Find all test dependencies that might have security issues
FindDependency findTestDeps = new FindDependency("*", "*", "test", null);
FindSecurityVulnerabilities findVulns = new FindSecurityVulnerabilities("test", null);
// Combine searches
List<Recipe> searchRecipes = Arrays.asList(findTestDeps, findVulns);
Map<String, List<Object>> allResults = new HashMap<>();
for (Recipe recipe : searchRecipes) {
RecipeRun run = recipe.run(sourceFiles, ctx);
// Collect data tables from each recipe
run.getDataTables().forEach((key, value) -> {
allResults.computeIfAbsent(key, k -> new ArrayList<>()).addAll(value);
});
}
// Find outdated dependencies
DependencyInsight insight = new DependencyInsight("*", "*", null, true);
RecipeRun insightRun = insight.run(sourceFiles, ctx);
List<DependencyInsight.DependencyReport> reports = insightRun.getDataTable(DependencyInsight.DependencyReport.class);
List<DependencyInsight.DependencyReport> outdated = reports.stream()
.filter(report -> !report.getRequestedVersion().equals(report.getResolvedVersion()))
.collect(toList());
System.out.println("Found " + outdated.size() + " potentially outdated dependencies:");
for (DependencyInsight.DependencyReport report : outdated) {
System.out.println(" " + report.getGroupId() + ":" + report.getArtifactId() +
" (requested: " + report.getRequestedVersion() +
", resolved: " + report.getResolvedVersion() + ")");
}// Collect comprehensive project analysis
ExecutionContext ctx = new InMemoryExecutionContext();
List<Recipe> analysisRecipes = Arrays.asList(
new FindMavenProject(),
new EffectiveDependencies(null, true),
new EffectiveManagedDependencies(),
new FindSecurityVulnerabilities(null, null),
new FindLicenses(true, null)
);
Map<String, Object> projectAnalysis = new HashMap<>();
for (Recipe recipe : analysisRecipes) {
RecipeRun run = recipe.run(sourceFiles, ctx);
String recipeName = recipe.getClass().getSimpleName();
// Store all data tables from this recipe
run.getDataTables().forEach((tableName, tableData) -> {
projectAnalysis.put(recipeName + "." + tableName, tableData);
});
}
// Access collected data
List<FindMavenProject.MavenProjectData> projects =
(List<FindMavenProject.MavenProjectData>) projectAnalysis.get("FindMavenProject.MavenProjectData");
List<EffectiveDependencies.Effective> dependencies =
(List<EffectiveDependencies.Effective>) projectAnalysis.get("EffectiveDependencies.Effective");
// Generate report
System.out.println("Project Analysis Report:");
System.out.println(" Projects: " + projects.size());
System.out.println(" Dependencies: " + dependencies.size());
System.out.println(" Security Issues: " +
projectAnalysis.getOrDefault("FindSecurityVulnerabilities.Vulnerability", Collections.emptyList()).size());Install with Tessl CLI
npx tessl i tessl/maven-org-openrewrite--rewrite-maven