@Override public void patch( final DiscoveryResult result, final List<? extends Location> locations, final Map<String, Object> context) { final ProjectVersionRef ref = result.getSelectedRef(); try { final MavenPomView pomView = (MavenPomView) context.get(POM_VIEW_CTX_KEY); // get all artifactItems with a version element. Only these need to be verified. final String depArtifactItemsPath = join(PATHS, "|"); logger.debug("Looking for dependency-plugin usages matching: '{}'", depArtifactItemsPath); // TODO: Switch to a DependencyView here, with path to dependencyManagement... final List<DependencyView> depArtifactItems = pomView.getAllDependenciesMatching(depArtifactItemsPath); if (depArtifactItems == null || depArtifactItems.isEmpty()) { return; } final Set<ProjectRelationship<?, ?>> accepted = new HashSet<ProjectRelationship<?, ?>>(result.getAcceptedRelationships()); final Map<VersionlessArtifactRef, DependencyRelationship> concreteDeps = new HashMap<VersionlessArtifactRef, DependencyRelationship>(); for (final ProjectRelationship<?, ?> rel : accepted) { if (rel instanceof DependencyRelationship && !rel.isManaged()) { final VersionlessArtifactRef key = new SimpleVersionlessArtifactRef(rel.getTargetArtifact()); logger.debug("Mapping existing dependency via key: {}", key); concreteDeps.put(key, (DependencyRelationship) rel); } } calculateDependencyPluginPatch(depArtifactItems, concreteDeps, ref, pomView, result); } catch (final GalleyMavenException e) { logger.error( String.format( "Failed to build/query MavenPomView for: %s from: %s. Reason: %s", ref, locations, e.getMessage()), e); } catch (final InvalidVersionSpecificationException e) { logger.error( String.format( "Failed to build/query MavenPomView for: %s from: %s. Reason: %s", ref, locations, e.getMessage()), e); } catch (final InvalidRefException e) { logger.error( String.format( "Failed to build/query MavenPomView for: %s from: %s. Reason: %s", ref, locations, e.getMessage()), e); } }
private void calculateDependencyPluginPatch( final List<DependencyView> depArtifactItems, final Map<VersionlessArtifactRef, DependencyRelationship> concreteDeps, final ProjectVersionRef ref, final MavenPomView pomView, final DiscoveryResult result) { logger.debug( "Detected {} dependency-plugin artifactItems that need to be accounted for in dependencies...", depArtifactItems == null ? 0 : depArtifactItems.size()); if (depArtifactItems != null && !depArtifactItems.isEmpty()) { final URI source = result.getSource(); for (final DependencyView depView : depArtifactItems) { try { final URI pomLocation = RelationshipUtils.profileLocation(depView.getProfileId()); final VersionlessArtifactRef depRef = depView.asVersionlessArtifactRef(); logger.debug("Detected dependency-plugin usage with key: {}", depRef); final DependencyRelationship dep = concreteDeps.get(depRef); if (dep != null) { if (!DependencyScope.runtime.implies(dep.getScope()) && (dep.getPomLocation().equals(pomLocation) || dep.getPomLocation() == POM_ROOT_URI)) { logger.debug("Correcting scope for: {}", dep); if (!result.removeDiscoveredRelationship(dep)) { logger.error("Failed to remove: {}", dep); } final Set<ProjectRef> excludes = dep.getExcludes(); final ProjectRef[] excludedRefs = excludes == null ? new ProjectRef[0] : excludes.toArray(new ProjectRef[excludes.size()]); final DependencyRelationship replacement = new SimpleDependencyRelationship( dep.getSources(), ref, dep.getTargetArtifact(), DependencyScope.embedded, dep.getIndex(), false, depView.getOriginInfo().isInherited(), depView.isOptional(), excludedRefs); if (!result.addDiscoveredRelationship(replacement)) { logger.error("Failed to inject: {}", replacement); } } } else if (depView.getVersion() != null) { logger.debug("Injecting new dep: {}", depView.asArtifactRef()); final DependencyRelationship injected = new SimpleDependencyRelationship( source, RelationshipUtils.profileLocation(depView.getProfileId()), ref, depView.asArtifactRef(), DependencyScope.embedded, concreteDeps.size(), false, depView.getOriginInfo().isInherited(), depView.isOptional()); if (!result.addDiscoveredRelationship(injected)) { logger.error("Failed to inject: {}", injected); } } else { logger.error( "Invalid dependency referenced in artifactItems of dependency plugin configuration: {}. " + "No version was specified, and it does not reference an actual dependency.", depRef); } } catch (final GalleyMavenException e) { logger.error( String.format( "Dependency is invalid: %s. Reason: %s. Skipping.", depView.toXML(), e.getMessage()), e); } catch (final InvalidVersionSpecificationException e) { logger.error( String.format( "Dependency is invalid: %s. Reason: %s. Skipping.", depView.toXML(), e.getMessage()), e); } catch (final InvalidRefException e) { logger.error( String.format( "Dependency is invalid: %s. Reason: %s. Skipping.", depView.toXML(), e.getMessage()), e); } } } }