Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #168 from Apicurio/feat/dependency-maven-plugin
Browse files Browse the repository at this point in the history
Created a maven plugin to verify all dependencies for productization
  • Loading branch information
EricWittmann authored Aug 26, 2024
2 parents 8e97ef0 + 741399c commit 0ee8973
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
15 changes: 15 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>

<!-- maven -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- shared -->
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
<version>3.3.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.apicurio.common.apps.maven;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

@Mojo(name = "verify-dependencies")
public class VerifyDependenciesMojo extends AbstractMojo {

@Parameter(required = false, defaultValue = "compile,runtime")
String scopes;

@Component
private MavenProject project;

@Component
private MavenSession session;

@Component(hint = "default")
private DependencyGraphBuilder dependencyGraphBuilder;

/**
* @see org.apache.maven.plugin.Mojo#execute()
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
ArtifactFilter filter = new ArtifactFilter() {
@Override
public boolean include(org.apache.maven.artifact.Artifact artifact) {
return true;
}
};

try {
Set<String> matchingScopes = parseScopes(scopes);

ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(project);
DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, filter);

Set<Artifact> invalidArtifacts = new TreeSet<>(new ArtifactComparator());

DependencyNodeVisitor visitor = new DependencyNodeVisitor() {

@Override
public boolean visit(DependencyNode dependencyNode) {
Artifact artifact = dependencyNode.getArtifact();
if (matchingScopes.contains(artifact.getScope())) {
if (!isValid(artifact)) {
invalidArtifacts.add(artifact);
}
}
return true;
}

@Override
public boolean endVisit(DependencyNode dependencyNode) {
return true;
}
};
rootNode.accept(visitor);

if (!invalidArtifacts.isEmpty()) {
String serializedInvalidArtifacts = serialize(invalidArtifacts);
throw new MojoFailureException("Invalid dependencies found: \n" + serializedInvalidArtifacts);
}
} catch (MojoFailureException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException("Cannot build project dependency graph", e);
}
}

private boolean isValid(Artifact artifact) {
return artifact.getVersion().contains("-redhat-");
}

private static String serialize(Set<Artifact> invalidArtifacts) {
StringBuilder sb = new StringBuilder();
for (Artifact artifact : invalidArtifacts) {
sb.append(" ");
sb.append(artifact.getGroupId()).append(":").append(artifact.getArtifactId()).append(":").append(artifact.getVersion());
sb.append(" [").append(artifact.getScope()).append("]");
sb.append("\n");
}
return sb.toString();
}

private static Set<String> parseScopes(String scopes) {
Set<String> rval = new HashSet<>();
String[] splits = scopes.split(",");
for (String split : splits) {
split = split.trim();
if (!split.isEmpty()) {
rval.add(split);
}
}
return rval;
}

private static class ArtifactComparator implements Comparator<Artifact> {

@Override
public int compare(Artifact a1, Artifact a2) {
String a1s = a1.getGroupId() + ":" + a1.getArtifactId() + ":" + a1.getVersion();
String a2s = a2.getGroupId() + ":" + a2.getArtifactId() + ":" + a2.getVersion();
return a1s.compareTo(a2s);
}
}

}

0 comments on commit 0ee8973

Please sign in to comment.