Well, here's how I did it.  It sure ain't pretty, but it works!

Briefly, I grab the set of artifacts from a MavenProject.  Usually because
I'm doing this from within a plugin whose dependency resolution has been set
to "test", these artifacts are resolved already, which is convenient, and I
don't bother to handle the case where they aren't already resolved.  Then I
invoke MavenProjectBuilder#buildFromRepository() on each of them to get
project information about each of them.  Now I have a list of
MavenProjects.  I hand them to a new ProjectSorter, which does the
topological sort I require, and keeps me blissfully ignorant of how exactly
Maven does its reactor sorting.

Finally, for each one, I grab its Artifact, and add it in order to the
returned list.  I threw in an ArtifactFilter in case I want to weed out
results.

Hopefully this will be useful for other people in similar situations.  Even
more hopefully someone will come along and ask pointedly why I went to all
the trouble to rewrite the ArtifactWhosieWhatsit#sort() method, and I will
reply because I didn't happen upon it.  :-)

Best,
Laird

public static final List<Artifact> topologicallySort(final MavenProject
project, MavenProjectBuilder builder, final ArtifactRepository
localRepository, final ArtifactFilter filter) throws
ProjectBuildingException, MissingProjectException,
DuplicateProjectException, CycleDetectedException {
    if (project == null) {
      throw new IllegalArgumentException("project == null");
    }
    List<Artifact> returnValue = null;

    @SuppressWarnings("unchecked")
    final Set<Artifact> artifacts = (Set<Artifact>)project.getArtifacts();
    if (artifacts != null) {
      returnValue = new ArrayList<Artifact>();
      if (!artifacts.isEmpty()) {
        final List<MavenProject> projects = new ArrayList<MavenProject>();
        projects.add(project);

        if (builder == null) {
          builder = new DefaultMavenProjectBuilder();
        }

        for (final Artifact artifact : artifacts) {
          if (artifact != null) {
            final MavenProject artifactProject =
builder.buildFromRepository(artifact,
project.getRemoteArtifactRepositories(), localRepository);
            assert artifactProject != null;
            projects.add(artifactProject);
          }
        }

        final ProjectSorter sorter = new ProjectSorter(projects);
        final List<MavenProject> sortedProjects =
sorter.getSortedProjects();
        assert sortedProjects != null;
        assert sortedProjects.size() == projects.size();

        final Map projectArtifactMap = project.getArtifactMap();
        assert projectArtifactMap != null;

        for (final MavenProject p : sortedProjects) {
          if (p != null) {
            final Artifact artifact = p.getArtifact();
            if (artifact != null) {
              final Artifact resolvedArtifact =
(Artifact)projectArtifactMap.get(artifact.getGroupId() + ":" +
artifact.getArtifactId());
              if (resolvedArtifact != null && (filter == null ||
filter.include(resolvedArtifact))) {
                returnValue.add(resolvedArtifact);
              }
            }
          }
        }

      }
    }

    return returnValue;
  }

Reply via email to