/**
   * Loads the dependency tree for the project via {@link #loadDependencyTree(MavenProject)} and
   * then uses the {@link DependencyNodeVisitor} to load the license data. If {@link #aggregating}
   * is enabled the method recurses on each child module.
   */
  @SuppressWarnings("unchecked")
  protected void parseProject(MavenProject project, DependencyNodeVisitor visitor)
      throws MojoExecutionException, MojoFailureException {
    final Log logger = this.getLog();
    logger.info("Parsing Dependencies for: " + project.getName());

    // Load and parse immediate dependencies
    final DependencyNode tree = this.loadDependencyTree(project);
    tree.accept(visitor);

    // If not including child deps don't recurse on modules
    if (!this.includeChildDependencies) {
      return;
    }

    // No child modules, return
    final List<MavenProject> collectedProjects = project.getCollectedProjects();
    if (collectedProjects == null) {
      return;
    }

    // Find all sub-modules for the project
    for (final MavenProject moduleProject : collectedProjects) {
      if (this.isExcluded(moduleProject, project.getArtifactId())) {
        continue;
      }

      this.parseProject(moduleProject, visitor);
    }
  }
 private void countHops() {
   hopCount = 0;
   DependencyNode parent = node.getParent();
   while (parent != null) {
     hopCount++;
     parent = parent.getParent();
   }
 }
 private ArtifactVersion extractArtifactVersion() {
   Artifact artifact = node.getArtifact();
   String version = artifact.getVersion();
   if (version != null) {
     return new DefaultArtifactVersion(version);
   }
   try {
     return artifact.getSelectedVersion();
   } catch (OverConstrainedVersionException e) {
     throw new RuntimeException("Version ranges problem with " + node.getArtifact(), e);
   }
 }
示例#4
0
  public PomClassLoader(ArtifactRepository localRepo, DependencyNode node, PomLoader pomLoader) {
    super(new URL[] {}, null);

    if (node.getArtifact().getFile() == null) {
      String s = localRepo.getBasedir() + File.separator + localRepo.pathOf(node.getArtifact());
      node.getArtifact().setFile(new File(s));
    }

    jarFile = node.getArtifact().getFile();
    if (jarFile == null) System.out.println("123");
    try {
      addURL(jarFile.toURI().toURL());
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
    name =
        node.getArtifact().getGroupId()
            + "-"
            + node.getArtifact().getArtifactId()
            + "-"
            + node.getArtifact().getVersion();
    pomLoader.add(this);

    includeDependency(localRepo, pomLoader, node.getChildren());
    getJSClassLoader();
  }
  /*
   * 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;
          }
        });
  }
示例#6
0
 private void includeDependency(
     ArtifactRepository localRepo, PomLoader pomLoader, Collection<DependencyNode> nodes) {
   for (DependencyNode dn : nodes) {
     if ("test".equals(dn.getArtifact().getScope())) continue;
     String n =
         dn.getArtifact().getGroupId()
             + "-"
             + dn.getArtifact().getArtifactId()
             + "-"
             + dn.getArtifact().getVersion();
     PomClassLoader pcl = pomLoader.get(n);
     if (pcl == null) {
       pcl = new PomClassLoader(localRepo, dn, pomLoader);
     }
     parents.add(pcl);
   }
 }
 private StringBuilder buildTreeString(DependencyNode node) {
   List<String> loc = new ArrayList<String>();
   DependencyNode currentNode = node;
   while (currentNode != null) {
     loc.add(getFullArtifactName(currentNode.getArtifact()));
     currentNode = currentNode.getParent();
   }
   Collections.reverse(loc);
   StringBuilder builder = new StringBuilder();
   for (int i = 0; i < loc.size(); i++) {
     for (int j = 0; j < i; j++) {
       builder.append("  ");
     }
     builder.append("+-").append(loc.get(i));
     builder.append("\n");
   }
   return builder;
 }
 public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
   if (log == null) {
     log = helper.getLog();
   }
   try {
     if (i18n == null) {
       i18n = (I18N) helper.getComponent(I18N.class);
     }
     DependencyNode node = getNode(helper);
     RequireUpperBoundDepsVisitor visitor = new RequireUpperBoundDepsVisitor();
     node.accept(visitor);
     List<String> errorMessages = buildErrorMessages(visitor.getConflicts());
     if (errorMessages.size() > 0) {
       throw new EnforcerRuleException(
           "Failed while enforcing RequireUpperBoundDeps. The error(s) are " + errorMessages);
     }
   } catch (ComponentLookupException e) {
     throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e);
   } catch (Exception e) {
     throw new EnforcerRuleException(e.getLocalizedMessage(), e);
   }
 }
示例#9
0
  /*
      public PomClassLoader(File file, PomLoader pomLoader) throws IOException, XmlPullParserException {
          super(new URL[]{}, null);
          this.pomLoader = pomLoader;
          if (file.getName().endsWith(".xml") || file.getName().endsWith(".pom"))
              setJarFile(file);
          else {
              haveJSLib = false;
              addURL(file.toURI().toURL());
          }
      }
  */
  public PomClassLoader(
      ArtifactRepository localRepo,
      DependencyNode node,
      File file,
      String name,
      PomLoader pomLoader) {
    super(new URL[] {}, null);
    haveJSLib = false;
    jarFile = file;
    try {
      addURL(jarFile.toURI().toURL());
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
    this.name = name;
    pomLoader.add(this);

    includeDependency(localRepo, pomLoader, node.getChildren());
    getJSClassLoader();
  }
  @NotNull
  private MavenServerExecutionResult createExecutionResult(
      File file, MavenExecutionResult result, DependencyNode rootNode) throws RemoteException {
    Collection<MavenProjectProblem> problems = MavenProjectProblem.createProblemsList();
    THashSet<MavenId> unresolvedArtifacts = new THashSet<MavenId>();

    validate(file, result.getExceptions(), problems, unresolvedArtifacts);

    MavenProject mavenProject = result.getMavenProject();
    if (mavenProject == null)
      return new MavenServerExecutionResult(null, problems, unresolvedArtifacts);

    MavenModel model =
        MavenModelConverter.convertModel(
            mavenProject.getModel(),
            mavenProject.getCompileSourceRoots(),
            mavenProject.getTestCompileSourceRoots(),
            mavenProject.getArtifacts(),
            (rootNode == null ? Collections.emptyList() : rootNode.getChildren()),
            mavenProject.getExtensionArtifacts(),
            getLocalRepositoryFile());

    RemoteNativeMavenProjectHolder holder = new RemoteNativeMavenProjectHolder(mavenProject);
    try {
      UnicastRemoteObject.exportObject(holder, 0);
    } catch (RemoteException e) {
      throw new RuntimeException(e);
    }

    Collection<String> activatedProfiles = collectActivatedProfiles(mavenProject);

    MavenServerExecutionResult.ProjectData data =
        new MavenServerExecutionResult.ProjectData(
            model,
            MavenModelConverter.convertToMap(mavenProject.getModel()),
            holder,
            activatedProfiles);
    return new MavenServerExecutionResult(data, problems, unresolvedArtifacts);
  }
  /**
   * 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;
  }
 /** Creates a new instance of ArtifactGraphEdge */
 public ArtifactGraphEdge(DependencyNode source, DependencyNode target) {
   edge = source.getArtifact().getId() + "--" + target.getArtifact().getId(); // NOI18N
   this.target = target;
   this.source = source;
 }
 private String constructKey() {
   Artifact artifact = node.getArtifact();
   return artifact.getGroupId() + ":" + artifact.getArtifactId();
 }