Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aether-users] Sort dependencies topologically

Hi,

In a Maven plugin, I'm trying to sort the dependencies expressed in the pom.xml (including the transitive ones) in a topological way. I tried to use the PreorderNodeListGenerator from Aether.
Here's an example of what I except; let's consider a project A with 2 direct dependencies C and B, with their transitive dependencies (arrows mean "depend on")
A -> C -> E and A -> B -> D -> E
From project A, I would expect my plugin to sort the dependencies as follow A-B-D-C-E (having E at the end is the important thing). However PreorderNodeListGenerator orders the dependencies like that: A-C-E-B-D. Note that this order differs when the dependencies in A's pom.xml are changed.
I have the feeling that PreorderNodeListGenerator sorts a reduced graph, where the conflicts and so have already been resolved.

Does anyone have the same problem as me? Or am I mistaken with the functionality of PreorderNodeListGenerator ?

Cheers,
Jérémy

PS: If you want to try it out, here's the code to paste in a simple Mojo

import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.CollectResult;
import org.sonatype.aether.collection.DependencyCollectionException;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.graph.FilteringDependencyVisitor;
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;

/**
 * 
 * @goal z
 * 
 */
public class MyMojo extends AbstractMojo {
/**
* The entry point to Aether, i.e. the component doing all the work.
* @component
*/
private RepositorySystem repoSystem;

/**
* The current repository/network configuration of Maven.
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession repoSession;

/**
* The project's remote repositories to use for the resolution.
* @parameter default-value="${project.remoteProjectRepositories}"
* @readonly
*/
private List<RemoteRepository> remoteRepos;

/**
* @parameter default-value="${project}"
* @readonly
*/
private MavenProject project;


public void execute() throws MojoFailureException, MojoExecutionException {

Dependency dependency = new Dependency(new DefaultArtifact(project.getArtifact().getId()), "compile");

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(dependency);
collectRequest.setRepositories(remoteRepos);

CollectResult collectResult = null;
try {   
collectResult = repoSystem.collectDependencies(repoSession, collectRequest);
} catch (DependencyCollectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
   FilteringDependencyVisitor fdv = new FilteringDependencyVisitor(nlg, null);

DependencyNode node = collectResult.getRoot();

node.accept(fdv);
for (DependencyNode dependencyNode : nlg.getNodes()) {
System.out.println(dependencyNode);
}
}
}

Back to the top