/**
  * Uses the {@link EnforcerRuleHelper} to populate the values of the {@link
  * DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory,
  * ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} factory method. <br>
  * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has
  * to do.
  *
  * @param helper
  * @return a Dependency Node which is the root of the project's dependency tree
  * @throws EnforcerRuleException when the build should fail
  */
 private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException {
   try {
     MavenProject project = (MavenProject) helper.evaluate("${project}");
     DependencyTreeBuilder dependencyTreeBuilder =
         (DependencyTreeBuilder) helper.getComponent(DependencyTreeBuilder.class);
     ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}");
     ArtifactFactory factory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class);
     ArtifactMetadataSource metadataSource =
         (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class);
     ArtifactCollector collector =
         (ArtifactCollector) helper.getComponent(ArtifactCollector.class);
     ArtifactFilter filter = null; // we need to evaluate all scopes
     DependencyNode node =
         dependencyTreeBuilder.buildDependencyTree(
             project, repository, factory, metadataSource, filter, collector);
     return node;
   } catch (ExpressionEvaluationException e) {
     throw new EnforcerRuleException(
         "Unable to lookup an expression " + e.getLocalizedMessage(), e);
   } catch (ComponentLookupException e) {
     throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e);
   } catch (DependencyTreeBuilderException e) {
     throw new EnforcerRuleException(
         "Could not build dependency tree " + e.getLocalizedMessage(), e);
   }
 }
 private DependencyNode resolveProject(MavenProject project, final MyArtifactFilter filter) {
   try {
     abstractMojo.getLog().debug("DependencyProcessor: Resolving project: " + project.getId());
     return dependencyTreeBuilder.buildDependencyTree(
         project, localRepository, factory, artifactMetadataSource, filter, collector);
   } catch (DependencyTreeBuilderException e) {
     abstractMojo.getLog().error("DependencyProcessor: Unable to build dependency tree.", e);
     return null;
   }
 }
  /*
   * Determine list of exports by bundles that have been marked provided in the pom
   * //TODO: we probably want to figure this out somewhere from the Karaf build itself instead of putting the burden on the user
   */
  private void readProvidedBundles() throws Exception {
    DependencyNode tree =
        dependencyTreeBuilder.buildDependencyTree(
            project,
            localRepo,
            factory,
            artifactMetadataSource,
            new ArtifactFilter() {

              public boolean include(Artifact artifact) {
                return true;
              }
            },
            collector);
    tree.accept(
        new DependencyNodeVisitor() {
          public boolean endVisit(DependencyNode node) {
            // we want the next sibling too
            return true;
          }

          public boolean visit(DependencyNode node) {
            if (node.getState() != DependencyNode.OMITTED_FOR_CONFLICT) {
              Artifact artifact = node.getArtifact();
              info("    scanning %s for exports", artifact);
              if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
                  && !artifact.getType().equals("pom")) {
                try {
                  for (Clause clause : ManifestUtils.getExports(getManifest("", artifact))) {
                    getLog()
                        .debug(" adding " + clause.getName() + " to list of available packages");
                    systemExports.add(clause.getName());
                  }
                } catch (ArtifactResolutionException e) {
                  error("Unable to find bundle exports for %s: %s", e, artifact, e.getMessage());
                } catch (ArtifactNotFoundException e) {
                  error("Unable to find bundle exports for %s: %s", e, artifact, e.getMessage());
                } catch (IOException e) {
                  error("Unable to find bundle exports for %s: %s", e, artifact, e.getMessage());
                }
              }
            }
            // we want the children too
            return true;
          }
        });
  }
  private Set<Artifact> resolveFromTree() {
    final Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();

    try {
      // we have to do this because Aether does not work.
      dependencyTreeBuilder
          .buildDependencyTree(
              project, localRepository, artifactFactory, artifactMetadataSource, artifactCollector)
          .getRootNode()
          .accept(
              new DependencyNodeVisitor() {
                @Override
                public boolean visit(DependencyNode dependencyNode) {
                  Artifact artifact = dependencyNode.getArtifact();

                  if (dependencyNode.getState() != DependencyNode.INCLUDED) return true;

                  if (artifact.getArtifactId().equals(project.getArtifactId())
                      && artifact.getGroupId().equals(project.getGroupId())) return true;

                  try {
                    artifactResolver.resolve(artifact, remoteRepositories, localRepository);
                  } catch (ArtifactResolutionException e) {
                    throw new RuntimeException(e);
                  } catch (ArtifactNotFoundException e) {
                    throw new RuntimeException(e);
                  }

                  resolvedArtifacts.add(artifact);
                  return true;
                }

                @Override
                public boolean endVisit(DependencyNode dependencyNode) {
                  return true;
                }
              });
    } catch (DependencyTreeBuilderException e) {
      throw new RuntimeException(e);
    }

    return resolvedArtifacts;
  }
Esempio n. 5
0
  public void execute() throws MojoExecutionException {
    try {
      DependencyVisualizer visualizer = new DependencyVisualizer();
      visualizer.cascade = cascade;
      visualizer.direction = direction;
      visualizer.hideOptional = hideOptional;
      visualizer.hidePoms = hidePoms;
      visualizer.hideOmitted = hideOmitted;
      visualizer.hideExternal = hideExternal;
      visualizer.hideVersion = hideVersion;
      visualizer.hideGroupId = hideGroupId;
      visualizer.hideType = hideType;
      visualizer.keepDot = keepDot;
      visualizer.label = label;
      visualizer.hideTransitive = hideTransitive;
      visualizer.log = getLog();
      visualizer.artifactGroupId = artifactGroupId;

      if (hideScopes != null) {
        for (String scope : hideScopes.split(",")) {
          visualizer.hideScopes.add(scope);
        }
      }

      ArrayList<MavenProject> projects = new ArrayList<MavenProject>();
      collectProjects(projects);

      for (MavenProject p : projects) {
        DependencyNode node = treeBuilder.buildDependencyTree(p, localRepository, null);
        visualizer.add(node);
      }

      getTarget().getParentFile().mkdirs();
      visualizer.export(getTarget());
      getLog().info("Dependency graph exported to: " + getTarget());
    } catch (DependencyTreeBuilderException e) {
      throw new MojoExecutionException("Could not build the depedency tree.", e);
    }
  }
  @Override
  public void run(PlexusContainer plexusContainer) throws Exception {
    Settings settings = mavenSettingsBuilder.buildSettings();

    File localRepository = new File(settings.getLocalRepository());
    ArtifactRepository artifactRepository =
        new DefaultArtifactRepository(
            "local", localRepository.toURI().toASCIIString(), artifactRepositoryLayout);

    ProfileManager profileManager =
        new DefaultProfileManager(plexusContainer, settings, System.getProperties());

    Method method =
        maven.getClass().getDeclaredMethod("resolveParameters", Settings.class, Properties.class);
    method.setAccessible(true);
    method.invoke(maven, settings, new Properties());

    MavenProject mavenProject =
        mavenProjectBuilder.build(
            new File("c:\\workspace\\AppercutScanner\\pom.xml"),
            artifactRepository,
            profileManager);

    plexusContainer.getLogger().info(mavenProject.toString());

    DependencyNode rootNode =
        dependencyTreeBuilder.buildDependencyTree(
            mavenProject,
            artifactRepository,
            artifactFactory,
            artifactMetadataSource,
            null,
            artifactCollector);

    plexusContainer.getLogger().info("rootNode:\n" + rootNode);
  }
  /**
   * Create classpath URLs list.
   *
   * @return classpath URL list
   * @throws MojoFailureException error creating URLs list
   */
  @SuppressWarnings("unchecked")
  private List<URL> createClasspathURLsList() throws MojoFailureException {
    List<URL> urls = new LinkedList<URL>();

    // 1. Get all project dependencies
    DependencyNode rootNode = null;
    try {
      ArtifactFilter artifactFilter = new ScopeArtifactFilter(null);
      rootNode =
          treeBuilder.buildDependencyTree(
              project,
              localRepository,
              artifactFactory,
              artifactMetadataSource,
              artifactFilter,
              artifactCollector);
    } catch (DependencyTreeBuilderException e) {
      throw new MojoFailureException(e.getMessage());
    }

    CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();

    rootNode.accept(visitor);
    List<DependencyNode> nodes = visitor.getNodes();

    // 2. Get all URL dependencies
    Set<Artifact> artifacts = new HashSet<Artifact>();
    for (DependencyNode dependencyNode : nodes) {
      // Initialize artifact
      Artifact artifact = dependencyNode.getArtifact();
      if (artifacts.contains(artifact)) {
        // Artifact already parsed
        continue;
      }
      artifacts.add(artifact);

      // Skip non jar type
      if (!EXTENSION_JAR.equalsIgnoreCase(artifact.getType())) {
        continue;
      }

      if (artifact.getArtifactId().equals(project.getArtifactId())) {
        getLog().info("Skip artifact: " + artifact);
        continue;
      }

      // Other scope
      try {
        URL url = extractArtifactUrl(artifact);
        urls.add(url);
      } catch (IOException e) {
        getLog().warn("Can't get URL object for artifact: " + artifact);
        continue;
      }
    }

    /*
          try {
    	urls.add(new File(project.getBasedir(), "target/classes").toURI().toURL());
    } catch (MalformedURLException e) {
    	getLog().warn("Can't add the target/classes of current maven project to classpath. Error: " + e);
    }
          */

    return urls;
  }