最后,这就是使用 Maven API 的方法。我使用了 Google Guava 和 Apache Commons Lang 3,所以如果您不熟悉这些库,请询问。
/**
* Gets all dependencies for an artifact, including the ones from WAR dependencies.
* @param artifact The artifact
* @return The dependencies, where the key is the artifactId and the values are all of its artifacts (different
* versions, different scopes)
* @throws MojoExecutionException if the dependencies can't be fetched
*/
protected Multimap<String, Artifact> getAllDependencies(final Artifact artifact) throws MojoExecutionException {
Multimap<String, Artifact> projectDependencies = getDependencies(artifact);
Multimap<String, Artifact> allDependencies = ArrayListMultimap.create(projectDependencies);
for (Artifact dependency : projectDependencies.values()) {
if (dependency.getType().equals("war")) {
Multimap<String, Artifact> warDependencies = getDependencies(dependency);
for (Artifact warArtifact : warDependencies.values()) {
if (!containsArtifact(allDependencies, warArtifact)) {
allDependencies.put(warArtifact.getArtifactId(), warArtifact);
}
}
}
}
return allDependencies;
}
/**
* Gets the dependencies for an artifact.
* @param artifact The artifact
* @return The dependencies, where the key is the artifactId and the values are all of its artifacts (different
* versions, different scopes)
* @throws MojoExecutionException if the dependencies can't be fetched
*/
protected Multimap<String, Artifact> getDependencies(final Artifact artifact) throws MojoExecutionException {
try {
Artifact pomArtifact = this.repositorySystem.createProjectArtifact(artifact.getGroupId(),
artifact.getArtifactId(), artifact.getVersion());
MavenProject mavenProject = this.mavenProjectBuilder.buildFromRepository(pomArtifact, this.remoteRepositories,
this.localRepository);
Multimap<String, Artifact> dependencies = ArrayListMultimap.create();
DependencyNode rootNode = this.dependencyGraphBuilder.buildDependencyGraph(mavenProject, this.artifactFilter);
CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
rootNode.accept(visitor);
for (DependencyNode dependencyNode : visitor.getNodes()) {
Artifact dependencyArtifact = dependencyNode.getArtifact();
if (!containsArtifact(dependencies, dependencyArtifact)) {
dependencies.put(dependencyArtifact.getArtifactId(), dependencyArtifact);
}
}
return dependencies;
} catch (Exception e) {
throw new MojoExecutionException("Can't get dependencies for " + artifact, e);
}
}
/**
* Shows whether an artifact is contained in the given artifacts.
* @param artifacts The artifacts
* @param artifact The artifact to check
* @return {@code true} if the artifact is contained, otherwise {@code false}
*/
protected boolean containsArtifact(final Multimap<String, Artifact> artifacts, final Artifact artifact) {
String artifactId = artifact.getArtifactId();
if (!artifacts.containsKey(artifactId)) {
return false;
} else {
String coordinates = getCoordinates(artifact);
for (Artifact existingArtifact : artifacts.get(artifactId)) {
String existingCoordinates = getCoordinates(existingArtifact);
if (coordinates.equals(existingCoordinates)) {
return true;
}
}
return false;
}
}
/**
* Gets the Maven coordinates for an artifact.
* @param artifact The artifact
* @return The Maven coordinates
*/
protected static String getCoordinates(final Artifact artifact) {
List<String> parts = Lists.newArrayList(artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(),
artifact.getVersion(), artifact.getScope());
return StringUtils.join(parts, ":");
}