use of org.commonjava.maven.ext.core.util.WildcardMap in project pom-manipulation-ext by release-engineering.
the class DependencyManipulator method applyOverrides.
/**
* Apply a set of version overrides to a list of dependencies. Return a set of the overrides which were not applied.
*
* @param project The current Project
* @param dependencies The list of dependencies
* @param explicitOverrides Any explicitOverrides to track for ignoring
* @param overrides The map of dependency version overrides
* @return The map of overrides that were not matched in the dependencies
* @throws ManipulationException if an error occurs
*/
private Map<ArtifactRef, String> applyOverrides(final Project project, final HashMap<ArtifactRef, Dependency> dependencies, final WildcardMap<String> explicitOverrides, final Map<ArtifactRef, String> overrides) throws ManipulationException {
// Duplicate the override map so unused overrides can be easily recorded
final Map<ArtifactRef, String> unmatchedVersionOverrides = new LinkedHashMap<>();
unmatchedVersionOverrides.putAll(overrides);
if (dependencies == null || dependencies.size() == 0) {
return unmatchedVersionOverrides;
}
final CommonState commonState = session.getState(CommonState.class);
final boolean strict = commonState.getStrict();
// Apply matching overrides to dependencies
for (final ArtifactRef dependency : dependencies.keySet()) {
ProjectRef depPr = new SimpleProjectRef(dependency.getGroupId(), dependency.getArtifactId());
// will most likely happen is last-wins.
for (final Map.Entry<ArtifactRef, String> entry : overrides.entrySet()) {
ProjectRef groupIdArtifactId = entry.getKey().asProjectRef();
if (depPr.equals(groupIdArtifactId)) {
final String oldVersion = dependencies.get(dependency).getVersion();
final String overrideVersion = entry.getValue();
final String resolvedValue = dependency.getVersionString();
if (isEmpty(overrideVersion)) {
logger.warn("Unable to align with an empty override version for " + groupIdArtifactId + "; ignoring");
} else if (isEmpty(oldVersion)) {
logger.debug("Dependency is a managed version for " + groupIdArtifactId + "; ignoring");
} else // By avoiding the potential duplicate work it also avoids a possible property clash problem.
if (explicitOverrides.containsKey(depPr)) {
logger.debug("Dependency {} matches known explicit override so not performing initial override pass.", depPr);
unmatchedVersionOverrides.remove(entry.getKey());
} else // have immediate access to the original property so the closest that is feasible is verify strict matching.
if (strict && oldVersion.contains("$") && !PropertiesUtils.checkStrictValue(session, resolvedValue, overrideVersion)) {
logger.debug("Original fully resolved version {} of {} does not match override version {} -> {} so ignoring", resolvedValue, dependency, entry.getKey(), overrideVersion);
if (commonState.getFailOnStrictViolation()) {
throw new ManipulationException("For {} replacing original property version {} (fully resolved: {} ) with new version {} for {} violates the strict version-alignment rule!", depPr.toString(), dependencies.get(dependency).getVersion(), resolvedValue, entry.getKey().getVersionString(), entry.getKey().asProjectRef().toString());
} else {
logger.warn("Replacing original property version {} with new version {} for {} violates the strict version-alignment rule!", resolvedValue, overrideVersion, dependencies.get(dependency).getVersion());
}
} else {
// Too much spurious logging with project.version.
if (!oldVersion.equals("${project.version}")) {
logger.info("Updating version {} for dependency {} from {}.", overrideVersion, dependency, project.getPom());
}
if (!PropertiesUtils.cacheProperty(project, commonState, versionPropertyUpdateMap, oldVersion, overrideVersion, entry.getKey(), false)) {
if (oldVersion.equals("${project.version}")) {
logger.debug("For dependency {} ; version is built in {} so skipping inlining {}", groupIdArtifactId, oldVersion, overrideVersion);
} else if (strict && !PropertiesUtils.checkStrictValue(session, resolvedValue, overrideVersion)) {
if (commonState.getFailOnStrictViolation()) {
throw new ManipulationException("Replacing original version {} in dependency {} with new version {} violates the strict version-alignment rule!", oldVersion, groupIdArtifactId.toString(), overrideVersion);
} else {
logger.warn("Replacing original version {} in dependency {} with new version {} violates the strict version-alignment rule!", oldVersion, groupIdArtifactId, overrideVersion);
}
} else {
logger.debug("Altered dependency {} : {} -> {}", groupIdArtifactId, oldVersion, overrideVersion);
if (oldVersion.contains("${")) {
String suffix = PropertiesUtils.getSuffix(session);
String replaceVersion;
if (commonState.getStrictIgnoreSuffix() && oldVersion.contains(suffix)) {
replaceVersion = StringUtils.substringBefore(oldVersion, suffix);
replaceVersion += suffix + StringUtils.substringAfter(overrideVersion, suffix);
} else {
replaceVersion = oldVersion + StringUtils.removeStart(overrideVersion, resolvedValue);
}
logger.debug("Resolved value is {} and replacement version is {} ", resolvedValue, replaceVersion);
// In this case the previous value couldn't be cached even though it contained a property
// as it was either multiple properties or a property combined with a hardcoded value. Therefore
// just append the suffix.
dependencies.get(dependency).setVersion(replaceVersion);
} else {
dependencies.get(dependency).setVersion(overrideVersion);
}
}
}
unmatchedVersionOverrides.remove(entry.getKey());
}
}
}
}
return unmatchedVersionOverrides;
}
use of org.commonjava.maven.ext.core.util.WildcardMap in project pom-manipulation-ext by release-engineering.
the class DependencyManipulator method apply.
/**
* Applies dependency overrides to the project.
*/
private void apply(final Project project, final Model model, final Map<ArtifactRef, String> overrides) throws ManipulationException {
// Map of Group : Map of artifactId [ may be wildcard ] : value
final WildcardMap<String> explicitOverrides = new WildcardMap<>();
final String projectGA = ga(project);
final DependencyState dependencyState = session.getState(DependencyState.class);
final CommonState commonState = session.getState(CommonState.class);
logger.info("Processing project {} ", projectGA);
Map<ArtifactRef, String> moduleOverrides = new LinkedHashMap<>(overrides);
moduleOverrides = removeReactorGAs(moduleOverrides);
try {
moduleOverrides = applyModuleVersionOverrides(projectGA, dependencyState.getDependencyExclusions(), moduleOverrides, explicitOverrides);
logger.debug("Module overrides are:\n{}", moduleOverrides);
logger.debug("Explicit overrides are:\n{}", explicitOverrides);
} catch (InvalidRefException e) {
logger.error("Invalid module exclusion override {} : {} ", moduleOverrides, explicitOverrides);
throw e;
}
if (project.isInheritanceRoot()) {
// Handle the situation where the top level parent refers to a prior build that is in the BOM.
if (project.getModelParent() != null) {
for (Map.Entry<ArtifactRef, String> entry : moduleOverrides.entrySet()) {
String oldValue = project.getModelParent().getVersion();
String newValue = entry.getValue();
if (entry.getKey().asProjectRef().equals(SimpleProjectRef.parse(ga(project.getModelParent())))) {
if (commonState.getStrict()) {
if (!PropertiesUtils.checkStrictValue(session, oldValue, newValue)) {
if (commonState.getFailOnStrictViolation()) {
throw new ManipulationException("Parent reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", ga(project.getModelParent()), newValue, oldValue);
} else {
logger.warn("Parent reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", ga(project.getModelParent()), newValue, oldValue);
// a new property either.
continue;
}
}
}
logger.debug(" Modifying parent reference from {} to {} for {} ", model.getParent().getVersion(), newValue, ga(project.getModelParent()));
model.getParent().setVersion(newValue);
break;
}
}
// Apply any explicit overrides to the top level parent. Convert it to a simulated
// dependency so we can reuse applyExplicitOverrides.
HashMap<ArtifactRef, Dependency> pDepMap = new HashMap<>();
Dependency d = new Dependency();
d.setGroupId(project.getModelParent().getGroupId());
d.setArtifactId(project.getModelParent().getArtifactId());
d.setVersion(project.getModelParent().getVersion());
pDepMap.put(SimpleArtifactRef.parse(d.getManagementKey()), d);
applyExplicitOverrides(project, pDepMap, explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
project.getModelParent().setVersion(d.getVersion());
}
if (session.getState(DependencyState.class).getOverrideDependencies()) {
// Apply overrides to project dependency management
logger.debug("Applying overrides to managed dependencies for: {}", projectGA);
final Map<ArtifactRef, String> nonMatchingVersionOverrides = applyOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, moduleOverrides);
final Map<ArtifactRef, String> matchedOverrides = new LinkedHashMap<>(moduleOverrides);
matchedOverrides.keySet().removeAll(nonMatchingVersionOverrides.keySet());
applyExplicitOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
if (commonState.getOverrideTransitive()) {
final List<Dependency> extraDeps = new ArrayList<>();
// Add dependencies to Dependency Management which did not match any existing dependency
for (final ArtifactRef var : overrides.keySet()) {
if (!nonMatchingVersionOverrides.containsKey(var)) {
// This one in the remote pom was already dealt with ; continue.
continue;
}
final Dependency newDependency = new Dependency();
newDependency.setGroupId(var.getGroupId());
newDependency.setArtifactId(var.getArtifactId());
newDependency.setType(var.getType());
newDependency.setClassifier(var.getClassifier());
final String artifactVersion = moduleOverrides.get(var);
newDependency.setVersion(artifactVersion);
extraDeps.add(newDependency);
logger.debug("New entry added to <DependencyManagement/> - {} : {} ", var, artifactVersion);
}
// If the model doesn't have any Dependency Management set by default, create one for it
DependencyManagement dependencyManagement = model.getDependencyManagement();
if (extraDeps.size() > 0) {
if (dependencyManagement == null) {
dependencyManagement = new DependencyManagement();
model.setDependencyManagement(dependencyManagement);
logger.debug("Added <DependencyManagement/> for current project");
}
dependencyManagement.getDependencies().addAll(0, extraDeps);
}
} else {
logger.debug("Non-matching dependencies ignored.");
}
} else {
logger.debug("NOT applying overrides to managed dependencies for top-pom: {}", projectGA);
}
} else {
// If a child module has a depMgmt section we'll change that as well.
if (session.getState(DependencyState.class).getOverrideDependencies()) {
logger.debug("Applying overrides to managed dependencies for: {}", projectGA);
applyOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, moduleOverrides);
applyExplicitOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
} else {
logger.debug("NOT applying overrides to managed dependencies for: {}", projectGA);
}
}
if (session.getState(DependencyState.class).getOverrideDependencies()) {
logger.debug("Applying overrides to concrete dependencies for: {}", projectGA);
// Apply overrides to project direct dependencies
applyOverrides(project, project.getResolvedDependencies(session), explicitOverrides, moduleOverrides);
applyExplicitOverrides(project, project.getResolvedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
final HashMap<Profile, HashMap<ArtifactRef, Dependency>> pd = project.getResolvedProfileDependencies(session);
final HashMap<Profile, HashMap<ArtifactRef, Dependency>> pmd = project.getResolvedProfileManagedDependencies(session);
for (Profile p : pd.keySet()) {
applyOverrides(project, pd.get(p), explicitOverrides, moduleOverrides);
applyExplicitOverrides(project, pd.get(p), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
}
for (Profile p : pmd.keySet()) {
applyOverrides(project, pmd.get(p), explicitOverrides, moduleOverrides);
applyExplicitOverrides(project, pmd.get(p), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
}
} else {
logger.debug("NOT applying overrides to concrete dependencies for: {}", projectGA);
}
}
Aggregations