protected void checkForRedundantVersions( final MavenProject mavenProject, final ResultCollector resultCollector, final ObjectWithPath<?> object, final ObjectWithPath<?> inheritedObject, final String dependencyDescription, final String inheritedDescription) { Object modelObject = object.getObject(); Object resolvedModelObject = tryResolveObject(object); if (isExcluded(resolvedModelObject)) { return; } String version = resolveVersion(modelObject, resolvedModelObject); String inheritedVersion = modelUtil.getVersion(tryResolveObject(inheritedObject)); // both have a version, but if they're different, that might be ok. // But if they're the same, then one is redundant. if (version != null && inheritedVersion != null && inheritedVersion.equals(version)) { InputLocation location = modelUtil.getLocation(modelObject, "version"); String message = dependencyDescription + " '" + modelUtil.getKey(modelObject) + "' has same version (" + version + ") as " + inheritedDescription; resultCollector.addViolation(mavenProject, this, message, location); } }
private Object tryResolveObject(final ObjectWithPath<?> objectWithPath) { try { return resolveObject(objectWithPath); } catch (final IllegalStateException e) { log.warn(e); return objectWithPath.getObject(); } }
/** * This uses the path of an object that was found in the "original model", and tries to find the * same object in the "model". The "model" has had properties replaced with values, so this is how * we find the resolved version of the object. */ private Object resolveObject(final ObjectWithPath<?> objectWithPath) { Object object = objectWithPath.getObject(); StringBuilder path = new StringBuilder(); path.append(objectWithPath.getPath()); path.append("["); path.append("groupId='").append(modelUtil.getGroupId(object)).append("'"); path.append(" and artifactId='").append(modelUtil.getArtifactId(object)).append("'"); String type = modelUtil.tryGetType(object); if (type != null) { path.append(" and type='").append(type).append("'"); } else { path.append(" and not(type)"); } String classifier = modelUtil.tryGetClassifier(object); if (classifier != null) { path.append(" and classifier='").append(classifier).append("'"); } else { path.append(" and not(classifier)"); } path.append("]"); // evaluate embedded properties if (path.toString().contains("${")) { try { Object evaluate = pluginParameterExpressionEvaluator.evaluate(path.toString()); path = new StringBuilder(evaluate.toString()); } catch (ExpressionEvaluationException e) { throw new IllegalStateException(e); } } Model model = objectWithPath.getProject().getModel(); Collection<Object> objects = expressionEvaluator.getPath(model, path.toString()); if (objects.isEmpty()) { throw new IllegalStateException("Could not resolve " + object + " using path " + path); } else if (objects.size() > 1) { throw new IllegalStateException("Found " + objects.size() + " objects using path " + path); } else { return objects.iterator().next(); } }